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 all commits
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
34 changes: 33 additions & 1 deletion CHANGELOG-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
* [BC BREAK] assets_base_urls and base_urls merging strategy has changed
* changed the default profiler storage to use the filesystem instead of SQLite
* added support for placeholders in route defaults and requirements (replaced by the value set in the service container)
* [BC BREAK] changed `session.xml` service name `session.storage.native` to `session.storage.native_file`
* added new session storage drivers to session.xml: `session.storage.native_memcache`, `session.storage.native_memcached`,
`session.storage.native_sqlite`, `session.storage.null`, `session.storage.memcache`,
and `session.storage.memcached`. Added `session.storage.functional_test.file` service for functional session testing.
* removed `session.storage.filesystem` service.

### SecurityBundle

Expand Down Expand Up @@ -143,6 +148,33 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
* removed the ContentTypeMimeTypeGuesser class as it is deprecated and never used on PHP 5.3
* added ResponseHeaderBag::makeDisposition() (implements RFC 6266)
* made mimetype to extension conversion configurable
* [BC BREAK] Moved flash messages out of the `Session` class and into `FlashBagInterface`.
Flashes are now stored as a bucket of messages per `$type` so there can be multiple messages per type.
There are four interface constants for type, `FlashBagInterface::INFO`, `FlashBagInterface::NOTICE`,
`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: `setFlashes()`
`setFlash()`, `hasFlash()`, `removeFlash()`, and `clearFlashes()`.
* [BC BREAK] 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
`getAttributes()`, `getFlashes()`.
* Moved attribute storage to `AttributeBagInterface`.
* Added `AttributeBag` to replicate attributes storage behaviour from 2.0.x
* Added `NamespacedAttributeBag` for namespace session attributes.
* Session now implements `SessionInterface` making implementation customizable and portable.
* [BC BREAK] Removed `NativeSessionStorage` and replaced with `NativeFileSessionStorage`
* Added session storage drivers for PHP native Memcache, Memcached and SQLite session save handlers.
* Added session storage drivers for custom Memcache, Memcached and Null session save handlers.
* Removed `FilesystemSessionStorage`, use `FunctionalTestFileSessionStorage` for functional testing instead.

### HttpKernel

Expand All @@ -169,7 +201,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c

### Serializer

* [BC BREAK] convert the `item` XML tag to an array
* [BC BREAK] convert the `item` XML tag to an array

``` xml
<?xml version="1.0"?>
Expand Down
62 changes: 61 additions & 1 deletion UPGRADE-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ UPGRADE FROM 2.0 to 2.1
and/or share a common base configuration (i.e. ``config.yml``), merging
could yield a set of base URL's for multiple environments.

* moved management of the locale from the Session class to the Request class
* [HttpFoundation] - moved management of the locale from the Session class to the Request class

Configuring the default locale:

Expand Down Expand Up @@ -40,3 +40,63 @@ UPGRADE FROM 2.0 to 2.1

Before: $session->getLocale()
After: $request->getLocale()

* [HttpFoundation] Flash Messages. Moved to own bucket and returns and array based on type.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo


Before (PHP):

<?php if ($view['session']->hasFlash('notice')): ?>
<div class="flash-notice">
<?php echo $view['session']->getFlash('notice') ?>
</div>
<?php endif; ?>

After (PHP):

<?php foreach ($view['session']->flashGet(Symfony\Component\HttpFoundation\FlashBag::NOTICE) as $notice): ?>
<div class="flash-notice">
<?php echo $notice; ?>
</div>
<?php endforeach; ?>

.. note::

You can of course declare `<?php use Symfony\Component\HttpFoundation\FlashBag; ?>` at the beginning
of the PHP template so you can use the shortcut `FlashBag::NOTICE`.

Before (Twig):

{% if app.session.hasFlash('notice') %}
<div class="flash-notice">
{{ app.session.flash('notice') }}
</div>
{% endif %}

After (Twig):

{% for flashMessage in app.session.flashGet(constant(Symfony\Component\HttpFoundation\FlashBag::NOTICE)) %}
<div class="flash-notice">
{{ flashMessage }}
</div>
{% endforeach %}

