Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpFoundation] Refactor session handling and flash messages #2714

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 43 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
a2a8024
[FrameworkBundle] Refactor tests.
Dec 4, 2011
f21cfda
[HttpFoundation] Added structured namespacing to session attributes.
Nov 2, 2011
d20a1a0
[HttpFoundation] Move flash messages out of Session class and change …
Nov 4, 2011
04c5a3a
[HttpFoundation] Introduce a SessionInterface to make session class m…
Nov 22, 2011
202bde1
[HttpFoundation] Refactored the FlashBag* changing the way old messag…
Nov 23, 2011
6e4ee80
[BC Break][HttpFoundation] Refactored session handling to be true to …
Nov 23, 2011
f8ed4d8
[HttpFoundation] Added some new Native*Storage drivers for SQLite and…
Nov 23, 2011
e970caa
[HttpFoundation] SessionID is passed by PHP.
Nov 23, 2011
5920b69
[HttpFoundation] Add MemcacheSessionStorage driver.
Nov 23, 2011
006df6b
[HttpFoundation] Add MemcachedSessionStorage driver.
Nov 23, 2011
fe5fb53
[HttpFoundation] Added prefix to storage keys and declared properties.
Nov 24, 2011
e415277
[HttpFoundation] Documentation.
Nov 24, 2011
c2625c2
[BC Break][FrameworkBundle] Fix unit tests in.
Nov 24, 2011
5b15138
[HttpFoundation] Cleaned up constants.
Nov 24, 2011
58bc557
[HttpFoundation] remove test, needs to be completely re-written.
Nov 24, 2011
fe7bc8d
[HttpFoundation] Refactor test.
Nov 24, 2011
13f59b8
[WebProfilerBundle] Removed hack to make flash messages persist for a…
Nov 24, 2011
3340eaf
[Security] Refactor session storage driver in test.
Nov 24, 2011
e624746
[HttpFoundation] Added NullSessionStorage
Nov 24, 2011
402c3bd
[FrameworkBundle] Update session configuration XML.
Nov 24, 2011
1083d32
[HttpFoundation] Added native memcached session storage driver.
Nov 24, 2011
07dba61
[HttpFoundation] Correct callback names.
Nov 24, 2011
37455f3
[HttpFoundation] Remove check for now to allow tests to pass.
Nov 24, 2011
51f06a7
[TwigBundle] Refactor test for session management.
Nov 24, 2011
efadac3
[HttpFoundation][FrameworkBundle][SecurityBundle] Make parameters con…
Nov 24, 2011
af52d9e
Updated changelog and upgrading documentation.
Nov 24, 2011
f66987a
[HttpFoundation] FlashBag docblocks and class constants.
Nov 25, 2011
d6f779c
[HttpFoundation][FrameworkBundle] FilsyststemSessionStorage drive is …
Nov 27, 2011
967eb54
Coding standards, docblocks.
Nov 28, 2011
597b400
Typo.
Nov 28, 2011
22cd77c
Simplified examples of how to show flash messages.
Dec 2, 2011
de9f6df
[HttpFoundation] Add simple flash-message API to SessionInterface.
Dec 2, 2011
27383ac
[HttpFoundation] Change attribute namespacing character.
Dec 3, 2011
6f3135f
[HttpFoundation] Fix docblock return value.
Dec 3, 2011
be6810c
[Bridge/HttpFoundation] Refactored DbalSessionStorage
Dec 4, 2011
aee6c8a
[HttpFoundation] Typo fix.
Dec 4, 2011
9b0e1df
[HttpFoundation][FrameworkBundle] Moved session attributes to it's ow…
Dec 8, 2011
8498d7e
[HttpFoundation][FrameworkBundle] Made configuration of session stora…
Dec 8, 2011
3cc1f7e
[HttpFoundation] Allow session.cache_limiter to be forced if really r…
Dec 9, 2011
ccb1696
[HttpFoundation] Fix sprintf() calls.
Dec 10, 2011
044dca4
Documentation, coding standards and docblocks.
Dec 10, 2011
e89a82c
[HttpFoundation][FrameworkBundle][TwigBundle][Bridge/Doctrine] Move b…
Dec 11, 2011
eee89d6
[HttpFoundation][FrameworkBundle][SecurityBundle] Introduced mock ses…
Dec 12, 2011
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
Prev Previous commit
Next Next commit
[HttpFoundation][FrameworkBundle][TwigBundle][Bridge/Doctrine] Move b…
…ag injection back to the storage constructors.

removed *bag from method names.
  • Loading branch information
