123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\HttpFoundation;
- use Symfony\Component\HttpFoundation\SessionStorage\SessionStorageInterface;
- /**
- * Session.
- *
- * @author Fabien Potencier <fabien@symfony.com>
- */
- class Session implements \Serializable
- {
- protected $storage;
- protected $attributes;
- protected $oldFlashes;
- protected $started;
- protected $defaultLocale;
- /**
- * Constructor.
- *
- * @param SessionStorageInterface $storage A SessionStorageInterface instance
- * @param string $defaultLocale The default locale
- */
- public function __construct(SessionStorageInterface $storage, $defaultLocale = 'en')
- {
- $this->storage = $storage;
- $this->defaultLocale = $defaultLocale;
- $this->attributes = array('_flash' => array(), '_locale' => $this->getDefaultLocale());
- \Locale::setDefault($this->attributes['_locale']);
- $this->started = false;
- }
- /**
- * Starts the session storage.
- */
- public function start()
- {
- if (true === $this->started) {
- return;
- }
- $this->storage->start();
- $this->attributes = $this->storage->read('_symfony2');
- if (!isset($this->attributes['_flash'])) {
- $this->attributes['_flash'] = array();
- }
- if (!isset($this->attributes['_locale'])) {
- $this->attributes['_locale'] = $this->getDefaultLocale();
- }
- \Locale::setDefault($this->attributes['_locale']);
- // flag current flash messages to be removed at shutdown
- $this->oldFlashes = array_flip(array_keys($this->attributes['_flash']));
- $this->started = true;
- }
- /**
- * Checks if an attribute is defined.
- *
- * @param string $name The attribute name
- *
- * @return Boolean true if the attribute is defined, false otherwise
- */
- public function has($name)
- {
- return array_key_exists($name, $this->attributes);
- }
- /**
- * Returns an attribute.
- *
- * @param string $name The attribute name
- * @param mixed $default The default value
- *
- * @return mixed
- */
- public function get($name, $default = null)
- {
- return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
- }
- /**
- * Sets an attribute.
- *
- * @param string $name
- * @param mixed $value
- */
- public function set($name, $value)
- {
- if (false === $this->started) {
- $this->start();
- }
- if ('_locale' === $name) {
- $this->setLocale($value);
- } else {
- $this->attributes[$name] = $value;
- }
- }
- /**
- * Returns attributes.
- *
- * @return array Attributes
- */
- public function getAttributes()
- {
- return $this->attributes;
- }
- /**
- * Sets attributes.
- *
- * @param array $attributes Attributes
- */
- public function setAttributes(array $attributes)
- {
- if (false === $this->started) {
- $this->start();
- }
- if (isset($attributes['_locale'])) {
- $this->setLocale($attributes['_locale']);
- }
- $this->attributes = $attributes;
- }
- /**
- * Removes an attribute.
- *
- * @param string $name
- */
- public function remove($name)
- {
- if (false === $this->started) {
- $this->start();
- }
- if (array_key_exists($name, $this->attributes)) {
- unset($this->attributes[$name]);
- }
- }
- /**
- * Clears all attributes.
- */
- public function clear()
- {
- if (false === $this->started) {
- $this->start();
- }
- $this->attributes = array();
- }
- /**
- * Invalidates the current session.
- */
- public function invalidate()
- {
- $this->clear();
- $this->storage->regenerate();
- }
- /**
- * Migrates the current session to a new session id while maintaining all
- * session attributes.
- */
- public function migrate()
- {
- $this->storage->regenerate();
- }
- /**
- * Returns the session ID
- *
- * @return mixed The session ID
- */
- public function getId()
- {
- return $this->storage->getId();
- }
- /**
- * Returns the locale
- *
- * @return string
- */
- public function getLocale()
- {
- return $this->attributes['_locale'];
- }
- /**
- * Sets the locale.
- *
- * @param string $locale
- */
- public function setLocale($locale)
- {
- if (false === $this->started) {
- $this->start();
- }
- \Locale::setDefault($this->attributes['_locale'] = $locale);
- }
- public function getFlashes()
- {
- return isset($this->attributes['_flash']) ? $this->attributes['_flash'] : array();
- }
- public function setFlashes($values)
- {
- if (false === $this->started) {
- $this->start();
- }
- $this->attributes['_flash'] = $values;
- $this->oldFlashes = array();
- }
- public function getFlash($name, $default = null)
- {
- return array_key_exists($name, $this->attributes['_flash']) ? $this->attributes['_flash'][$name] : $default;
- }
- public function setFlash($name, $value)
- {
- if (false === $this->started) {
- $this->start();
- }
- $this->attributes['_flash'][$name] = $value;
- unset($this->oldFlashes[$name]);
- }
- public function hasFlash($name)
- {
- if (false === $this->started) {
- $this->start();
- }
- return array_key_exists($name, $this->attributes['_flash']);
- }
- public function removeFlash($name)
- {
- if (false === $this->started) {
- $this->start();
- }
- unset($this->attributes['_flash'][$name]);
- }
- public function clearFlashes()
- {
- if (false === $this->started) {
- $this->start();
- }
- $this->attributes['_flash'] = array();
- $this->oldFlashes = array();
- }
- public function save()
- {
- if (false === $this->started) {
- $this->start();
- }
- if (isset($this->attributes['_flash'])) {
- $this->attributes['_flash'] = array_diff_key($this->attributes['_flash'], $this->oldFlashes);
- }
- $this->storage->write('_symfony2', $this->attributes);
- }
- public function __destruct()
- {
- $this->save();
- }
- public function serialize()
- {
- return serialize(array($this->storage, $this->defaultLocale));
- }
- public function unserialize($serialized)
- {
- list($this->storage, $this->defaultLocale) = unserialize($serialized);
- $this->attributes = array();
- $this->started = false;
- }
- private function getDefaultLocale()
- {
- return $this->defaultLocale;
- }
- }
|