diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index 33b2f14e23f95..0d78cbdb1b6ee 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -655,6 +655,9 @@ public function hasSession() public function setSession(SessionInterface $session) { $this->session = $session; + if (!$this->hasPreviousSession()) { + $this->session->markAsEmpty(); + } } /** diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index b0b3ff3d0bff0..464bac1f673b9 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -46,6 +46,14 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable */ private $attributeName; + /** + * True if we have no previous session (No session cookie) + * and nothing has been written to the session + * + * @var bool + */ + private $emptySession = false; + /** * Constructor. * @@ -66,11 +74,24 @@ public function __construct(SessionStorageInterface $storage = null, AttributeBa $this->registerBag($flashes); } + /** + * {@inheritdoc} + */ + public function markAsEmpty() + { + if (!$this->emptySession && !$this->isStarted()) { + $this->emptySession = true; + } + } + + /** * {@inheritdoc} */ public function start() { + $this->emptySession = false; + return $this->storage->start(); } @@ -79,6 +100,8 @@ public function start() */ public function has($name) { + if ($this->emptySession) {return null;} + return $this->storage->getBag($this->attributeName)->has($name); } @@ -87,6 +110,8 @@ public function has($name) */ public function get($name, $default = null) { + if ($this->emptySession) {return $default;} + return $this->storage->getBag($this->attributeName)->get($name, $default); } @@ -95,6 +120,7 @@ public function get($name, $default = null) */ public function set($name, $value) { + $this->emptySession = false; $this->storage->getBag($this->attributeName)->set($name, $value); } @@ -103,6 +129,8 @@ public function set($name, $value) */ public function all() { + if ($this->emptySession) {return array();} + return $this->storage->getBag($this->attributeName)->all(); } @@ -111,6 +139,7 @@ public function all() */ public function replace(array $attributes) { + $this->emptySession = false; $this->storage->getBag($this->attributeName)->replace($attributes); } @@ -119,6 +148,8 @@ public function replace(array $attributes) */ public function remove($name) { + if ($this->emptySession) {return null;} + return $this->storage->getBag($this->attributeName)->remove($name); } @@ -127,6 +158,7 @@ public function remove($name) */ public function clear() { + if ($this->emptySession) {return null;} $this->storage->getBag($this->attributeName)->clear(); } @@ -145,6 +177,10 @@ public function isStarted() */ public function getIterator() { + if ($this->emptySession) { + return new \ArrayIterator(array()); + } + return new \ArrayIterator($this->storage->getBag($this->attributeName)->all()); } @@ -155,6 +191,8 @@ public function getIterator() */ public function count() { + if ($this->emptySession) {return 0;} + return count($this->storage->getBag($this->attributeName)->all()); } diff --git a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php index a94fad00d6f0f..22bd514422af6 100644 --- a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php @@ -20,6 +20,14 @@ */ interface SessionInterface { + /** + * Mark this session as empty if this session has not started. + * This method should be called when we have no previous session + * (no session cookie) and then the session will be internally + * marked as empty, if it has not started yet. + */ + public function markAsEmpty(); + /** * Starts the session storage. *