Skip to content

[FrameworkBundle] Add ability to use existing service as lock/semaphore resource #58249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ CHANGELOG
* Enable `json_decode_detailed_errors` in the default serializer context in debug mode by default when `seld/jsonlint` is installed
* Register `Symfony\Component\Serializer\NameConverter\SnakeCaseToCamelCaseNameConverter` as a service named `serializer.name_converter.snake_case_to_camel_case` if available
* Deprecate `session.sid_length` and `session.sid_bits_per_character` config options
* Add the ability to use an existing service as a lock/semaphore resource

7.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ private function registerSessionConfiguration(array $config, ContainerBuilder $c
}
$container->resolveEnvPlaceholders($config['handler_id'], null, $usedEnvs);

if ($usedEnvs || preg_match('#^[a-z]++://#', $config['handler_id'])) {
if ($usedEnvs || str_contains($config['handler_id'], '://')) {
$id = '.cache_connection.'.ContainerBuilder::hash($config['handler_id']);

$container->getDefinition('session.abstract_handler')
Expand Down Expand Up @@ -2004,6 +2004,9 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont
$storeDefinitions = [];
foreach ($resourceStores as $resourceStore) {
$storeDsn = $container->resolveEnvPlaceholders($resourceStore, null, $usedEnvs);
if (!$usedEnvs && !str_contains($resourceStore, '://')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks BC in some use cases, like doctrine-bundle tests revealed https://github.com/doctrine/DoctrineBundle/actions/runs/10976729897/job/30477816481

$resourceStore = new Reference($resourceStore);
}
Copy link
Contributor Author

@HypeMC HypeMC Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
$container->resolveEnvPlaceholders($config['handler_id'], null, $usedEnvs);
if ($usedEnvs || preg_match('#^[a-z]++://#', $config['handler_id'])) {
$id = '.cache_connection.'.ContainerBuilder::hash($config['handler_id']);

$storeDefinition = new Definition(PersistingStoreInterface::class);
$storeDefinition
->setFactory([StoreFactory::class, 'createStore'])
Expand Down Expand Up @@ -2045,6 +2048,9 @@ private function registerSemaphoreConfiguration(array $config, ContainerBuilder

foreach ($config['resources'] as $resourceName => $resourceStore) {
$storeDsn = $container->resolveEnvPlaceholders($resourceStore, null, $usedEnvs);
if (!$usedEnvs && !str_contains($resourceStore, '://')) {
$resourceStore = new Reference($resourceStore);
}
$storeDefinition = new Definition(SemaphoreStoreInterface::class);
$storeDefinition->setFactory([SemaphoreStoreFactory::class, 'createStore']);
$storeDefinition->setArguments([$resourceStore]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$container->register('my_service', \Redis::class);
$container->setAlias('factory_public_alias', 'lock.default.factory')
->setPublic(true);

$container->loadFromExtension('framework', [
'annotations' => false,
'http_method_override' => false,
'handle_all_throwables' => true,
'php_errors' => ['log' => true],
'lock' => 'my_service',
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$container->register('my_service', \Redis::class);
$container->setAlias('factory_public_alias', 'semaphore.default.factory')
->setPublic(true);

$container->loadFromExtension('framework', [
'annotations' => false,
'http_method_override' => false,
'handle_all_throwables' => true,
'php_errors' => ['log' => true],
'semaphore' => 'my_service',
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<services>
<service id="my_service" class="\Redis" />
<service id="factory_public_alias" alias="lock.default.factory" public="true" />
</services>

<framework:config http-method-override="false" handle-all-throwables="true">
<framework:annotations enabled="false" />
<framework:php-errors log="true" />
<framework:lock>
<framework:resource>my_service</framework:resource>
</framework:lock>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<services>
<service id="my_service" class="\Redis" />
<service id="factory_public_alias" alias="semaphore.default.factory" public="true" />
</services>

<framework:config http-method-override="false" handle-all-throwables="true">
<framework:annotations enabled="false" />
<framework:php-errors log="true" />
<framework:semaphore>
<framework:resource>my_service</framework:resource>
</framework:semaphore>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
my_service:
class: \Redis
factory_public_alias:
alias: lock.default.factory
public: true

framework:
annotations: false
http_method_override: false
handle_all_throwables: true
php_errors:
log: true
lock: my_service
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
my_service:
class: \Redis
factory_public_alias:
alias: semaphore.default.factory
public: true

framework:
annotations: false
http_method_override: false
handle_all_throwables: true
php_errors:
log: true
semaphore: my_service
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveTaggedIteratorArgumentPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -57,6 +58,7 @@
use Symfony\Component\HttpFoundation\IpUtils;
use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
use Symfony\Component\HttpKernel\Fragment\FragmentUriGeneratorInterface;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\Store\SemaphoreStore;
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransportFactory;
use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpTransportFactory;
Expand All @@ -67,6 +69,7 @@
use Symfony\Component\Notifier\TexterInterface;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\Security\Core\AuthenticationEvents;
use Symfony\Component\Semaphore\SemaphoreFactory;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
Expand Down Expand Up @@ -2433,6 +2436,19 @@ public function testNamedLocks()
self::assertStringContainsString('REDIS_DSN', $storeDef->getArgument(0));
}

public function testLockWithService()
{
$container = $this->createContainerFromFile('lock_service', [], true, false);
$container->getCompilerPassConfig()->setOptimizationPasses([new ResolveChildDefinitionsPass()]);
$container->compile();

self::assertTrue($container->hasDefinition('lock.default.factory'));
$storeDef = $container->getDefinition($container->getDefinition('lock.default.factory')->getArgument(0));
self::assertEquals(new Reference('my_service'), $storeDef->getArgument(0));

self::assertInstanceOf(LockFactory::class, $container->get('factory_public_alias'));
}

public function testDefaultSemaphore()
{
$container = $this->createContainerFromFile('semaphore');
Expand All @@ -2455,6 +2471,19 @@ public function testNamedSemaphores()
self::assertStringContainsString('REDIS_DSN', $storeDef->getArgument(0));
}

public function testSemaphoreWithService()
{
$container = $this->createContainerFromFile('semaphore_service', [], true, false);
$container->getCompilerPassConfig()->setOptimizationPasses([new ResolveChildDefinitionsPass()]);
$container->compile();

self::assertTrue($container->hasDefinition('semaphore.default.factory'));
$storeDef = $container->getDefinition($container->getDefinition('semaphore.default.factory')->getArgument(0));
self::assertEquals(new Reference('my_service'), $storeDef->getArgument(0));

self::assertInstanceOf(SemaphoreFactory::class, $container->get('factory_public_alias'));
}

protected function createContainer(array $data = [])
{
return new ContainerBuilder(new EnvPlaceholderParameterBag(array_merge([
Expand Down