Session.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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;
  11. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  12. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
  13. use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
  14. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  15. use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
  16. use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
  17. // Help opcache.preload discover always-needed symbols
  18. class_exists(AttributeBag::class);
  19. class_exists(FlashBag::class);
  20. class_exists(SessionBagProxy::class);
  21. /**
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. * @author Drak <drak@zikula.org>
  24. */
  25. class Session implements SessionInterface, \IteratorAggregate, \Countable
  26. {
  27. protected $storage;
  28. private $flashName;
  29. private $attributeName;
  30. private $data = [];
  31. private $usageIndex = 0;
  32. private $usageReporter;
  33. public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null, callable $usageReporter = null)
  34. {
  35. $this->storage = $storage ?: new NativeSessionStorage();
  36. $this->usageReporter = $usageReporter;
  37. $attributes = $attributes ?: new AttributeBag();
  38. $this->attributeName = $attributes->getName();
  39. $this->registerBag($attributes);
  40. $flashes = $flashes ?: new FlashBag();
  41. $this->flashName = $flashes->getName();
  42. $this->registerBag($flashes);
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function start()
  48. {
  49. return $this->storage->start();
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function has(string $name)
  55. {
  56. return $this->getAttributeBag()->has($name);
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function get(string $name, $default = null)
  62. {
  63. return $this->getAttributeBag()->get($name, $default);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function set(string $name, $value)
  69. {
  70. $this->getAttributeBag()->set($name, $value);
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function all()
  76. {
  77. return $this->getAttributeBag()->all();
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function replace(array $attributes)
  83. {
  84. $this->getAttributeBag()->replace($attributes);
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function remove(string $name)
  90. {
  91. return $this->getAttributeBag()->remove($name);
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function clear()
  97. {
  98. $this->getAttributeBag()->clear();
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function isStarted()
  104. {
  105. return $this->storage->isStarted();
  106. }
  107. /**
  108. * Returns an iterator for attributes.
  109. *
  110. * @return \ArrayIterator An \ArrayIterator instance
  111. */
  112. public function getIterator()
  113. {
  114. return new \ArrayIterator($this->getAttributeBag()->all());
  115. }
  116. /**
  117. * Returns the number of attributes.
  118. *
  119. * @return int
  120. */
  121. public function count()
  122. {
  123. return \count($this->getAttributeBag()->all());
  124. }
  125. public function &getUsageIndex(): int
  126. {
  127. return $this->usageIndex;
  128. }
  129. /**
  130. * @internal
  131. */
  132. public function isEmpty(): bool
  133. {
  134. if ($this->isStarted()) {
  135. ++$this->usageIndex;
  136. if ($this->usageReporter && 0 <= $this->usageIndex) {
  137. ($this->usageReporter)();
  138. }
  139. }
  140. foreach ($this->data as &$data) {
  141. if (!empty($data)) {
  142. return false;
  143. }
  144. }
  145. return true;
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function invalidate(int $lifetime = null)
  151. {
  152. $this->storage->clear();
  153. return $this->migrate(true, $lifetime);
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public function migrate(bool $destroy = false, int $lifetime = null)
  159. {
  160. return $this->storage->regenerate($destroy, $lifetime);
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function save()
  166. {
  167. $this->storage->save();
  168. }
  169. /**
  170. * {@inheritdoc}
  171. */
  172. public function getId()
  173. {
  174. return $this->storage->getId();
  175. }
  176. /**
  177. * {@inheritdoc}
  178. */
  179. public function setId(string $id)
  180. {
  181. if ($this->storage->getId() !== $id) {
  182. $this->storage->setId($id);
  183. }
  184. }
  185. /**
  186. * {@inheritdoc}
  187. */
  188. public function getName()
  189. {
  190. return $this->storage->getName();
  191. }
  192. /**
  193. * {@inheritdoc}
  194. */
  195. public function setName(string $name)
  196. {
  197. $this->storage->setName($name);
  198. }
  199. /**
  200. * {@inheritdoc}
  201. */
  202. public function getMetadataBag()
  203. {
  204. ++$this->usageIndex;
  205. if ($this->usageReporter && 0 <= $this->usageIndex) {
  206. ($this->usageReporter)();
  207. }
  208. return $this->storage->getMetadataBag();
  209. }
  210. /**
  211. * {@inheritdoc}
  212. */
  213. public function registerBag(SessionBagInterface $bag)
  214. {
  215. $this->storage->registerBag(new SessionBagProxy($bag, $this->data, $this->usageIndex, $this->usageReporter));
  216. }
  217. /**
  218. * {@inheritdoc}
  219. */
  220. public function getBag(string $name)
  221. {
  222. $bag = $this->storage->getBag($name);
  223. return method_exists($bag, 'getBag') ? $bag->getBag() : $bag;
  224. }
  225. /**
  226. * Gets the flashbag interface.
  227. *
  228. * @return FlashBagInterface
  229. */
  230. public function getFlashBag()
  231. {
  232. return $this->getBag($this->flashName);
  233. }
  234. /**
  235. * Gets the attributebag interface.
  236. *
  237. * Note that this method was added to help with IDE autocompletion.
  238. */
  239. private function getAttributeBag(): AttributeBagInterface
  240. {
  241. return $this->getBag($this->attributeName);
  242. }
  243. }