4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\BrowserKit;
17 * @author Fabien Potencier <fabien@symfony.com>
21 protected $stack = array();
22 protected $position = -1;
27 public function clear()
29 $this->stack = array();
34 * Adds a Request to the history.
36 public function add(Request $request)
38 $this->stack = array_slice($this->stack, 0, $this->position + 1);
39 $this->stack[] = clone $request;
40 $this->position = count($this->stack) - 1;
44 * Returns true if the history is empty.
46 * @return bool true if the history is empty, false otherwise
48 public function isEmpty()
50 return 0 == count($this->stack);
54 * Goes back in the history.
56 * @return Request A Request instance
58 * @throws \LogicException if the stack is already on the first page
60 public function back()
62 if ($this->position < 1) {
63 throw new \LogicException('You are already on the first page.');
66 return clone $this->stack[--$this->position];
70 * Goes forward in the history.
72 * @return Request A Request instance
74 * @throws \LogicException if the stack is already on the last page
76 public function forward()
78 if ($this->position > count($this->stack) - 2) {
79 throw new \LogicException('You are already on the last page.');
82 return clone $this->stack[++$this->position];
86 * Returns the current element in the history.
88 * @return Request A Request instance
90 * @throws \LogicException if the stack is empty
92 public function current()
94 if (-1 == $this->position) {
95 throw new \LogicException('The page history is empty.');
98 return clone $this->stack[$this->position];