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 * @param Request $request A Request instance
38 public function add(Request $request)
40 $this->stack = array_slice($this->stack, 0, $this->position + 1);
41 $this->stack[] = clone $request;
42 $this->position = count($this->stack) - 1;
46 * Returns true if the history is empty.
48 * @return bool true if the history is empty, false otherwise
50 public function isEmpty()
52 return count($this->stack) == 0;
56 * Goes back in the history.
58 * @return Request A Request instance
60 * @throws \LogicException if the stack is already on the first page
62 public function back()
64 if ($this->position < 1) {
65 throw new \LogicException('You are already on the first page.');
68 return clone $this->stack[--$this->position];
72 * Goes forward in the history.
74 * @return Request A Request instance
76 * @throws \LogicException if the stack is already on the last page
78 public function forward()
80 if ($this->position > count($this->stack) - 2) {
81 throw new \LogicException('You are already on the last page.');
84 return clone $this->stack[++$this->position];
88 * Returns the current element in the history.
90 * @return Request A Request instance
92 * @throws \LogicException if the stack is empty
94 public function current()
96 if (-1 == $this->position) {
97 throw new \LogicException('The page history is empty.');
100 return clone $this->stack[$this->position];