Skip to content

Commit

Permalink
IBX-570: Fixed respecting ezplatform.session.* parameters (#40)
Browse files Browse the repository at this point in the history
* IBX-570: Fixed respecting `ezplatform.session.*` parameters

* IBX-570: Implemented changes after code review

* IBX-570: Fixed setting correct session handler
  • Loading branch information
webhdx authored Jun 25, 2021
1 parent be46837 commit c7e9017
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"require-dev": {
"ezsystems/ezplatform-code-style": "^1.0@dev",
"friendsofphp/php-cs-fixer": "^2.15",
"phpunit/phpunit": "^8.3.5"
"phpunit/phpunit": "^8.3.5",
"matthiasnoback/symfony-dependency-injection-test": "^4.2"
},
"scripts": {
"test": "phpunit -v -c phpunit.xml",
Expand Down
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);
}
}
}
7 changes: 7 additions & 0 deletions src/EzPlatformCoreBundle/bundle/EzPlatformCoreBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

namespace EzSystems\EzPlatformCoreBundle;

use EzSystems\EzPlatformCoreBundle\DependencyInjection\Compiler\SessionConfigurationPass;
use EzSystems\EzPlatformCoreBundle\DependencyInjection\EzPlatformCoreExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -23,4 +25,9 @@ public function getContainerExtension(): ExtensionInterface
{
return new EzPlatformCoreExtension();
}

public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new SessionConfigurationPass());
}
}
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'));
}
}

0 comments on commit c7e9017

Please sign in to comment.