Skip to content

Commit af0a140

Browse files
author
Drak
committed
[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.
1 parent 8fc2397 commit af0a140

File tree

10 files changed

+40
-3
lines changed

10 files changed

+40
-3
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ CHANGELOG
99
* added `TimedPhpEngine`
1010
* added `--clean` option the the `translation:update` command
1111
* added `http_method_override` option
12+
* Reintroduce `auto_start` session config flag which control to `SessionListener` to manually start sessions
13+
during framework request cycle.
14+
* Added session config option `on_demand_mode` to control session start on demand.
1215

1316
2.2.0
1417
-----

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ private function addSessionSection(ArrayNodeDefinition $rootNode)
184184
->info('session configuration')
185185
->canBeUnset()
186186
->children()
187+
->booleanNode('auto_start')->info('Flag for SessionListener to start session')
188+
->defaultValue(true)->end()
189+
->scalarNode('on_demand_mode')->info('Start session on demand (on access), 0 - off, 1 - on (strict), 2 - off (lax)')
190+
->defaultValue(1)->end()
191+
->scalarNode('mock_name')->defaultValue('MOCKSESSID')->end()
187192
->scalarNode('storage_id')->defaultValue('session.storage.native')->end()
188193
->scalarNode('handler_id')->defaultValue('session.handler.native_file')->end()
189194
->scalarNode('name')->end()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,14 @@ private function registerSessionConfiguration(array $config, ContainerBuilder $c
314314

315315
$container->setParameter('session.storage.options', $options);
316316

317+
// this controls the SessionListener to start session
318+
$container->setParameter('session.auto_start', $config['auto_start']);
319+
320+
// this controls the session start on demand feature
321+
$container->setParameter('session.storage.on_demand_mode', $config['on_demand_mode']);
322+
323+
$container->setParameter('session.storage.mock_name', $config['mock_name']);
324+
317325
// session handler (the internal callback registered with PHP session management)
318326
if (null == $config['handler_id']) {
319327
// Set the handler class to be null

src/Symfony/Bundle/FrameworkBundle/EventListener/SessionListener.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ class SessionListener implements EventSubscriberInterface
2929
* @var ContainerInterface
3030
*/
3131
private $container;
32+
private $autoStart;
3233

33-
public function __construct(ContainerInterface $container)
34+
public function __construct(ContainerInterface $container, $autoStart)
3435
{
3536
$this->container = $container;
37+
$this->autoStart = $autoStart;
3638
}
3739

3840
public function onKernelRequest(GetResponseEvent $event)
@@ -46,7 +48,11 @@ public function onKernelRequest(GetResponseEvent $event)
4648
return;
4749
}
4850

49-
$request->setSession($this->container->get('session'));
51+
$request->setSession($session = $this->container->get('session'));
52+
53+
if ($this->autoStart || $request->hasPreviousSession()) {
54+
$session->start();
55+
}
5056
}
5157

5258
public static function getSubscribedEvents()

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
</xsd:complexType>
7878

7979
<xsd:complexType name="session">
80+
<xsd:attribute name="auto-start" type="xsd:boolean" />
81+
<xsd:attribute name="on-demand-mode" type="xsd:string" />
8082
<xsd:attribute name="storage-id" type="xsd:string" />
8183
<xsd:attribute name="handler-id" type="xsd:string" />
8284
<xsd:attribute name="name" type="xsd:string" />

src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<parameter key="session.class">Symfony\Component\HttpFoundation\Session\Session</parameter>
99
<parameter key="session.flashbag.class">Symfony\Component\HttpFoundation\Session\Flash\FlashBag</parameter>
1010
<parameter key="session.attribute_bag.class">Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag</parameter>
11+
<parameter key="session.metadata_bag.class">Symfony\Component\HttpFoundation\Session\Storage\MetadataBag</parameter>
1112
<parameter key="session.storage.native.class">Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage</parameter>
1213
<parameter key="session.storage.mock_file.class">Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage</parameter>
1314
<parameter key="session.handler.native_file.class">Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler</parameter>
@@ -24,14 +25,20 @@
2425
<service id="session.storage.native" class="%session.storage.native.class%">
2526
<argument>%session.storage.options%</argument>
2627
<argument type="service" id="session.handler" />
28+
<argument type="service" id="session.metadata_bag" />
29+
<argument>%session.storage.on_demand_mode%</argument>
2730
</service>
2831

2932
<service id="session.flash_bag" class="%session.flashbag.class%" public="false" />
33+
<service id="session.metadata_bag" class="%session.metadata_bag.class%" public="false" />
3034

3135
<service id="session.attribute_bag" class="%session.attribute_bag.class%" public="false" />
3236

3337
<service id="session.storage.mock_file" class="%session.storage.mock_file.class%" public="false">
3438
<argument>%kernel.cache_dir%/sessions</argument>
39+
<argument>%session.storage.mock_name%</argument>
40+
<argument type="service" id="session.metadata_bag" />
41+
<argument>%session.storage.on_demand_mode%</argument>
3542
</service>
3643

3744
<service id="session.handler.native_file" class="%session.handler.native_file.class%" public="false">
@@ -41,6 +48,7 @@
4148
<service id="session_listener" class="%session_listener.class%">
4249
<tag name="kernel.event_subscriber" />
4350
<argument type="service" id="service_container" />
51+
<argument>%session.auto_start%</argument>
4452
</service>
4553

4654
<!-- for BC -->

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
'session' => array(
2525
'storage_id' => 'session.storage.native',
2626
'handler_id' => 'session.handler.native_file',
27+
'on_demand_mode' => 1,
2728
'name' => '_SYMFONY',
2829
'cookie_lifetime' => 86400,
2930
'cookie_path' => '/',

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<framework:esi enabled="true" />
1313
<framework:profiler only-exceptions="true" enabled="false" />
1414
<framework:router resource="%kernel.root_dir%/config/routing.xml" type="xml" />
15-
<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" />
15+
<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" />
1616
<framework:templating assets-version="SomeVersionScheme" cache="/path/to/cache" >
1717
<framework:loader>loader.foo</framework:loader>
1818
<framework:loader>loader.bar</framework:loader>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ framework:
1818
session:
1919
storage_id: session.storage.native
2020
handler_id: session.handler.native_file
21+
on_demand_mode: 1
2122
name: _SYMFONY
2223
cookie_lifetime: 86400
2324
cookie_path: /

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public function testSession()
9797
$this->assertEquals('session.storage.native', (string) $container->getAlias('session.storage'));
9898
$this->assertEquals('session.handler.native_file', (string) $container->getAlias('session.handler'));
9999

100+
$this->assertTrue($container->getParameter('session.auto_start'));
101+
$this->assertEquals($container->getParameter('session.storage.on_demand_mode'), 1);
102+
100103
$options = $container->getParameter('session.storage.options');
101104
$this->assertEquals('_SYMFONY', $options['name']);
102105
$this->assertEquals(86400, $options['cookie_lifetime']);

0 commit comments

Comments
 (0)