Skip to content

Commit 37c5915

Browse files
committed
Deprecate session.storage
1 parent c757845 commit 37c5915

File tree

54 files changed

+468
-39
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+468
-39
lines changed

UPGRADE-5.3.md

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Form
3131
FrameworkBundle
3232
---------------
3333

34+
* Deprecate the `session.storage` alias and `session.storage.*` services, use the `session.storage.factory` alias and `session.storage.factory.*` services instead
35+
* Deprecate the `framework.session.storage_id` configuration option, use the `framework.session.storage_factory_id` configuration option instead
3436
* Deprecate the `session` service and the `SessionInterface` alias, use the `\Symfony\Component\HttpFoundation\Request::getSession()` or the new `\Symfony\Component\HttpFoundation\RequestStack::getSession()` methods instead
3537

3638
HttpFoundation

UPGRADE-6.0.md

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ Form
6969
FrameworkBundle
7070
---------------
7171

72+
* Remove the `session.storage` alias and `session.storage.*` services, use the `session.storage.factory` alias and `session.storage.factory.*` services instead
73+
* Remove `framework.session.storage_id` configuration option, use the `framework.session.storage_factory_id` configuration option instead
7274
* Remove the `session` service and the `SessionInterface` alias, use the `\Symfony\Component\HttpFoundation\Request::getSession()` or the new `\Symfony\Component\HttpFoundation\RequestStack::getSession()` methods instead
7375
* `MicroKernelTrait::configureRoutes()` is now always called with a `RoutingConfigurator`
7476
* The "framework.router.utf8" configuration option defaults to `true`

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
5.3
55
---
66

