[FrameworkBundle] Add configuration to allow control over session start on demand.
1. Gives user control over session start on demand mode. 2. Re-introduce flag to allow session listener to manually start session.
This commit is contained in:
parent
8fc2397760
commit
af0a14009f
@ -9,6 +9,9 @@ CHANGELOG
|
||||
* added `TimedPhpEngine`
|
||||
* added `--clean` option the the `translation:update` command
|
||||
* added `http_method_override` option
|
||||
* Reintroduce `auto_start` session config flag which control to `SessionListener` to manually start sessions
|
||||
during framework request cycle.
|
||||
* Added session config option `on_demand_mode` to control session start on demand.
|
||||
|
||||
2.2.0
|
||||
-----
|
||||
|
@ -184,6 +184,11 @@ class Configuration implements ConfigurationInterface
|
||||
->info('session configuration')
|
||||
->canBeUnset()
|
||||
->children()
|
||||
->booleanNode('auto_start')->info('Flag for SessionListener to start session')
|
||||
->defaultValue(true)->end()
|
||||
->scalarNode('on_demand_mode')->info('Start session on demand (on access), 0 - off, 1 - on (strict), 2 - off (lax)')
|
||||
->defaultValue(1)->end()
|
||||
->scalarNode('mock_name')->defaultValue('MOCKSESSID')->end()
|
||||
->scalarNode('storage_id')->defaultValue('session.storage.native')->end()
|
||||
->scalarNode('handler_id')->defaultValue('session.handler.native_file')->end()
|
||||
->scalarNode('name')->end()
|
||||
|
@ -314,6 +314,14 @@ class FrameworkExtension extends Extension
|
||||
|
||||
$container->setParameter('session.storage.options', $options);
|
||||
|
||||
// this controls the SessionListener to start session
|
||||
$container->setParameter('session.auto_start', $config['auto_start']);
|
||||
|
||||
// this controls the session start on demand feature
|
||||
$container->setParameter('session.storage.on_demand_mode', $config['on_demand_mode']);
|
||||
|
||||
$container->setParameter('session.storage.mock_name', $config['mock_name']);
|
||||
|
||||
// session handler (the internal callback registered with PHP session management)
|
||||
if (null == $config['handler_id']) {
|
||||
// Set the handler class to be null
|
||||
|
@ -29,10 +29,12 @@ class SessionListener implements EventSubscriberInterface
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
private $container;
|
||||
private $autoStart;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
public function __construct(ContainerInterface $container, $autoStart)
|
||||
{
|
||||
$this->container = $container;
|
||||
$this->autoStart = $autoStart;
|
||||
}
|
||||
|
||||
public function onKernelRequest(GetResponseEvent $event)
|
||||
@ -46,7 +48,11 @@ class SessionListener implements EventSubscriberInterface
|
||||
return;
|
||||
}
|
||||
|
||||
$request->setSession($this->container->get('session'));
|
||||
$request->setSession($session = $this->container->get('session'));
|
||||
|
||||
if ($this->autoStart || $request->hasPreviousSession()) {
|
||||
$session->start();
|
||||
}
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents()
|
||||
|
@ -77,6 +77,8 @@
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="session">
|
||||
<xsd:attribute name="auto-start" type="xsd:boolean" />
|
||||
<xsd:attribute name="on-demand-mode" type="xsd:string" />
|
||||
<xsd:attribute name="storage-id" type="xsd:string" />
|
||||
<xsd:attribute name="handler-id" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
|
@ -8,6 +8,7 @@
|
||||
<parameter key="session.class">Symfony\Component\HttpFoundation\Session\Session</parameter>
|
||||
<parameter key="session.flashbag.class">Symfony\Component\HttpFoundation\Session\Flash\FlashBag</parameter>
|
||||
<parameter key="session.attribute_bag.class">Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag</parameter>
|
||||
<parameter key="session.metadata_bag.class">Symfony\Component\HttpFoundation\Session\Storage\MetadataBag</parameter>
|
||||
<parameter key="session.storage.native.class">Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage</parameter>
|
||||
<parameter key="session.storage.mock_file.class">Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage</parameter>
|
||||
<parameter key="session.handler.native_file.class">Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler</parameter>
|
||||
@ -24,14 +25,20 @@
|
||||
<service id="session.storage.native" class="%session.storage.native.class%">
|
||||
<argument>%session.storage.options%</argument>
|
||||
<argument type="service" id="session.handler" />
|
||||
<argument type="service" id="session.metadata_bag" />
|
||||
<argument>%session.storage.on_demand_mode%</argument>
|
||||
</service>
|
||||
|
||||
<service id="session.flash_bag" class="%session.flashbag.class%" public="false" />
|
||||
<service id="session.metadata_bag" class="%session.metadata_bag.class%" public="false" />
|
||||
|
||||
<service id="session.attribute_bag" class="%session.attribute_bag.class%" public="false" />
|
||||
|
||||
<service id="session.storage.mock_file" class="%session.storage.mock_file.class%" public="false">
|
||||
<argument>%kernel.cache_dir%/sessions</argument>
|
||||
<argument>%session.storage.mock_name%</argument>
|
||||
<argument type="service" id="session.metadata_bag" />
|
||||
<argument>%session.storage.on_demand_mode%</argument>
|
||||
</service>
|
||||
|
||||
<service id="session.handler.native_file" class="%session.handler.native_file.class%" public="false">
|
||||
@ -41,6 +48,7 @@
|
||||
<service id="session_listener" class="%session_listener.class%">
|
||||
<tag name="kernel.event_subscriber" />
|
||||
<argument type="service" id="service_container" />
|
||||
<argument>%session.auto_start%</argument>
|
||||
</service>
|
||||
|
||||
<!-- for BC -->
|
||||
|
@ -24,6 +24,7 @@ $container->loadFromExtension('framework', array(
|
||||
'session' => array(
|
||||
'storage_id' => 'session.storage.native',
|
||||
'handler_id' => 'session.handler.native_file',
|
||||
'on_demand_mode' => 1,
|
||||
'name' => '_SYMFONY',
|
||||
'cookie_lifetime' => 86400,
|
||||
'cookie_path' => '/',
|
||||
|
@ -12,7 +12,7 @@
|
||||
<framework:esi enabled="true" />
|
||||
<framework:profiler only-exceptions="true" enabled="false" />
|
||||
<framework:router resource="%kernel.root_dir%/config/routing.xml" type="xml" />
|
||||
<framework:session gc-maxlifetime="90000" gc-probability="1" gc-divisor="108" storage-id="session.storage.native" handler-id="session.handler.native_file" name="_SYMFONY" cookie-lifetime="86400" cookie-path="/" cookie-domain="example.com" cookie-secure="true" cookie-httponly="true" save-path="/path/to/sessions" />
|
||||
<framework:session gc-maxlifetime="90000" gc-probability="1" gc-divisor="108" storage-id="session.storage.native" handler-id="session.handler.native_file" name="_SYMFONY" cookie-lifetime="86400" cookie-path="/" cookie-domain="example.com" cookie-secure="true" cookie-httponly="true" save-path="/path/to/sessions" auto-start="true" on-demand-mode="1" />
|
||||
<framework:templating assets-version="SomeVersionScheme" cache="/path/to/cache" >
|
||||
<framework:loader>loader.foo</framework:loader>
|
||||
<framework:loader>loader.bar</framework:loader>
|
||||
|
@ -18,6 +18,7 @@ framework:
|
||||
session:
|
||||
storage_id: session.storage.native
|
||||
handler_id: session.handler.native_file
|
||||
on_demand_mode: 1
|
||||
name: _SYMFONY
|
||||
cookie_lifetime: 86400
|
||||
cookie_path: /
|
||||
|
@ -97,6 +97,9 @@ abstract class FrameworkExtensionTest extends TestCase
|
||||
$this->assertEquals('session.storage.native', (string) $container->getAlias('session.storage'));
|
||||
$this->assertEquals('session.handler.native_file', (string) $container->getAlias('session.handler'));
|
||||
|
||||
$this->assertTrue($container->getParameter('session.auto_start'));
|
||||
$this->assertEquals($container->getParameter('session.storage.on_demand_mode'), 1);
|
||||
|
||||
$options = $container->getParameter('session.storage.options');
|
||||
$this->assertEquals('_SYMFONY', $options['name']);
|
||||
$this->assertEquals(86400, $options['cookie_lifetime']);
|
||||
|
Reference in New Issue
Block a user