8000 Do not start session on get, if no prevoius session by TerjeBr · Pull Request #6152 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Do not start session on get, if no prevoius session #6152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,9 @@ public function hasSession()
public function setSession(SessionInterface $session)
{
$this->session = $session;
if (!$this->hasPreviousSession()) {
$this->session->markAsEmpty();
}
}

/**
Expand Down
38 changes: 38 additions & 0 deletions src/Symfony/Component/HttpFoundation/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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();
}

Expand All @@ -79,6 +100,8 @@ public function start()
*/
public function has($name)
{
if ($this->emptySession) {return null;}

return $this->storage->getBag($this->attributeName)->has($name);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -103,6 +129,8 @@ public function set($name, $value)
*/
public function all()
{
if ($this->emptySession) {return array();}

return $this->storage->getBag($this->attributeName)->all();
}

Expand All @@ -111,6 +139,7 @@ public function all()
*/
public function replace(array $attributes)
{
$this->emptySession = false;
$this->storage->getBag($this->attributeName)->replace($attributes);
}

Expand All @@ -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);
}

Expand All @@ -127,6 +158,7 @@ public function remove($name)
*/
public function clear()
{
if ($this->emptySession) {return null;}
$this->storage->getBag($this->attributeName)->clear();
}

Expand All @@ -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());
}

Expand All @@ -155,6 +191,8 @@ public function getIterator()
*/
public function count()
{
if ($this->emptySession) {return 0;}

return count($this->storage->getBag($this->attributeName)->all());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
0