Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\BrowserKit;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * History.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
18 */
|
Chris@0
|
19 class History
|
Chris@0
|
20 {
|
Chris@0
|
21 protected $stack = array();
|
Chris@0
|
22 protected $position = -1;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Clears the history.
|
Chris@0
|
26 */
|
Chris@0
|
27 public function clear()
|
Chris@0
|
28 {
|
Chris@0
|
29 $this->stack = array();
|
Chris@0
|
30 $this->position = -1;
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * Adds a Request to the history.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @param Request $request A Request instance
|
Chris@0
|
37 */
|
Chris@0
|
38 public function add(Request $request)
|
Chris@0
|
39 {
|
Chris@0
|
40 $this->stack = array_slice($this->stack, 0, $this->position + 1);
|
Chris@0
|
41 $this->stack[] = clone $request;
|
Chris@0
|
42 $this->position = count($this->stack) - 1;
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * Returns true if the history is empty.
|
Chris@0
|
47 *
|
Chris@0
|
48 * @return bool true if the history is empty, false otherwise
|
Chris@0
|
49 */
|
Chris@0
|
50 public function isEmpty()
|
Chris@0
|
51 {
|
Chris@0
|
52 return count($this->stack) == 0;
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * Goes back in the history.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @return Request A Request instance
|
Chris@0
|
59 *
|
Chris@0
|
60 * @throws \LogicException if the stack is already on the first page
|
Chris@0
|
61 */
|
Chris@0
|
62 public function back()
|
Chris@0
|
63 {
|
Chris@0
|
64 if ($this->position < 1) {
|
Chris@0
|
65 throw new \LogicException('You are already on the first page.');
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 return clone $this->stack[--$this->position];
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * Goes forward in the history.
|
Chris@0
|
73 *
|
Chris@0
|
74 * @return Request A Request instance
|
Chris@0
|
75 *
|
Chris@0
|
76 * @throws \LogicException if the stack is already on the last page
|
Chris@0
|
77 */
|
Chris@0
|
78 public function forward()
|
Chris@0
|
79 {
|
Chris@0
|
80 if ($this->position > count($this->stack) - 2) {
|
Chris@0
|
81 throw new \LogicException('You are already on the last page.');
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 return clone $this->stack[++$this->position];
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 /**
|
Chris@0
|
88 * Returns the current element in the history.
|
Chris@0
|
89 *
|
Chris@0
|
90 * @return Request A Request instance
|
Chris@0
|
91 *
|
Chris@0
|
92 * @throws \LogicException if the stack is empty
|
Chris@0
|
93 */
|
Chris@0
|
94 public function current()
|
Chris@0
|
95 {
|
Chris@0
|
96 if (-1 == $this->position) {
|
Chris@0
|
97 throw new \LogicException('The page history is empty.');
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 return clone $this->stack[$this->position];
|
Chris@0
|
101 }
|
Chris@0
|
102 }
|