菜谱项目

MockArraySessionStorage.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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\HttpFoundation\Session\Storage;
  11. use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
  12. /**
  13. * MockArraySessionStorage mocks the session for unit tests.
  14. *
  15. * No PHP session is actually started since a session can be initialized
  16. * and shutdown only once per PHP execution cycle.
  17. *
  18. * When doing functional testing, you should use MockFileSessionStorage instead.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
  22. * @author Drak <drak@zikula.org>
  23. */
  24. class MockArraySessionStorage implements SessionStorageInterface
  25. {
  26. /**
  27. * @var string
  28. */
  29. protected $id = '';
  30. /**
  31. * @var string
  32. */
  33. protected $name;
  34. /**
  35. * @var bool
  36. */
  37. protected $started = false;
  38. /**
  39. * @var bool
  40. */
  41. protected $closed = false;
  42. /**
  43. * @var array
  44. */
  45. protected $data = array();
  46. /**
  47. * @var MetadataBag
  48. */
  49. protected $metadataBag;
  50. /**
  51. * @var array|SessionBagInterface[]
  52. */
  53. protected $bags = array();
  54. /**
  55. * @param string $name Session name
  56. * @param MetadataBag $metaBag MetadataBag instance
  57. */
  58. public function __construct($name = 'MOCKSESSID', MetadataBag $metaBag = null)
  59. {
  60. $this->name = $name;
  61. $this->setMetadataBag($metaBag);
  62. }
  63. public function setSessionData(array $array)
  64. {
  65. $this->data = $array;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function start()
  71. {
  72. if ($this->started) {
  73. return true;
  74. }
  75. if (empty($this->id)) {
  76. $this->id = $this->generateId();
  77. }
  78. $this->loadSession();
  79. return true;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function regenerate($destroy = false, $lifetime = null)
  85. {
  86. if (!$this->started) {
  87. $this->start();
  88. }
  89. $this->metadataBag->stampNew($lifetime);
  90. $this->id = $this->generateId();
  91. return true;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function getId()
  97. {
  98. return $this->id;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function setId($id)
  104. {
  105. if ($this->started) {
  106. throw new \LogicException('Cannot set session ID after the session has started.');
  107. }
  108. $this->id = $id;
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function getName()
  114. {
  115. return $this->name;
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function setName($name)
  121. {
  122. $this->name = $name;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function save()
  128. {
  129. if (!$this->started || $this->closed) {
  130. throw new \RuntimeException('Trying to save a session that was not started yet or was already closed');
  131. }
  132. // nothing to do since we don't persist the session data
  133. $this->closed = false;
  134. $this->started = false;
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function clear()
  140. {
  141. // clear out the bags
  142. foreach ($this->bags as $bag) {
  143. $bag->clear();
  144. }
  145. // clear out the session
  146. $this->data = array();
  147. // reconnect the bags to the session
  148. $this->loadSession();
  149. }
  150. /**
  151. * {@inheritdoc}
  152. */
  153. public function registerBag(SessionBagInterface $bag)
  154. {
  155. $this->bags[$bag->getName()] = $bag;
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function getBag($name)
  161. {
  162. if (!isset($this->bags[$name])) {
  163. throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
  164. }
  165. if (!$this->started) {
  166. $this->start();
  167. }
  168. return $this->bags[$name];
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function isStarted()
  174. {
  175. return $this->started;
  176. }
  177. public function setMetadataBag(MetadataBag $bag = null)
  178. {
  179. if (null === $bag) {
  180. $bag = new MetadataBag();
  181. }
  182. $this->metadataBag = $bag;
  183. }
  184. /**
  185. * Gets the MetadataBag.
  186. *
  187. * @return MetadataBag
  188. */
  189. public function getMetadataBag()
  190. {
  191. return $this->metadataBag;
  192. }
  193. /**
  194. * Generates a session ID.
  195. *
  196. * This doesn't need to be particularly cryptographically secure since this is just
  197. * a mock.
  198. *
  199. * @return string
  200. */
  201. protected function generateId()
  202. {
  203. return hash('sha256', uniqid('ss_mock_', true));
  204. }
  205. protected function loadSession()
  206. {
  207. $bags = array_merge($this->bags, array($this->metadataBag));
  208. foreach ($bags as $bag) {
  209. $key = $bag->getStorageKey();
  210. $this->data[$key] = isset($this->data[$key]) ? $this->data[$key] : array();
  211. $bag->initialize($this->data[$key]);
  212. }
  213. $this->started = true;
  214. $this->closed = false;
  215. }
  216. }