-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IBX-570: Fixed respecting
ezplatform.session.*
parameters (#40)
* IBX-570: Fixed respecting `ezplatform.session.*` parameters * IBX-570: Implemented changes after code review * IBX-570: Fixed setting correct session handler
- Loading branch information
Showing
4 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/EzPlatformCoreBundle/bundle/DependencyInjection/Compiler/SessionConfigurationPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace EzSystems\EzPlatformCoreBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Configures session handler based on `ezplatform.session.handler_id` | ||
* and `ezplatform.session.save_path`. | ||
* | ||
* This ensures parameters have the highest priority and the configuration | ||
* will be respected with default framework.yaml file. | ||
* | ||
* @internal | ||
*/ | ||
final class SessionConfigurationPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$handlerId = $container->hasParameter('ezplatform.session.handler_id') | ||
? $container->getParameter('ezplatform.session.handler_id') | ||
: null; | ||
|
||
$savePath = $container->hasParameter('ezplatform.session.save_path') | ||
? $container->getParameter('ezplatform.session.save_path') | ||
: null; | ||
|
||
if (null !== $handlerId) { | ||
$usedEnvs = []; | ||
$container->resolveEnvPlaceholders($handlerId, null, $usedEnvs); | ||
|
||
// code below follows FrameworkExtension from symfony/framework-bundle | ||
if ($usedEnvs || preg_match('#^[a-z]++://#', $handlerId)) { | ||
$id = '.cache_connection.' . ContainerBuilder::hash($handlerId); | ||
|
||
$container->getDefinition('session.abstract_handler') | ||
->replaceArgument( | ||
0, | ||
$container->has($id) | ||
? new Reference($id) | ||
: $handlerId | ||
); | ||
|
||
$container->setAlias('session.handler', 'session.abstract_handler'); | ||
} else { | ||
$container->setAlias('session.handler', $handlerId); | ||
} | ||
|
||
$container | ||
->getDefinition('session.storage.native') | ||
->replaceArgument(1, new Reference('session.handler')); | ||
$container | ||
->getDefinition('session.storage.php_bridge') | ||
->replaceArgument(0, new Reference('session.handler')); | ||
} | ||
|
||
if (null !== $savePath) { | ||
$container->setParameter('session.save_path', $savePath); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...EzPlatformCoreBundle/bundle/DependencyInjection/Compiler/SessionConfigurationPassTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace EzSystems\Tests\EzPlatformCoreBundle\DependencyInjection\Compiler; | ||
|
||
use EzSystems\EzPlatformCoreBundle\DependencyInjection\Compiler\SessionConfigurationPass; | ||
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\AbstractSessionHandler; | ||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\SessionHandlerFactory; | ||
|
||
class SessionConfigurationPassTest extends AbstractCompilerPassTestCase | ||
{ | ||
protected function registerCompilerPass(ContainerBuilder $container): void | ||
{ | ||
$container->addCompilerPass(new SessionConfigurationPass()); | ||
} | ||
|
||
public function testCompile(): void | ||
{ | ||
$this->container->setDefinition( | ||
'session.storage.native', | ||
(new Definition())->setArguments([null, null, null]) | ||
); | ||
$this->container->setDefinition( | ||
'session.storage.php_bridge', | ||
(new Definition())->setArguments([null, null]) | ||
); | ||
$this->container->setParameter('ezplatform.session.handler_id', 'my_handler'); | ||
$this->container->setParameter('ezplatform.session.save_path', 'my_save_path'); | ||
|
||
$this->compile(); | ||
|
||
$this->assertContainerBuilderHasAlias('session.handler', 'my_handler'); | ||
$this->assertContainerBuilderHasParameter('session.save_path', 'my_save_path'); | ||
$this->assertContainerBuilderHasServiceDefinitionWithArgument( | ||
'session.storage.native', | ||
1, | ||
new Reference('session.handler') | ||
); | ||
$this->assertContainerBuilderHasServiceDefinitionWithArgument( | ||
'session.storage.php_bridge', | ||
0, | ||
new Reference('session.handler') | ||
); | ||
} | ||
|
||
public function testCompileWithDsn(): void | ||
{ | ||
$dsn = 'redis://instance.local:1234'; | ||
|
||
$definition = new Definition(AbstractSessionHandler::class); | ||
$definition->setFactory([SessionHandlerFactory::class, 'createHandler']); | ||
$definition->setArguments([$dsn]); | ||
|
||
$this->container->setDefinition('session.abstract_handler', $definition); | ||
$this->container->setParameter('ezplatform.session.handler_id', $dsn); | ||
$this->container->setDefinition( | ||
'session.storage.native', | ||
(new Definition())->setArguments([null, null, null]) | ||
); | ||
$this->container->setDefinition( | ||
'session.storage.php_bridge', | ||
(new Definition())->setArguments([null, null]) | ||
); | ||
|
||
$this->compile(); | ||
|
||
$this->assertContainerBuilderHasAlias('session.handler', 'session.abstract_handler'); | ||
} | ||
|
||
public function testCompileWithNullValues(): void | ||
{ | ||
$this->container->setParameter('ezplatform.session.handler_id', null); | ||
$this->container->setParameter('ezplatform.session.save_path', null); | ||
|
||
$this->compile(); | ||
|
||
$this->assertContainerBuilderNotHasService('session.handler'); | ||
self::assertNotTrue($this->container->hasParameter('session.save_path')); | ||
} | ||
} |