Skip to content

Commit a090129

Browse files
gnat42fabpot
authored andcommitted
[FrameworkBundle] Inform the user when save_path will be ignored
1 parent 2bf5da5 commit a090129

File tree

7 files changed

+47
-2
lines changed

7 files changed

+47
-2
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,12 @@ private function addSessionSection(ArrayNodeDefinition $rootNode)
470470
$rootNode
471471
->children()
472472
->arrayNode('session')
473+
->validate()
474+
->ifTrue(function ($v) {
475+
return empty($v['handler_id']) && !empty($v['save_path']);
476+
})
477+
->thenInvalid('Session save path is ignored without a handler service')
478+
->end()
473479
->info('session configuration')
474480
->canBeEnabled()
475481
->children()
@@ -498,7 +504,7 @@ private function addSessionSection(ArrayNodeDefinition $rootNode)
498504
->defaultTrue()
499505
->setDeprecated('The "%path%.%node%" option is enabled by default and deprecated since Symfony 3.4. It will be always enabled in 4.0.')
500506
->end()
501-
->scalarNode('save_path')->defaultValue('%kernel.cache_dir%/sessions')->end()
507+
->scalarNode('save_path')->end()
502508
->integerNode('metadata_update_threshold')
503509
->defaultValue('0')
504510
->info('seconds to wait between 2 session metadata updates')

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

+9
Original file line numberDiff line numberDiff line change
@@ -873,13 +873,22 @@ private function registerSessionConfiguration(array $config, ContainerBuilder $c
873873

874874
// session handler (the internal callback registered with PHP session management)
875875
if (null === $config['handler_id']) {
876+
// If the user set a save_path without using a non-default \SessionHandler, it will silently be ignored
877+
if (isset($config['save_path'])) {
878+
throw new LogicException('Session save path is ignored without a handler service');
879+
}
880+
876881
// Set the handler class to be null
877882
$container->getDefinition('session.storage.native')->replaceArgument(1, null);
878883
$container->getDefinition('session.storage.php_bridge')->replaceArgument(0, null);
879884
} else {
880885
$container->setAlias('session.handler', $config['handler_id'])->setPrivate(true);
881886
}
882887

888+
if (!isset($config['save_path'])) {
889+
$config['save_path'] = ini_get('session.save_path');
890+
}
891+
883892
$container->setParameter('session.save_path', $config['save_path']);
884893

885894
if (\PHP_VERSION_ID < 70000) {

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

-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ protected static function getBundleDefaultConfig()
378378
'handler_id' => 'session.handler.native_file',
379379
'cookie_httponly' => true,
380380
'gc_probability' => 1,
381-
'save_path' => '%kernel.cache_dir%/sessions',
382381
'metadata_update_threshold' => '0',
383382
'use_strict_mode' => true,
384383
],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', [
4+
'session' => [
5+
'handler_id' => null,
6+
'save_path' => '/some/path',
7+
],
8+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
9+
<framework:config>
10+
<framework:session handler-id="null" save-path="/some/path"/>
11+
</framework:config>
12+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
framework:
2+
session:
3+
handler_id: null
4+
save_path: /some/path

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

+7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
2525
use Symfony\Component\Cache\Adapter\ProxyAdapter;
2626
use Symfony\Component\Cache\Adapter\RedisAdapter;
27+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
2728
use Symfony\Component\DependencyInjection\ChildDefinition;
2829
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
2930
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -474,6 +475,12 @@ public function testNullSessionHandler()
474475
$this->assertNull($container->getDefinition('session.storage.php_bridge')->getArgument(0));
475476
}
476477

478+
public function testNullSessionHandlerWithSavePath()
479+
{
480+
$this->expectException(InvalidConfigurationException::class);
481+
$this->createContainerFromFile('session_savepath');
482+
}
483+
477484
public function testRequest()
478485
{
479486
$container = $this->createContainerFromFile('full');

0 commit comments

Comments
 (0)