Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3738e84

Browse filesBrowse files
committed
Improve proposal design for attribute and decorator
1 parent b14f895 commit 3738e84
Copy full SHA for 3738e84

File tree

18 files changed

+232
-198
lines changed
Filter options

18 files changed

+232
-198
lines changed

‎src/Symfony/Bridge/Doctrine/Attribute/Transactional.php renamed to ‎src/Symfony/Bridge/Doctrine/Decorator/Transactional.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Decorator/Transactional.php
+6-7Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,23 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Bridge\Doctrine\Attribute;
12+
namespace Symfony\Bridge\Doctrine\Decorator;
1313

14-
use Symfony\Bridge\Doctrine\Decorator\DoctrineTransactionDecorator;
15-
use Symfony\Component\Decorator\Attribute\Decorate;
14+
use Symfony\Component\Decorator\Attribute\DecoratorMetadata;
1615

1716
/**
1817
* Wraps persistence method operations within a single Doctrine transaction.
1918
*
2019
* @author Yonel Ceruto <open@yceruto.dev>
2120
*/
2221
#[\Attribute(\Attribute::TARGET_METHOD)]
23-
class Transactional extends Decorate
22+
class Transactional extends DecoratorMetadata
2423
{
2524
/**
2625
* @param string|null $name The entity manager name (null for the default one)
2726
*/
28-
public function __construct(?string $name = null)
29-
{
30-
parent::__construct(DoctrineTransactionDecorator::class, ['name' => $name]);
27+
public function __construct(
28+
public ?string $name = null,
29+
) {
3130
}
3231
}

‎src/Symfony/Bridge/Doctrine/Decorator/DoctrineTransactionDecorator.php renamed to ‎src/Symfony/Bridge/Doctrine/Decorator/TransactionalDecorator.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Decorator/TransactionalDecorator.php
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@
1818
/**
1919
* @author Yonel Ceruto <open@yceruto.dev>
2020
*/
21-
class DoctrineTransactionDecorator implements DecoratorInterface
21+
class TransactionalDecorator implements DecoratorInterface
2222
{
2323
public function __construct(
2424
private readonly ManagerRegistry $managerRegistry,
2525
) {
2626
}
2727

28-
public function decorate(\Closure $func, ?string $name = null): \Closure
28+
public function decorate(\Closure $func, Transactional $transactional = new Transactional()): \Closure
2929
{
30-
$entityManager = $this->managerRegistry->getManager($name);
30+
$entityManager = $this->managerRegistry->getManager($transactional->name);
3131

3232
if (!$entityManager instanceof EntityManagerInterface) {
33-
throw new \RuntimeException(\sprintf('The manager "%s" is not an entity manager.', $name));
33+
throw new \RuntimeException(\sprintf('The manager "%s" is not an entity manager.', $transactional->name));
3434
}
3535

3636
return static function (mixed ...$args) use ($func, $entityManager) {

‎src/Symfony/Bridge/Doctrine/Tests/Decorator/DoctrineTransactionDecoratorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/Decorator/DoctrineTransactionDecoratorTest.php
+7-7Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
use Doctrine\ORM\EntityManagerInterface;
1616
use Doctrine\Persistence\ManagerRegistry;
1717
use PHPUnit\Framework\TestCase;
18-
use Symfony\Bridge\Doctrine\Attribute\Transactional;
19-
use Symfony\Bridge\Doctrine\Decorator\DoctrineTransactionDecorator;
20-
use Symfony\Component\Decorator\DecoratorChain;
21-
use Symfony\Component\Decorator\DecoratorLocator;
18+
use Symfony\Bridge\Doctrine\Decorator\Transactional;
19+
use Symfony\Bridge\Doctrine\Decorator\TransactionalDecorator;
20+
use Symfony\Component\Decorator\CallableDecorator;
21+
use Symfony\Component\Decorator\Resolver\DecoratorResolver;
2222

2323
class DoctrineTransactionDecoratorTest extends TestCase
2424
{
2525
private ManagerRegistry $managerRegistry;
2626
private Connection $connection;
2727
private EntityManagerInterface $entityManager;
28-
private DecoratorChain $decorator;
28+
private CallableDecorator $decorator;
2929

3030
protected function setUp(): void
3131
{
@@ -37,8 +37,8 @@ protected function setUp(): void
3737
$this->managerRegistry = $this->createMock(ManagerRegistry::class);
3838
$this->managerRegistry->method('getManager')->willReturn($this->entityManager);
3939

40-
$this->decorator = new DecoratorChain(new DecoratorLocator([
41-
DoctrineTransactionDecorator::class => fn () => new DoctrineTransactionDecorator($this->managerRegistry),
40+
$this->decorator = new CallableDecorator(new DecoratorResolver([
41+
TransactionalDecorator::class => fn () => new TransactionalDecorator($this->managerRegistry),
4242
]));
4343
}
4444

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/decorator.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/decorator.php
+8-7Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,23 @@
1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

1414
use Symfony\Bundle\FrameworkBundle\EventListener\DecorateControllerListener;
15-
use Symfony\Component\Decorator\DecoratorChain;
15+
use Symfony\Component\Decorator\CallableDecorator;
1616
use Symfony\Component\Decorator\DecoratorInterface;
17+
use Symfony\Component\Decorator\Resolver\DecoratorResolverInterface;
1718

1819
return static function (ContainerConfigurator $container) {
1920
$container->services()
20-
->set('decorator.chain', DecoratorChain::class)
21+
->set('decorator.callable_decorator', CallableDecorator::class)
2122
->args([
22-
abstract_arg('decorators locator, set in DecoratorsPass'),
23+
service(DecoratorResolverInterface::class),
2324
])
2425

25-
->set('decorator.listener.controller', DecorateControllerListener::class)
26+
->alias(DecoratorInterface::class, 'decorator.callable_decorator')
27+
28+
->set('decorator.decorate_controller.listener', DecorateControllerListener::class)
2629
->args([
27-
service('decorator.chain'),
30+
service('decorator.callable_decorator'),
2831
])
2932
->tag('kernel.event_subscriber')
30-
31-
->alias(DecoratorInterface::class, 'decorator.chain')
3233
;
3334
};

‎src/Symfony/Component/Decorator/Attribute/Decorate.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Decorator/Attribute/Decorate.php
-33Lines changed: 0 additions & 33 deletions
This file was deleted.
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Decorator\Attribute;
13+
14+
/**
15+
* DecoratorMetadata is the abstract class for all decorator metadata attributes.
16+
*
17+
* @author Yonel Ceruto <open@yceruto.dev>
18+
*
19+
* @experimental
20+
*/
21+
abstract class DecoratorMetadata
22+
{
23+
public function decoratedBy(): string
24+
{
25+
return static::class.'Decorator';
26+
}
27+
}
+57Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Decorator;
13+
14+
use Symfony\Component\Decorator\Attribute\DecoratorMetadata;
15+
use Symfony\Component\Decorator\Resolver\DecoratorResolver;
16+
use Symfony\Component\Decorator\Resolver\DecoratorResolverInterface;
17+
18+
/**
19+
* Wraps a callable with all the decorators linked to it.
20+
*
21+
* @author Yonel Ceruto <open@yceruto.dev>
22+
*
23+
* @experimental
24+
*/
25+
class CallableDecorator implements DecoratorInterface
26+
{
27+
public function __construct(
28+
private readonly DecoratorResolverInterface $resolver = new DecoratorResolver([]),
29+
) {
30+
}
31+
32+
public function call(callable $callable, mixed ...$args): mixed
33+
{
34+
return $this->decorate($callable(...))(...$args);
35+
}
36+
37+
public function decorate(\Closure $func): \Closure
38+
{
39+
foreach ($this->getMetadata($func) as $metadata) {
40+
$func = $this->resolver->resolve($metadata)->decorate($func, $metadata);
41+
}
42+
43+
return $func;
44+
}
45+
46+
/**
47+
* @return iterable<DecoratorMetadata>
48+
*/
49+
private function getMetadata(\Closure $func): iterable
50+
{
51+
$attributes = (new \ReflectionFunction($func))->getAttributes(DecoratorMetadata::class, \ReflectionAttribute::IS_INSTANCEOF);
52+
53+
foreach (array_reverse($attributes) as $attribute) {
54+
yield $attribute->newInstance();
55+
}
56+
}
57+
}

‎src/Symfony/Component/Decorator/DecoratorChain.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Decorator/DecoratorChain.php
-63Lines changed: 0 additions & 63 deletions
This file was deleted.

‎src/Symfony/Component/Decorator/DecoratorLocator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Decorator/DecoratorLocator.php
-30Lines changed: 0 additions & 30 deletions
This file was deleted.

‎src/Symfony/Component/Decorator/DependencyInjection/DecoratorsPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Decorator/DependencyInjection/DecoratorsPass.php
+13-4Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111

1212
namespace Symfony\Component\Decorator\DependencyInjection;
1313

14-
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
14+
use Symfony\Component\Decorator\Resolver\DecoratorResolver;
15+
use Symfony\Component\Decorator\Resolver\DecoratorResolverInterface;
1516
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
1617
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1718
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
19+
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
1820
use Symfony\Component\DependencyInjection\ContainerBuilder;
21+
use Symfony\Component\DependencyInjection\Definition;
1922

2023
/**
2124
* @author Yonel Ceruto <open@yceruto.dev>
@@ -26,14 +29,20 @@
2629

2730
public function process(ContainerBuilder $container): void
2831
{
29-
if (!$container->hasDefinition('decorator.chain')) {
32+
if (!$container->hasDefinition('decorator.callable_decorator')) {
3033
return;
3134
}
3235

3336
$tagName = new TaggedIteratorArgument('decorator', needsIndexes: true);
3437
$decorators = $this->findAndSortTaggedServices($tagName, $container);
3538

36-
$decoratorChain = $container->getDefinition('decorator.chain');
37-
$decoratorChain->replaceArgument(0, new ServiceLocatorArgument($decorators));
39+
$resolver = (new Definition(DecoratorResolver::class))
40+
->addArgument(ServiceLocatorTagPass::map($decorators))
41+
->addTag('container.service_locator');
42+
43+
$id = '.service_locator.'.ContainerBuilder::hash($resolver);
44+
$container->setDefinition($id, $resolver);
45+
46+
$container->setAlias(DecoratorResolverInterface::class, $id);
3847
}
3948
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.