Drak committed Dec 12, 2011
commit e89a82c1c639c20ac11ecbe987709a58bc13a50c
13 changes: 7 additions & 6 deletions CHANGELOG-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,19 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
`FlashBagInterface::WARNING` and `FlashBagInterface::ERROR`.
* Flash messages are expired when retrieved (with $clear = true) set. This makes the implementation
more flexible and removed some dependencies in the Session management cycle.
* [BC BREAK] Removed the following methods from the Session class: `getFlashes()`, `setFlashes()`
`getFlash()`, `setFlash()`, `hasFlash()`, `removeFlash()`, `clearFlashes()` and `save()`.
Added `flashGet($clear=false)` used to get flash messages for display, and `flashAdd($message, $type)`
to add flash messages. Flash messages are now stored in a separate `FlashBagInterface` for which there
is a method` getFlashBag()` which can be used for deeper manipulation of the flash message collection.
* [BC BREAK] Removed the following methods from the Session class: `setFlashes()`
`setFlash()`, `hasFlash()`, `removeFlash()`, `clearFlashes()` and `save()`.
Changed `getFlash($clear=false)` now returns flash messages for display, and added
`addFlash($message, $type)` to add flash messages.
`getFlashes()` now returns the `FlashBagInterface` for which there which can be used for deeper
manipulation of the flash message collection.
* `Session` object takes two additional object in the constructor: `AttributeBagInterface` and
`FlashBagInterface` after the `SessionStorageInterface`.
* Added `AbstractSessionStorage` base class for session storage drivers.
* Added `SessionSaveHandler` interface which storage drivers should implement after inheriting from
`AbstractSessionStorage` when writing custom session save handlers.
* [BC BREAK] `SessionStorageInterface` methods removed: `write()`, `read()` and `remove()`. Added
`getAttributeBag()`, `setAttributeBag()`, `getFlashBag()`, `setFlashBag()`.
`getAttributes()`, `getFlashes()`.
* Moved attribute storage to `AttributeBagInterface`.
* Added `AttributeBag` to replicate attributes storage behaviour from 2.0.x
* Added `NamespacedAttributeBag` for namespace session attributes.
Expand Down
10 changes: 5 additions & 5 deletions UPGRADE-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ UPGRADE FROM 2.0 to 2.1
{% endforeach %}

