Drupal investigation

History.php 2.3KB

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