8000 [Session] Do not start the session unless it is (or has been) written to by TerjeBr · Pull Request #6388 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Session] Do not start the session unless it is (or has been) written to #6388

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
< 8000 ;?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\Session\Attribute;

use Symfony\Component\HttpFoundation\Session\EmptyBag;
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
use Symfony\Component\HttpFoundation\Session\Storage\EmptyStorageInterface;

/**
* A wrapper class for an empty attribute bag
*
* @author Terje Bråten <terje@braten.be>
*/
class EmptyAttributeBag extends EmptyBag implements AttributeBagInterface, \IteratorAggregate, \Countable
{
/**
* {@inheritdoc}
*/
public function has($name)
{
if ($this->isEmpty) {
return false;
}

return $this->realBag->has($name);
}

/**
* {@inheritdoc}
*/
public function get($name, $default = null)
{
if ($this->isEmpty) {
return $default;
}

return $this->realBag->get($name, $default);
}

/**
* {@inheritdoc}
*/
public function set($name, $value)
{
$this->startSession();
$this->realBag->set($name, $value);
}

/**
* {@inheritdoc}
*/
public function all()
{
if ($this->isEmpty) {
return array();
}

return $this->realBag->all();
}

/**
* {@inheritdoc}
*/
public function replace(array $attributes)
{
$this->startSession();
$this->realBag->replace($attributes);
}

/**
* {@inheritdoc}
*/
public function remove($name)
{
if ($this->isEmpty) {
return null;
}

return $this->realBag->remove($name);
}

/**
* Returns an iterator for attributes.
*
* @return \ArrayIterator An \ArrayIterator instance
*/
public function getIterator()
{
return new \ArrayIterator($this->all());
}

/**
* Returns the number of attributes.
*
* @return int The number of attributes
*/
public function count()
{
if ($this->isEmpty) {
return 0;
}

return $this->realBag->count();
}
}
133 changes: 133 additions & 0 deletions src/Symfony/Component/HttpFoundation/Session/EmptyBag.php
8000
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?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\Session;

use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
use Symfony\Component\HttpFoundation\Session\Attribute\EmptyAttributeBag;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\Flash\EmptyFlashBag;
use Symfony\Component\HttpFoundation\Session\Storage\EmptyStorageInterface;

/**
* Abstract base class for Empty Session bags
*
* @author Terje Bråten <terje@braten.be>
*/
class EmptyBag implements SessionBagInterface
{
/**
* Flag if the bag is still empty
*
* @var boolean $isEmpty
*/
protected $isEmpty = true;

/**
* The empty storage containing this bag
*
* @var EmptyStorageInterface $storage
*/
protected $storage;

/**
* The session bag this empty bag is a proxy for
*
* @var SessionBagInterface $realBag
*/
protected $realBag;

/**
* Constructor.
*
* @param EmptyStorageInterface $storage
* @param SessionBagInterface $realBag
*/
public function __construct(EmptyStorageInterface $storage, SessionBagInterface $realBag)
{
$this->storage = $storage;
$this->realBag = $realBag;
}

/**
* Create a new empty bag
*
* @param EmptyStorageInterface $storage
* @param SessionBagInterface $realBag
*/
public static function create(EmptyStorageInterface $storage,
SessionBagInterface $realBag)
{
if ($realBag instanceof AttributeBagInterface) {
return new EmptyAttributeBag($storage, $realBag);
}
if ($realBag instanceof FlashBagInterface) {
return new EmptyFlashBag($storage, $realBag);
}

throw new \LogicException('Unknown bag interface');
}

/**
* {@inheritdoc}
*/
public function getName()
{
return $this->realBag->getName();
}

/**
* {@inheritdoc}
*/
public function initialize(array &$array)
{
$this->startSession();

return $this->realBag->initialize($array);
}

/**
* Start the session
* Something has been written to the bag,
* and the storage must be informed
*/
protected function startSession()
{
if ($this->isEmpty) {
$this->isEmpty = false;
$this->realBag = $this->storage->getRealBag($this->getName());
}
}

/**
* Gets the storage key for this bag.
*
* @return string
*/
public function getStorageKey()
{
return $this->realBag->getStorageKey();
}

/**
* Clears out data from bag.
*
* @return mixed Whatever data was contained.
*/
public function clear()
{
if ($this->isEmpty) {
return array();
}

return $this->realBag->clear();
}
}
147 changes: 147 additions & 0 deletions src/Symfony/Component/HttpFoundation/Session/Flash/EmptyFlashBag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?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\Session\Flash;

use Symfony\Component\HttpFoundation\Session\EmptyBag;
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
use Symfony\Component\HttpFoundation\Session\Storage\EmptyStorageInterface;

/**
* A wrapper class for an empty FlashBag flash message container.
*
* @author Terje Bråten <terje@braten.be>
*/
class EmptyFlashBag extends EmptyBag implements FlashBagInterface, \IteratorAggregate, \Countable
{
/**
* {@inheritdoc}
*/
public function add($type, $message)
{
$this->startSession();
$this->realBag->add($type, $message);
}

/**
* {@inheritdoc}
*/
public function peek($type, array $default = array())
{
if ($this->isEmpty) {
return $default;
}

return $this->realBag->peek($type, $default);
}

/**
* {@inheritdoc}
*/
public function peekAll()
{
if ($this->isEmpty) {
return array();
}

return $this->realBag->peekAll();
}

/**
* {@inheritdoc}
*/
public function get($type, array $default = array())
{
if ($this->isEmpty) {
return $default;
}

return $this->realBag->get($type, $default);
}

/**
* {@inheritdoc}
*/
public function all()
{
if ($this->isEmpty) {
return array();
}

return $this->realBag->all();
}

/**
* {@inheritdoc}
*/
public function set($type, $messages)
{
$this->startSession();
$this->realBag->set($type, $messages);
}

/**
* {@inheritdoc}
*/
public function setAll(array $messages)
{
$this->startSession();
$this->realBag->setAll($messages);
}

/**
* {@inheritdoc}
*/
public function has($type)
{
if ($this->isEmpty) {
return false;
}

return $this->realBag->has($type);
}

/**
* {@inheritdoc}
*/
public function keys()
{
if ($this->isEmpty) {
return array();
}

return $this->realBag->keys();
}

/**
* Returns an iterator for flashes.
*
* @return \ArrayIterator An \ArrayIterator instance
*/
public function getIterator()
{
return new \ArrayIterator($this->all());
}

/**
* Returns the number of flashes.
*
* @return int The number of flashes
*/
public function count()
{
if ($this->isEmpty) {
return 0;
}

return $this->realBag->count();
}
}
Loading
0