* [HttpFoundation] Session object now requires two additional constructor arguments but will default to
sensible defaults for convenience. The methods, `getFlashes()`, `setFlashes()`
getFlash()`, `setFlash()`, `hasFlash()`, `removeFlash()`, `clearFlashes()` and `save()`
have all been removed from the `Session` object. You may use `flashAdd()` to add flash
messages and `flashGet()` to retrieve for display. `getFlashBag()` if you need to deeply
manipulate the flash message container.
sensible defaults for convenience. The methods, `setFlashes()`, `setFlash()`, `hasFlash()`,
`removeFlash()`, `clearFlashes()` and `save()` have all been removed from the `Session` object.
You may use `addFlash()` to add flashes. `getFlash()` now returns an array for display.
`getFlashes()` returns the FlashBagInterface if you need to deeply manipulate the flash message
container.

* [HttpFoundation] Session storage drivers should inherit from
`Symfony\Component\HttpFoundation\SessionStorage\AbstractSessionStorage`
Expand Down
21 changes: 19 additions & 2 deletions src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Symfony\Bridge\Doctrine\HttpFoundation;

use Doctrine\DBAL\Platforms\MySqlPlatform;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
use Symfony\Component\HttpFoundation\FlashBagInterface;
use Symfony\Component\HttpFoundation\SessionStorage\AbstractSessionStorage;
use Symfony\Component\HttpFoundation\SessionStorage\SessionSaveHandlerInterface;
use Doctrine\DBAL\Driver\Connection;
Expand All @@ -15,15 +17,30 @@
*/
class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface
{
/**
* @var Connection
*/
private $con;

/**
* @var string
*/
private $tableName;

public function __construct(Connection $con, $tableName = 'sessions', array $options = array())
/**
*
* @param Connection $con An instance of Connection.
* @param string $tableName Table name.
* @param array $options Session configuration options
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
*/
public function __construct(Connection $con, $tableName = 'sessions', array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
{
$this->con = $con;
$this->tableName = $tableName;

parent::__construct($options);
parent::__construct($attributes, $flashes, $options);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private function addSessionSection(ArrayNodeDefinition $rootNode)
->canBeUnset()
->children()
->booleanNode('auto_start')->defaultFalse()->end()
->scalarNode('storage_id')->defaultValue('session.storage.native')->end()
->scalarNode('storage_id')->defaultValue('session.storage.native_file')->end()
->scalarNode('name')->end()
->scalarNode('lifetime')->end()
->scalarNode('path')->end()
Expand Down
16 changes: 14 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
<services>
<service id="session" class="%session.class%">
<argument type="service" id="session.storage" />
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>

<service id="session.flash_bag" class="%session.flashbag.class%" public="false" />
Expand All @@ -36,37 +34,51 @@
<service id="session.storage.native_file" class="%session.storage.native_file.class%" public="false">
<argument>%kernel.cache_dir%/sessions</argument>
<argument>%session.storage.options%</argument>
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>

<service id="session.storage.native_memcache" class="%session.storage.native_memcache.class%" public="false">
<argument>tcp://127.0.0.1:11211?persistent=0</argument>
<argument>%session.storage.options%</argument>
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>

<service id="session.storage.native_memcached" class="%session.storage.native_memcached.class%" public="false">
<argument>127.0.0.1:11211</argument>
<argument>%session.storage.options%</argument>
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>

<service id="session.storage.memcache" class="%session.storage.memcache.class%" public="false">
<argument type="service" id="session.memcache" />
<argument>tcp://127.0.0.1:11211?persistent=0</argument>
<argument>%session.storage.options%</argument>
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>

<service id="session.storage.memcached" class="%session.storage.memcached.class%" public="false">
<argument type="service" id="session.memcached" />
<argument>tcp://127.0.0.1:11211?persistent=0</argument>
<argument>%session.storage.options%</argument>
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>

<service id="session.storage.native_sqlite" class="%session.storage.native_sqlite.class%" public="false">
<argument>%kernel.cache_dir%/sf2_sqlite_sess.db</argument>
<argument>%session.storage.options%</argument>
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>

<service id="session.storage.null" class="%session.storage.null.class%" public="false">
<argument>%session.storage.options%</argument>
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>

<service id="session_listener" class="%session_listener.class%">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ public function get($name, $default = null)

public function getFlashes($type)
{
return $this->session->getFlashBag()->get($type);
return $this->session->getFlashes()->get($type);
}

public function getAllFlashes()
{
return $this->session->getFlashBag()->all();
return $this->session->getFlashes()->all();
}

public function hasFlashes($type)
{
return $this->session->getFlashBag()->has($type);
return $this->session->getFlashes()->has($type);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
),
'session' => array(
'auto_start' => true,
'storage_id' => 'session.storage.native',
'storage_id' => 'session.storage.native_file',
'name' => '_SYMFONY',
'lifetime' => 86400,
'path' => '/',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<framework:esi enabled="true" />
<framework:profiler only-exceptions="true" />
<framework:router resource="%kernel.root_dir%/config/routing.xml" type="xml" />
<framework:session auto-start="true" storage-id="session.storage.native" name="_SYMFONY" lifetime="86400" path="/" domain="example.com" secure="true" httponly="true" />
<framework:session auto-start="true" storage-id="session.storage.native_file" name="_SYMFONY" lifetime="86400" path="/" domain="example.com" secure="true" httponly="true" />
<framework:templating assets-version="SomeVersionScheme" cache="/path/to/cache" >
<framework:loader>loader.foo</framework:loader>
<framework:loader>loader.bar</framework:loader>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ framework:
type: xml
session:
auto_start: true
storage_id: session.storage.native
storage_id: session.storage.native_file
name: _SYMFONY
lifetime: 86400
path: /
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function testSession()
$this->assertTrue($container->hasDefinition('session'), '->registerSessionConfiguration() loads session.xml');
$this->assertEquals('fr', $container->getParameter('kernel.default_locale'));
$this->assertTrue($container->getDefinition('session_listener')->getArgument(1));
$this->assertEquals('session.storage.native', (string) $container->getAlias('session.storage'));
$this->assertEquals('session.storage.native_file', (string) $container->getAlias('session.storage'));

$options = $container->getParameter('session.storage.options');
$this->assertEquals('_SYMFONY', $options['name']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public function setUp()
{
$this->request = new Request();

$session = new Session(new ArraySessionStorage(), new AttributeBag(), new FlashBag());
$session = new Session(new ArraySessionStorage(new AttributeBag(), new FlashBag()));
$session->set('foobar', 'bar');
$session->getFlashBag()->add('bar', FlashBag::NOTICE);
$session->getFlashes()->add('bar', FlashBag::NOTICE);

$this->request->setSession($session);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function getContainer()
{
$container = new Container();
$request = new Request();
$session = new Session(new ArraySessionStorage(), new AttributeBag(), new FlashBag());
$session = new Session(new ArraySessionStorage(new AttributeBag(), new FlashBag()));

$request->setSession($session);
$container->set('request', $request);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function getContainer()
{
$container = new Container();
$request = new Request();
$session = new Session(new ArraySessionStorage(), new AttributeBag(), new FlashBag());
$session = new Session(new ArraySessionStorage(new AttributeBag(), new FlashBag()));

$request->setSession($session);
$container->set('request', $request);
Expand Down
37 changes: 16 additions & 21 deletions src/Symfony/Component/HttpFoundation/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Symfony\Component\HttpFoundation\SessionStorage\SessionStorageInterface;
use Symfony\Component\HttpFoundation\FlashBagInterface;
use Symfony\Component\HttpFoundation\AttributeBagInterface;

/**
* Session.
Expand All @@ -30,20 +29,16 @@ class Session implements SessionInterface
*
* @var SessionStorageInterface
*/
private $storage;
protected $storage;

/**
* Constructor.
*
* @param SessionStorageInterface $storage A SessionStorageInterface instance.
* @param AttributeBagInterface $attributeBag An AttributeBagInterface instance, null for default.
* @param FlashBagInterface $flashBag A FlashBagInterface instance, null for default.
* @param SessionStorageInterface $storage A SessionStorageInterface instance.
*/
public function __construct(SessionStorageInterface $storage, AttributeBagInterface $attributeBag = null, FlashBagInterface $flashBag = null)
public function __construct(SessionStorageInterface $storage)
{
$this->storage = $storage;
$this->storage->setAttributeBag($attributeBag ? $attributeBag : new AttributeBag);
$this->storage->setFlashBag($flashBag ? $flashBag : new FlashBag);
}

/**
Expand All @@ -67,7 +62,7 @@ public function start()
*/
public function has($name)
{
return $this->storage->getAttributeBag()->has($name);
return $this->storage->getAttributes()->has($name);
}

/**
Expand All @@ -82,7 +77,7 @@ public function has($name)
*/
public function get($name, $default = null)
{
return $this->storage->getAttributeBag()->get($name, $default);
return $this->storage->getAttributes()->get($name, $default);
}

/**
Expand All @@ -95,7 +90,7 @@ public function get($name, $default = null)
*/
public function set($name, $value)
{
$this->storage->getAttributeBag()->set($name, $value);
$this->storage->getAttributes()->set($name, $value);
}

/**
Expand All @@ -107,7 +102,7 @@ public function set($name, $value)
*/
public function all()
{
return $this->storage->getAttributeBag()->all();
return $this->storage->getAttributes()->all();
}

/**
Expand All @@ -119,7 +114,7 @@ public function all()
*/
public function replace(array $attributes)
{
$this->storage->getAttributeBag()->replace($attributes);
$this->storage->getAttributes()->replace($attributes);
}

/**
Expand All @@ -131,7 +126,7 @@ public function replace(array $attributes)
*/
public function remove($name)
{
return $this->storage->getAttributeBag()->remove($name);
return $this->storage->getAttributes()->remove($name);
}

/**
Expand All @@ -141,7 +136,7 @@ public function remove($name)
*/
public function clear()
{
$this->storage->getAttributeBag()->clear();
$this->storage->getAttributes()->clear();
}

/**
Expand Down Expand Up @@ -207,9 +202,9 @@ public function unserialize($serialized)
*
* @return FlashBagInterface
*/
public function getFlashBag()
public function getFlashes()
{
return $this->storage->getFlashBag();
return $this->storage->getFlashes();
}

/**
Expand All @@ -218,9 +213,9 @@ public function getFlashBag()
* @param string $message
* @param string $type
*/
public function flashAdd($message, $type = FlashBagInterface::NOTICE)
public function addFlash($message, $type = FlashBagInterface::NOTICE)
{
$this->storage->getFlashBag()->add($message, $type);
$this->storage->getFlashes()->add($message, $type);
}

/**
Expand All @@ -231,8 +226,8 @@ public function flashAdd($message, $type = FlashBagInterface::NOTICE)
*
* @return array
*/
public function flashGet($type, $clear = true)
public function getFlash($type, $clear = true)
{
return $this->storage->getFlashBag()->get($type, $clear);
return $this->storage->getFlashes()->get($type, $clear);
}
}
6 changes: 3 additions & 3 deletions src/Symfony/Component/HttpFoundation/SessionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ function migrate();
*
* @return FlashBagInterface
*/
function getFlashBag();
function getFlashes();

/**
* Adds a flash to the stack for a given type.
*
* @param string $message
* @param string $type
*/
function flashAdd($message, $type = FlashBagInterface::NOTICE);
function addFlash($message, $type = FlashBagInterface::NOTICE);

/**
* Gets flash messages for a given type.
Expand All @@ -66,5 +66,5 @@ function flashAdd($message, $type = FlashBagInterface::NOTICE);
*
* @return array
*/
function flashGet($type, $clear = true);
function getFlash($type, $clear = true);
}
Loading