[FrameworkBundle] Refactored code for changes to HttpFoundation component.
Native PHP sessions stored to file are done with session.storage.native_file Functional testing is done with session.storage.mock_file Default flash message implementation done with FlashBag (session.flash_bag) Default attribute storage implementation with AttributeBag (session.attribute_bag) Services added: session.storage.native_file, session.storage.native_memcache, session.storage.native_memcache, session.storage.native_sqlite, session.storage.memcache, session.storage.memcached, session.storage.null, session.storage.mock_file, session.flash_bag, session.attribute_bag Services removed: session.storage.native, session.storage.filesystem
This commit is contained in:
parent
669bc96c7f
commit
7aaf024b2a
@ -167,7 +167,7 @@ class Configuration implements ConfigurationInterface
|
|||||||
->canBeUnset()
|
->canBeUnset()
|
||||||
->children()
|
->children()
|
||||||
->booleanNode('auto_start')->defaultFalse()->end()
|
->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('name')->end()
|
||||||
->scalarNode('lifetime')->end()
|
->scalarNode('lifetime')->end()
|
||||||
->scalarNode('path')->end()
|
->scalarNode('path')->end()
|
||||||
|
@ -69,7 +69,6 @@ class TestSessionListener implements EventSubscriberInterface
|
|||||||
|
|
||||||
if ($session = $event->getRequest()->getSession()) {
|
if ($session = $event->getRequest()->getSession()) {
|
||||||
$session->save();
|
$session->save();
|
||||||
$session->close();
|
|
||||||
|
|
||||||
$params = session_get_cookie_params();
|
$params = session_get_cookie_params();
|
||||||
|
|
||||||
|
@ -6,8 +6,19 @@
|
|||||||
|
|
||||||
<parameters>
|
<parameters>
|
||||||
<parameter key="session.class">Symfony\Component\HttpFoundation\Session</parameter>
|
<parameter key="session.class">Symfony\Component\HttpFoundation\Session</parameter>
|
||||||
<parameter key="session.storage.native.class">Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage</parameter>
|
<parameter key="session.flashbag.class">Symfony\Component\HttpFoundation\FlashBag</parameter>
|
||||||
<parameter key="session.storage.filesystem.class">Symfony\Component\HttpFoundation\SessionStorage\FilesystemSessionStorage</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.mock_file.class">Symfony\Component\HttpFoundation\SessionStorage\MockFileSessionStorage</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>
|
<parameter key="session_listener.class">Symfony\Bundle\FrameworkBundle\EventListener\SessionListener</parameter>
|
||||||
</parameters>
|
</parameters>
|
||||||
|
|
||||||
@ -16,13 +27,66 @@
|
|||||||
<argument type="service" id="session.storage" />
|
<argument type="service" id="session.storage" />
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<service id="session.storage.native" class="%session.storage.native.class%" public="false">
|
<service id="session.flash_bag" class="%session.flashbag.class%" public="false" />
|
||||||
<argument>%session.storage.options%</argument>
|
<service id="session.attribute_bag" class="%session.attribute_bag.class%" public="false" />
|
||||||
</service>
|
<service id="session.memcache" class="%session.memcache.class%" public="false" />
|
||||||
|
<service id="session.memcached" class="%session.memcached.class%" public="false" />
|
||||||
|
|
||||||
<service id="session.storage.filesystem" class="%session.storage.filesystem.class%" public="false">
|
<service id="session.storage.mock_file" class="%session.storage.mock_file.class%" public="false">
|
||||||
<argument>%kernel.cache_dir%/sessions</argument>
|
<argument>%kernel.cache_dir%/sessions</argument>
|
||||||
<argument>%session.storage.options%</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_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>
|
||||||
|
|
||||||
<service id="session_listener" class="%session_listener.class%">
|
<service id="session_listener" class="%session_listener.class%">
|
||||||
|
@ -13,6 +13,7 @@ namespace Symfony\Bundle\FrameworkBundle\Templating\Helper;
|
|||||||
|
|
||||||
use Symfony\Component\Templating\Helper\Helper;
|
use Symfony\Component\Templating\Helper\Helper;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\FlashBagInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SessionHelper provides read-only access to the session attributes.
|
* SessionHelper provides read-only access to the session attributes.
|
||||||
@ -46,19 +47,19 @@ class SessionHelper extends Helper
|
|||||||
return $this->session->get($name, $default);
|
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($type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFlashes()
|
public function getAllFlashes()
|
||||||
{
|
{
|
||||||
return $this->session->getFlashes();
|
return $this->session->getAllFlashes();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasFlash($name)
|
public function hasFlashes($type)
|
||||||
{
|
{
|
||||||
return $this->session->hasFlash($name);
|
return $this->session->hasFlashes($type);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -20,7 +20,7 @@ $container->loadFromExtension('framework', array(
|
|||||||
),
|
),
|
||||||
'session' => array(
|
'session' => array(
|
||||||
'auto_start' => true,
|
'auto_start' => true,
|
||||||
'storage_id' => 'session.storage.native',
|
'storage_id' => 'session.storage.native_file',
|
||||||
'name' => '_SYMFONY',
|
'name' => '_SYMFONY',
|
||||||
'lifetime' => 86400,
|
'lifetime' => 86400,
|
||||||
'path' => '/',
|
'path' => '/',
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
<framework:esi enabled="true" />
|
<framework:esi enabled="true" />
|
||||||
<framework:profiler only-exceptions="true" />
|
<framework:profiler only-exceptions="true" />
|
||||||
<framework:router resource="%kernel.root_dir%/config/routing.xml" type="xml" />
|
<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:templating assets-version="SomeVersionScheme" cache="/path/to/cache" >
|
||||||
<framework:loader>loader.foo</framework:loader>
|
<framework:loader>loader.foo</framework:loader>
|
||||||
<framework:loader>loader.bar</framework:loader>
|
<framework:loader>loader.bar</framework:loader>
|
||||||
|
@ -14,7 +14,7 @@ framework:
|
|||||||
type: xml
|
type: xml
|
||||||
session:
|
session:
|
||||||
auto_start: true
|
auto_start: true
|
||||||
storage_id: session.storage.native
|
storage_id: session.storage.native_file
|
||||||
name: _SYMFONY
|
name: _SYMFONY
|
||||||
lifetime: 86400
|
lifetime: 86400
|
||||||
path: /
|
path: /
|
||||||
|
@ -78,7 +78,7 @@ abstract class FrameworkExtensionTest extends TestCase
|
|||||||
$this->assertTrue($container->hasDefinition('session'), '->registerSessionConfiguration() loads session.xml');
|
$this->assertTrue($container->hasDefinition('session'), '->registerSessionConfiguration() loads session.xml');
|
||||||
$this->assertEquals('fr', $container->getParameter('kernel.default_locale'));
|
$this->assertEquals('fr', $container->getParameter('kernel.default_locale'));
|
||||||
$this->assertTrue($container->getDefinition('session_listener')->getArgument(1));
|
$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');
|
$options = $container->getParameter('session.storage.options');
|
||||||
$this->assertEquals('_SYMFONY', $options['name']);
|
$this->assertEquals('_SYMFONY', $options['name']);
|
||||||
|
@ -51,7 +51,7 @@ class SessionController extends ContainerAware
|
|||||||
{
|
{
|
||||||
$request = $this->container->get('request');
|
$request = $this->container->get('request');
|
||||||
$session = $request->getSession();
|
$session = $request->getSession();
|
||||||
$session->setFlash('notice', $message);
|
$session->addFlash($message, 'notice');
|
||||||
|
|
||||||
return new RedirectResponse($this->container->get('router')->generate('session_showflash'));
|
return new RedirectResponse($this->container->get('router')->generate('session_showflash'));
|
||||||
}
|
}
|
||||||
@ -61,8 +61,8 @@ class SessionController extends ContainerAware
|
|||||||
$request = $this->container->get('request');
|
$request = $this->container->get('request');
|
||||||
$session = $request->getSession();
|
$session = $request->getSession();
|
||||||
|
|
||||||
if ($session->hasFlash('notice')) {
|
if ($session->hasFlashes('notice')) {
|
||||||
$output = $session->getFlash('notice');
|
list($output) = $session->popFlashes('notice');
|
||||||
} else {
|
} else {
|
||||||
$output = 'No flash was set.';
|
$output = 'No flash was set.';
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ framework:
|
|||||||
default_locale: en
|
default_locale: en
|
||||||
session:
|
session:
|
||||||
auto_start: true
|
auto_start: true
|
||||||
storage_id: session.storage.filesystem
|
storage_id: session.storage.mock_file
|
||||||
|
|
||||||
services:
|
services:
|
||||||
logger: { class: Symfony\Component\HttpKernel\Log\NullLogger }
|
logger: { class: Symfony\Component\HttpKernel\Log\NullLogger }
|
||||||
|
@ -15,6 +15,8 @@ use Symfony\Component\HttpFoundation\Request;
|
|||||||
use Symfony\Component\HttpFoundation\Session;
|
use Symfony\Component\HttpFoundation\Session;
|
||||||
use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
|
use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
|
||||||
use Symfony\Bundle\FrameworkBundle\Templating\Helper\SessionHelper;
|
use Symfony\Bundle\FrameworkBundle\Templating\Helper\SessionHelper;
|
||||||
|
use Symfony\Component\HttpFoundation\FlashBag;
|
||||||
|
use Symfony\Component\HttpFoundation\AttributeBag;
|
||||||
|
|
||||||
class SessionHelperTest extends \PHPUnit_Framework_TestCase
|
class SessionHelperTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
@ -24,9 +26,9 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$this->request = new Request();
|
$this->request = new Request();
|
||||||
|
|
||||||
$session = new Session(new ArraySessionStorage());
|
$session = new Session(new ArraySessionStorage(new AttributeBag(), new FlashBag()));
|
||||||
$session->set('foobar', 'bar');
|
$session->set('foobar', 'bar');
|
||||||
$session->setFlash('foo', 'bar');
|
$session->addFlash('bar', FlashBag::NOTICE);
|
||||||
|
|
||||||
$this->request->setSession($session);
|
$this->request->setSession($session);
|
||||||
}
|
}
|
||||||
@ -40,14 +42,11 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$helper = new SessionHelper($this->request);
|
$helper = new SessionHelper($this->request);
|
||||||
|
|
||||||
$this->assertTrue($helper->hasFlash('foo'));
|
$this->assertTrue($helper->hasFlashes(FlashBag::NOTICE));
|
||||||
|
|
||||||
$this->assertEquals('bar', $helper->getFlash('foo'));
|
$this->assertEquals(array('bar'), $helper->getFlashes(FlashBag::NOTICE));
|
||||||
$this->assertEquals('foo', $helper->getFlash('bar', 'foo'));
|
|
||||||
|
|
||||||
$this->assertNull($helper->getFlash('foobar'));
|
$this->assertEquals(array(FlashBag::NOTICE => array('bar')), $helper->getAllFlashes());
|
||||||
|
|
||||||
$this->assertEquals(array('foo' => 'bar'), $helper->getFlashes());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGet()
|
public function testGet()
|
||||||
|
@ -19,6 +19,8 @@ use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
|
|||||||
use Symfony\Component\Templating\TemplateNameParser;
|
use Symfony\Component\Templating\TemplateNameParser;
|
||||||
use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables;
|
use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables;
|
||||||
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
|
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
|
||||||
|
use Symfony\Component\HttpFoundation\FlashBag;
|
||||||
|
use Symfony\Component\HttpFoundation\AttributeBag;
|
||||||
|
|
||||||
class PhpEngineTest extends TestCase
|
class PhpEngineTest extends TestCase
|
||||||
{
|
{
|
||||||
@ -64,7 +66,7 @@ class PhpEngineTest extends TestCase
|
|||||||
{
|
{
|
||||||
$container = new Container();
|
$container = new Container();
|
||||||
$request = new Request();
|
$request = new Request();
|
||||||
$session = new Session(new ArraySessionStorage());
|
$session = new Session(new ArraySessionStorage(new AttributeBag(), new FlashBag()));
|
||||||
|
|
||||||
$request->setSession($session);
|
$request->setSession($session);
|
||||||
$container->set('request', $request);
|
$container->set('request', $request);
|
||||||
|
Reference in New Issue
Block a user