Skip to content

Commit 31c18d3

Browse files
committed
merged branch drak/start_on_demand (PR #7576)
This PR was merged into the master branch. Discussion ---------- [2.3][Session] Give greater control over how and when session starts | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | na | License | MIT | Doc PR | symfony/symfony-docs#2475 Refs #6036 Gives control over how start on demand works: allowing to turn it on or off and to allow bag access when session is off. Commits ------- f431cb0 Fix tests 1f521d8 Coding standards 2583c26 [HttpFoundation][FrameworkBundle] Keep save auto_start behaviour as in 2.2 and make component values consistent with FrameworkBundle's configuration options. ceaf69b [FrameworkBundle] Use more sophisticated validation and configuration. af0a140 [FrameworkBundle] Add configuration to allow control over session start on demand. 8fc2397 [HttpFoundation] Give control over how session start on demand.
2 parents 1099a67 + 14f29e7 commit 31c18d3

File tree

10 files changed

+48
-3
lines changed

10 files changed

+48
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ CHANGELOG
1010
* added `TimedPhpEngine`
1111
* added `--clean` option the the `translation:update` command
1212
* added `http_method_override` option
13+
* Reintroduce `auto_start` session config flag to instruct the `SessionListener` to manually start session
14+
* Added session config option `on_demand_mode` to control session start on demand.
1315

1416
2.2.0
1517
-----

DependencyInjection/Configuration.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ private function addSessionSection(ArrayNodeDefinition $rootNode)
184184
->info('session configuration')
185185
->canBeUnset()
186186
->children()
187+
->booleanNode('auto_start')
188+
->defaultFalse()
189+
->info('Flag for SessionListener to start session')
190+
->end()
191+
->enumNode('on_demand_mode')
192+
->values(array('off', 'on', 'off_lax'))
193+
->defaultValue('on')
194+
->info('Start session on demand: off, on, or off_lax')
195+
->end()
196+
->scalarNode('mock_name')->defaultValue('MOCKSESSID')->end()
187197
->scalarNode('storage_id')->defaultValue('session.storage.native')->end()
188198
->scalarNode('handler_id')->defaultValue('session.handler.native_file')->end()
189199
->scalarNode('name')->end()

DependencyInjection/FrameworkExtension.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Config\Resource\FileResource;
1919
use Symfony\Component\Config\Resource\DirectoryResource;
2020
use Symfony\Component\Finder\Finder;
21+
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
2122
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
2223
use Symfony\Component\Config\FileLocator;
2324

@@ -314,6 +315,14 @@ private function registerSessionConfiguration(array $config, ContainerBuilder $c
314315

315316
$container->setParameter('session.storage.options', $options);
316317

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

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 = false)
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()

Resources/config/schema/symfony-1.0.xsd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@
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" />
82+
<xsd:attribute name="mock-name" type="xsd:string" />
8083
<xsd:attribute name="storage-id" type="xsd:string" />
8184
<xsd:attribute name="handler-id" type="xsd:string" />
8285
<xsd:attribute name="name" type="xsd:string" />

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.php_bridge.class">Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage</parameter>
1314
<parameter key="session.storage.mock_file.class">Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage</parameter>
@@ -25,18 +26,24 @@
2526
<service id="session.storage.native" class="%session.storage.native.class%">
2627
<argument>%session.storage.options%</argument>
2728
<argument type="service" id="session.handler" />
29+
<argument type="service" id="session.metadata_bag" />
30+
<argument>%session.storage.on_demand_mode%</argument>
2831
</service>
2932

3033
<service id="session.storage.php_bridge" class="%session.storage.php_bridge.class%">
3134
<argument type="service" id="session.handler" />
3235
</service>
3336

3437
<service id="session.flash_bag" class="%session.flashbag.class%" public="false" />
38+
<service id="session.metadata_bag" class="%session.metadata_bag.class%" public="false" />
3539

3640
<service id="session.attribute_bag" class="%session.attribute_bag.class%" public="false" />
3741

3842
<service id="session.storage.mock_file" class="%session.storage.mock_file.class%" public="false">
3943
<argument>%kernel.cache_dir%/sessions</argument>
44+
<argument>%session.storage.mock_name%</argument>
45+
<argument type="service" id="session.metadata_bag" />
46+
<argument>%session.storage.on_demand_mode%</argument>
4047
</service>
4148

4249
<service id="session.handler.native_file" class="%session.handler.native_file.class%" public="false">
@@ -46,6 +53,7 @@
4653
<service id="session_listener" class="%session_listener.class%">
4754
<tag name="kernel.event_subscriber" />
4855
<argument type="service" id="service_container" />
56+
<argument>%session.auto_start%</argument>
4957
</service>
5058

5159
<!-- for BC -->

Tests/DependencyInjection/Fixtures/php/full.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
'session' => array(
2525
'storage_id' => 'session.storage.native',
2626
'handler_id' => 'session.handler.native_file',
27+
'auto_start' => true,
28+
'on_demand_mode' => 'on',
2729
'name' => '_SYMFONY',
2830
'cookie_lifetime' => 86400,
2931
'cookie_path' => '/',

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="on" />
1616
<framework:templating assets-version="SomeVersionScheme" cache="/path/to/cache" >
1717
<framework:loader>loader.foo</framework:loader>
1818
<framework:loader>loader.bar</framework:loader>

Tests/DependencyInjection/Fixtures/yml/full.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ framework:
1818
session:
1919
storage_id: session.storage.native
2020
handler_id: session.handler.native_file
21+
auto_start: true
22+
on_demand_mode: on
2123
name: _SYMFONY
2224
cookie_lifetime: 86400
2325
cookie_path: /

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'), 'on');
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)