* [HttpFoundation] Session object now requires two additional constructor arguments but will default to
sensible defaults for convenience. The methods, `setFlashes()`, `setFlash()`, `hasFlash()`,
`removeFlash()`, and `clearFlashes()` 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`
and no longer should implement `read()`, `write()`, `remove()` which were removed from the
`SessionStorageInterface`.

* [HttpFoundation] Any session storage drive that wants to use custom save handlers should
implement `Symfony\Component\HttpFoundation\SessionStorage\SessionSaveHandlerInterface`

* [FrameworkBundle] The service session.storage.native is now called `session.storage.native_file`

* [FrameworkBundle] The service `session.storage.filesystem` is deprecated and should be replaced
`session.storage.native_file`

50 changes: 23 additions & 27 deletions src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace Symfony\Bridge\Doctrine\HttpFoundation;

use Doctrine\DBAL\Platforms\MySqlPlatform;
use Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage;
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 @@ -12,39 +15,32 @@
* @author Fabien Potencier <[email protected]>
* @author Johannes M. Schmitt <[email protected]>
*/
class DbalSessionStorage extends NativeSessionStorage
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)
{
parent::__construct($options);

$this->con = $con;
$this->tableName = $tableName;
}

/**
* Starts the session.
*/
public function start()
{
if (self::$sessionStarted) {
return;
}

// use this object as the session handler
session_set_save_handler(
array($this, 'sessionOpen'),
array($this, 'sessionClose'),
array($this, 'sessionRead'),
array($this, 'sessionWrite'),
array($this, 'sessionDestroy'),
array($this, 'sessionGC')
);

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

/**
Expand Down Expand Up @@ -102,7 +98,7 @@ public function sessionDestroy($id)
*
* @throws \RuntimeException If any old sessions cannot be cleaned
*/
public function sessionGC($lifetime)
public function sessionGc($lifetime)
{
try {
$this->con->executeQuery("DELETE FROM {$this->tableName} WHERE sess_time < :time", array(
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ public function onKernelResponse(FilterResponseEvent $event)

if ($session = $event->getRequest()->getSession()) {
$session->save();
$session->close();

$params = session_get_cookie_params();

$event->getResponse()->headers->setCookie(new Cookie(session_name(), session_id(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly']));
}
}
Expand Down
73 changes: 68 additions & 5 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@

<parameters>
<parameter key="session.class">Symfony\Component\HttpFoundation\Session</parameter>
<parameter key="session.storage.native.class">Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage</parameter>
<parameter key="session.storage.filesystem.class">Symfony\Component\HttpFoundation\SessionStorage\FilesystemSessionStorage</parameter>
<parameter key="session.flashbag.class">Symfony\Component\HttpFoundation\FlashBag</parameter>
<parameter key="session.attribute_bag.class">Symfony\Component\HttpFoundation\AttributeBag</parameter>
<parameter key="session.storage.native_file.class">Symfony\Component\HttpFoundation\SessionStorage\NativeFileSessionStorage</parameter>
<parameter key="session.storage.null.class">Symfony\Component\HttpFoundation\SessionStorage\NullSessionStorage</parameter>
<parameter key="session.storage.native_memcache.class">Symfony\Component\HttpFoundation\SessionStorage\NativeMemcacheSessionStorage</parameter>
<parameter key="session.storage.native_memcached.class">Symfony\Component\HttpFoundation\SessionStorage\NativeMemcachedSessionStorage</parameter>
<parameter key="session.storage.native_sqlite.class">Symfony\Component\HttpFoundation\SessionStorage\NativeSqliteSessionStorage</parameter>
<parameter key="session.storage.memcache.class">Symfony\Component\HttpFoundation\SessionStorage\MemcacheSessionStorage</parameter>
<parameter key="session.storage.memcached.class">Symfony\Component\HttpFoundation\SessionStorage\MemcachedSessionStorage</parameter>
<parameter key="session.storage.functional_test.file.class">Symfony\Component\HttpFoundation\SessionStorage\FunctionalTestFileSessionStorage</parameter>
<parameter key="session.memcache.class">Memcache</parameter>
<parameter key="session.memcached.class">Memcached</parameter>

<parameter key="session_listener.class">Symfony\Bundle\FrameworkBundle\EventListener\SessionListener</parameter>
</parameters>

Expand All @@ -16,13 +27,65 @@
<argument type="service" id="session.storage" />
</service>

<service id="session.storage.native" class="%session.storage.native.class%" public="false">
<argument>%session.storage.options%</argument>
<service id="session.flash_bag" class="%session.flashbag.class%" public="false" />
<service id="session.attribute_bag" class="%session.attribute_bag.class%" public="false" />
<service id="session.memcache" class="%session.memcache.class%" public="false" />
<service id="session.memcached" class="%session.memcached.class%" public="false" />

<service id="session.storage.functional_test.file" class="%session.storage.functional_test.file.class%" public="false">
<argument>%kernel.cache_dir%/sessions</argument>
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>

<service id="session.storage.filesystem" class="%session.storage.filesystem.class%" public="false">
<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 @@ -13,6 +13,7 @@

use Symfony\Component\Templating\Helper\Helper;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\FlashBagInterface;

/**
* SessionHelper provides read-only access to the session attributes.
Expand Down Expand Up @@ -46,19 +47,19 @@ public function get($name, $default = null)
return $this->session->get($name, $default);
}

public function getFlash($name, $default = null)
public function getFlashes($type)
{
return $this->session->getFlash($name, $default);
return $this->session->getFlashes()->get($type);
}

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

public function hasFlash($name)
public function hasFlashes($type)
{
return $this->session->hasFlash($name);
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
Loading