7+
* Deprecate the `session.storage` alias and `session.storage.*` services, use the `session.storage.factory` alias and `session.storage.factory.*` services instead
8+
* Deprecate the `framework.session.storage_id` configuration option, use the `framework.session.storage_factory_id` configuration option instead
79
* Deprecate the `session` service and the `SessionInterface` alias, use the `Request::getSession()` or the new `RequestStack::getSession()` methods instead
810
* Added `AbstractController::renderForm()` to render a form and set the appropriate HTTP status code
911
* Added support for configuring PHP error level to log levels

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SessionPass.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class SessionPass implements CompilerPassInterface
2222
{
2323
public function process(ContainerBuilder $container)
2424
{
25-
if (!$container->has('session.storage')) {
25+
if (!$container->has('session.factory')) {
2626
return;
2727
}
2828

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

+7
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,15 @@ private function addSessionSection(ArrayNodeDefinition $rootNode)
600600
->arrayNode('session')
601601
->info('session configuration')
602602
->canBeEnabled()
603+
->beforeNormalization()
604+
->ifTrue(function ($v) {
605+
return \is_array($v) && isset($v['storage_id']) && isset($v['storage_factory_id']);
606+
})
607+
->thenInvalid('You cannot use both "storage_id" and "storage_factory_id" at the same time under "framework.session"')
608+
->end()
603609
->children()
604610
->scalarNode('storage_id')->defaultValue('session.storage.native')->end()
611+
->scalarNode('storage_factory_id')->defaultNull()->end()
605612
->scalarNode('handler_id')->defaultValue('session.handler.native_file')->end()
606613
->scalarNode('name')
607614
->validate()

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

+33-8
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
use Symfony\Component\HttpClient\RetryableHttpClient;
7171
use Symfony\Component\HttpClient\ScopingHttpClient;
7272
use Symfony\Component\HttpFoundation\Request;
73+
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
7374
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
7475
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
7576
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
@@ -1028,7 +1029,20 @@ private function registerSessionConfiguration(array $config, ContainerBuilder $c
10281029
$loader->load('session.php');
10291030

10301031
// session storage
1031-
$container->setAlias('session.storage', $config['storage_id']);
1032+
if (null === $config['storage_factory_id']) {
1033+
trigger_deprecation('symfony/framework-bundle', '5.3', 'Not setting the "framework.session.storage_factory_id" configuration option is deprecated, it will default to "session.storage.factory.native" and will replace the "framework.session.storage_id" configuration option in version 6.0.');
1034+
$container->setAlias('session.storage', $config['storage_id']);
1035+
$container->setAlias('session.storage.factory', 'session.storage.factory.service');
1036+
} else {
1037+
$container->setAlias('session.storage.factory', $config['storage_factory_id']);
1038+
1039+
$container->removeAlias(SessionStorageInterface::class);
1040+
$container->removeDefinition('session.storage.metadata_bag');
1041+
$container->removeDefinition('session.storage.native');
1042+
$container->removeDefinition('session.storage.php_bridge');
1043+
$container->removeAlias('session.storage.filesystem');
1044+
}
1045+
10321046
$options = ['cache_limiter' => '0'];
10331047
foreach (['name', 'cookie_lifetime', 'cookie_path', 'cookie_domain', 'cookie_secure', 'cookie_httponly', 'cookie_samesite', 'use_cookies', 'gc_maxlifetime', 'gc_probability', 'gc_divisor', 'sid_length', 'sid_bits_per_character'] as $key) {
10341048
if (isset($config[$key])) {
@@ -1037,20 +1051,31 @@ private function registerSessionConfiguration(array $config, ContainerBuilder $c
10371051
}
10381052

10391053
if ('auto' === ($options['cookie_secure'] ?? null)) {
1040-
$locator = $container->getDefinition('session_listener')->getArgument(0);
1041-
$locator->setValues($locator->getValues() + [
1042-
'session_storage' => new Reference('session.storage', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
1043-
'request_stack' => new Reference('request_stack'),
1044-
]);
1054+
if (null === $config['storage_factory_id']) {
1055+
$locator = $container->getDefinition('session_listener')->getArgument(0);
1056+
$locator->setValues($locator->getValues() + [
1057+
'session_storage' => new Reference('session.storage', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
1058+
'request_stack' => new Reference('request_stack'),
1059+
]);
1060+
} else {
1061+
$container->getDefinition('session.storage.factory.native')->replaceArgument(3, true);
1062+
$container->getDefinition('session.storage.factory.php_bridge')->replaceArgument(2, true);
1063+
}
10451064
}
10461065

10471066
$container->setParameter('session.storage.options', $options);
10481067

10491068
// session handler (the internal callback registered with PHP session management)
10501069
if (null === $config['handler_id']) {
10511070
// Set the handler class to be null
1052-
$container->getDefinition('session.storage.native')->replaceArgument(1, null);
1053-
$container->getDefinition('session.storage.php_bridge')->replaceArgument(0, null);
1071+
if ($container->hasDefinition('session.storage.native')) {
1072+
$container->getDefinition('session.storage.native')->replaceArgument(1, null);
1073+
$container->getDefinition('session.storage.php_bridge')->replaceArgument(0, null);
1074+
} else {
1075+
$container->getDefinition('session.storage.factory.native')->replaceArgument(1, null);
1076+
$container->getDefinition('session.storage.factory.php_bridge')->replaceArgument(0, null);
1077+
}
1078+
10541079
$container->setAlias('session.handler', 'session.handler.native_file');
10551080
} else {
10561081
$container->resolveEnvPlaceholders($config['handler_id'], null, $usedEnvs);

src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\Component\DependencyInjection\ContainerInterface;
1919
use Symfony\Component\HttpFoundation\Request;
2020
use Symfony\Component\HttpFoundation\Response;
21-
use Symfony\Component\HttpFoundation\Session\Session;
2221
use Symfony\Component\HttpKernel\HttpKernelBrowser;
2322
use Symfony\Component\HttpKernel\KernelInterface;
2423
use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile;
@@ -123,7 +122,7 @@ public function loginUser($user, string $firewallContext = 'main'): self
123122

124123
$token = new TestBrowserToken($user->getRoles(), $user);
125124
$token->setAuthenticated(true);
126-
$session = new Session($this->getContainer()->get('test.service_container')->get('session.storage'));
125+
$session = $this->getContainer()->get('test.service_container')->get('session.factory')->createSession();
127126
$session->set('_security_'.$firewallContext, serialize($token));
128127
$session->save();
129128

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

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107

108108
<xsd:complexType name="session">
109109
<xsd:attribute name="enabled" type="xsd:boolean" />
110+
<xsd:attribute name="storage-factory-id" type="xsd:string" />
110111
<xsd:attribute name="storage-id" type="xsd:string" />
111112
<xsd:attribute name="handler-id" type="xsd:string" />
112113
<xsd:attribute name="name" type="xsd:string" />

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

+53-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
1717
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
1818
use Symfony\Component\HttpFoundation\Session\Session;
19+
use Symfony\Component\HttpFoundation\Session\SessionFactory;
1920
use Symfony\Component\HttpFoundation\Session\SessionInterface;
2021
use Symfony\Component\HttpFoundation\Session\Storage\Handler\AbstractSessionHandler;
2122
use Symfony\Component\HttpFoundation\Session\Storage\Handler\IdentityMarshaller;
@@ -25,8 +26,12 @@
2526
use Symfony\Component\HttpFoundation\Session\Storage\Handler\StrictSessionHandler;
2627
use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;
2728
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
29+
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorageFactory;
2830
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
31+
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorageFactory;
2932
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
33+
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorageFactory;
34+
use Symfony\Component\HttpFoundation\Session\Storage\ServiceSessionFactory;
3035
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
3136
use Symfony\Component\HttpKernel\EventListener\SessionListener;
3237

@@ -35,37 +40,80 @@
3540

3641
$container->services()
3742
->set('.session.do-not-use', Session::class) // to be removed in 6.0
43+
->factory([service('session.factory'), 'createSession'])
44+
->set('session.factory', SessionFactory::class)
3845
->args([
39-
service('session.storage'),
40-
null, // AttributeBagInterface
41-
null, // FlashBagInterface
46+
service('request_stack'),
47+
service('session.storage.factory'),
4248
[service('session_listener'), 'onSessionUsage'],
4349
])
50+
51+
->set('session.storage.factory.native', NativeSessionStorageFactory::class)
52+
->args([
53+
param('session.storage.options'),
54+
service('session.handler'),
55+
inline_service(MetadataBag::class)
56+
->args([
57+
param('session.metadata.storage_key'),
58+
param('session.metadata.update_threshold'),
59+
]),
60+
false,
61+
])
62+
->set('session.storage.factory.php_bridge', PhpBridgeSessionStorageFactory::class)
63+
->args([
64+
service('session.handler'),
65+
inline_service(MetadataBag::class)
66+
->args([
67+
param('session.metadata.storage_key'),
68+
param('session.metadata.update_threshold'),
69+
]),
70+
false,
71+
])
72+
->set('session.storage.factory.mock_file', MockFileSessionStorageFactory::class)
73+
->args([
74+
param('kernel.cache_dir').'/sessions',
75+
'MOCKSESSID',
76+
inline_service(MetadataBag::class)
77+
->args([
78+
param('session.metadata.storage_key'),
79+
param('session.metadata.update_threshold'),
80+
]),
81+
])
82+
->set('session.storage.factory.service', ServiceSessionFactory::class)
83+
->args([
84+
service('session.storage'),
85+
])
86+
->deprecate('symfony/framework-bundle', '5.3', 'The "%service_id%" service is deprecated, use "session.storage.factory.native", "session.storage.factory.php_bridge" or "session.storage.factory.mock_file" instead.')
87+
4488
->set('.session.deprecated', SessionInterface::class) // to be removed in 6.0
4589
->factory([inline_service(DeprecatedSessionFactory::class)->args([service('request_stack')]), 'getSession'])
4690
->alias(SessionInterface::class, '.session.do-not-use')
4791
->deprecate('symfony/framework-bundle', '5.3', 'The "%alias_id%" alias is deprecated, use "$requestStack->getSession()" instead.')
4892
->alias(SessionStorageInterface::class, 'session.storage')
93+
->deprecate('symfony/framework-bundle', '5.3', 'The "%alias_id%" alias is deprecated, use "session.storage.factory" instead.')
4994
->alias(\SessionHandlerInterface::class, 'session.handler')
5095

5196
->set('session.storage.metadata_bag', MetadataBag::class)
5297
->args([
5398
param('session.metadata.storage_key'),
5499
param('session.metadata.update_threshold'),
55100
])
101+
->deprecate('symfony/framework-bundle', '5.3', 'The "%service_id%" service is deprecated, create your own "session.storage.factory" instead.')
56102

57103
->set('session.storage.native', NativeSessionStorage::class)
58104
->args([
59105
param('session.storage.options'),
60106
service('session.handler'),
61107
service('session.storage.metadata_bag'),
62108
])
109+
->deprecate('symfony/framework-bundle', '5.3', 'The "%service_id%" service is deprecated, use "session.storage.factory.native" instead.')
63110

64111
->set('session.storage.php_bridge', PhpBridgeSessionStorage::class)
65112
->args([
66113
service('session.handler'),
67114
service('session.storage.metadata_bag'),
68115
])
116+
->deprecate('symfony/framework-bundle', '5.3', 'The "%service_id%" service is deprecated, use "session.storage.factory.php_bridge" instead.')
69117

70118
->set('session.flash_bag', FlashBag::class)
71119
->factory([service('.session.do-not-use'), 'getFlashBag'])
@@ -83,6 +131,7 @@
83131
'MOCKSESSID',
84132
service('session.storage.metadata_bag'),
85133
])
134+
->deprecate('symfony/framework-bundle', '5.3', 'The "%service_id%" service is deprecated, use "session.storage.factory.mock_file" instead.')
86135

87136
->set('session.handler.native_file', StrictSessionHandler::class)
88137
->args([
@@ -108,6 +157,7 @@
108157

109158
// for BC
110159
->alias('session.storage.filesystem', 'session.storage.mock_file')
160+
->deprecate('symfony/framework-bundle', '5.3', 'The "%alias_id%" alias is deprecated, use "session.storage.factory.mock_file" instead.')
111161

112162
->set('session.marshaller', IdentityMarshaller::class)
113163

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SessionPassTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function testProcess()
2222
{
2323
$container = new ContainerBuilder();
2424
$container
25-
->register('session.storage'); // marker service
25+
->register('session.factory'); // marker service
2626
$container
2727
->register('.session.do-not-use');
2828

@@ -41,7 +41,7 @@ public function testProcessUserDefinedSession()
4141
];
4242
$container = new ContainerBuilder();
4343
$container
44-
->register('session.storage'); // marker service
44+
->register('session.factory'); // marker service
4545
$container
4646
->register('session')
4747
->setArguments($arguments);
@@ -70,7 +70,7 @@ public function testProcessUserDefinedAlias()
7070
];
7171
$container = new ContainerBuilder();
7272
$container
73-
->register('session.storage'); // marker service
73+
->register('session.factory'); // marker service
7474
$container
7575
->register('trueSession')
7676
->setArguments($arguments);

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

+1
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ protected static function getBundleDefaultConfig()
463463
'session' => [
464464
'enabled' => false,
465465
'storage_id' => 'session.storage.native',
466+
'storage_factory_id' => null,
466467
'handler_id' => 'session.handler.native_file',
467468
'cookie_httponly' => true,
468469
'cookie_samesite' => null,

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

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'legacy_error_messages' => false,
77
],
88
'session' => [
9+
'storage_factory_id' => 'session.storage.factory.native',
910
'handler_id' => null,
1011
],
1112
]);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
'utf8' => true,
2828
],
2929
'session' => [
30-
'storage_id' => 'session.storage.native',
30+
'storage_factory_id' => 'session.storage.factory.native',
3131
'handler_id' => 'session.handler.native_file',
3232
'name' => '_SYMFONY',
3333
'cookie_lifetime' => 86400,

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

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
$container->loadFromExtension('framework', [
44
'session' => [
5+
'storage_factory_id' => 'session.storage.factory.native',
56
'handler_id' => null,
67
],
78
]);

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

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
$container->loadFromExtension('framework', [
44
'session' => [
5+
'storage_factory_id' => 'session.storage.factory.native',
56
'handler_id' => null,
67
'cookie_secure' => 'auto',
78
],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
// To be removed in Symfony 6.0
4+
$container->loadFromExtension('framework', [
5+
'session' => [
6+
'handler_id' => null,
7+
'cookie_secure' => 'auto',
8+
],
9+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// To be removed in Symfony 6.0
4+
$container->loadFromExtension('framework', [
5+
'session' => [
6+
'handler_id' => null,
7+
],
8+
]);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<framework:config>
1010
<framework:csrf-protection />
1111
<framework:form legacy-error-messages="false" />
12-
<framework:session />
12+
<framework:session storage-factory-id="session.storage.factory.native" />
1313
</framework:config>
1414
</container>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<framework:config>
1010
<framework:csrf-protection field-name="_custom" />
11-
<framework:session />
11+
<framework:session storage-factory-id="session.storage.factory.native" />
1212
<framework:form legacy-error-messages="false" />
1313
</framework:config>
1414
</container>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<framework:config>
1010
<framework:csrf-protection field-name="_custom_form" />
1111
<framework:form legacy-error-messages="false" />
12-
<framework:session />
12+
<framework:session storage-factory-id="session.storage.factory.native" />
1313
</framework:config>
1414
</container>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<framework:ssi enabled="true" />
1616
<framework:profiler only-exceptions="true" enabled="false" />
1717
<framework:router resource="%kernel.project_dir%/config/routing.xml" type="xml" utf8="true" />
18-
<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="false" use-cookies="true" save-path="/path/to/sessions" sid-length="22" sid-bits-per-character="4" />
18+
<framework:session gc-maxlifetime="90000" gc-probability="1" gc-divisor="108" storage-factory-id="session.storage.factory.native" handler-id="session.handler.native_file" name="_SYMFONY" cookie-lifetime="86400" cookie-path="/" cookie-domain="example.com" cookie-secure="true" cookie-httponly="false" use-cookies="true" save-path="/path/to/sessions" sid-length="22" sid-bits-per-character="4" />
1919
<framework:request>
2020
<framework:format name="csv">
2121
<framework:mime-type>text/csv</framework:mime-type>

0 commit comments

Comments
 (0)