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

[DependencyInjection] added AutoDecorationServicePass #38432

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

Closed
wants to merge 1 commit into from
Closed
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
[DependencyInjection] added auto decoration using DecoratorInterface …
…or DecoratorPriorityAwareInterface
  • Loading branch information
Grégory SURACI committed Oct 6, 2020
commit 07ebc13c3039fa266325b786783ba66536638b2f
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\DecorationPriorityAwareInterface;
use Symfony\Component\DependencyInjection\DecoratorInterface;

/**
* Adds decorated service to definition of services implementing DecoratorInterface.
*
* @author Grégory SURACI <gregory.suraci@free.fr>
*/
class AutoDecorationServicePass implements CompilerPassInterface
{
private $throwOnException;

public function __construct(bool $throwOnException = true)
{
$this->throwOnException = $throwOnException;
}

/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
foreach ($container->getDefinitions() as $definition) {
$className = $definition->getClass();

if (null === $className) {
continue;
}

try {
$classInterfaces = class_implements($className);

if (!\in_array(DecoratorInterface::class, $classInterfaces)) {
continue;
}

if (\in_array(DecorationPriorityAwareInterface::class, $classInterfaces)) {
$definition->setDecoratedService(
$className::getDecoratedServiceId(),
null,
$className::getDecorationPriority()
);

continue;
}

$definition->setDecoratedService($className::getDecoratedServiceId());
} catch (\Throwable $e) {
if ($this->throwOnException) {
throw $e;
}

continue;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function __construct()
new AutowireRequiredPropertiesPass(),
new ResolveBindingsPass(),
new ServiceLocatorTagPass(),
new AutoDecorationServicePass(false),
new DecoratorServicePass(),
new CheckDefinitionValidityPass(),
new AutowirePass(false),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection;

/**
* DecorationPriorityAwareInterface sets the decoration_priority value for a class implementing DecoratorInterface.
*
* @author Grégory SURACI <gregory.suraci@free.fr>
*/
interface DecorationPriorityAwareInterface
{
/**
* @return int the decoration priority
*/
public static function getDecorationPriority(): int;
}
25 changes: 25 additions & 0 deletions 25 src/Symfony/Component/DependencyInjection/DecoratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection;

/**
* DecoratorInterface defines a decorator class without configuration.
*
* @author Grégory SURACI <gregory.suraci@free.fr>
*/
interface DecoratorInterface
{
/**
* @return string the serviceId/FQCN that will be decorated by this interface's implementation
*/
public static function getDecoratedServiceId(): string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Compiler\AutoDecorationServicePass;
use Symfony\Component\DependencyInjection\Compiler\DecoratorServicePass;
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Dummy;
use Symfony\Component\DependencyInjection\Tests\Fixtures\DummyDecorator1;
use Symfony\Component\DependencyInjection\Tests\Fixtures\DummyDecorator2;
use Symfony\Component\DependencyInjection\Tests\Fixtures\DummyDecorator3;

class AutoDecorationServicePassTest extends TestCase
{
public function testProcessUsingFQCN()
{
$container = new ContainerBuilder();
$dummyDefinition = $container
->register(Dummy::class)
->setPublic(true)
;
$decoratorDefinition = $container
->register('dummy.extended', DummyDecorator1::class)
->setPublic(true)
;

$this->process($container);

$this->assertSame($dummyDefinition, $container->getDefinition('dummy.extended.inner'));
$this->assertFalse($container->getDefinition('dummy.extended.inner')->isPublic());

$this->assertNull($decoratorDefinition->getDecoratedService());
}

public function testProcessUsingStringServiceId()
{
$container = new ContainerBuilder();
$dummyDefinition = $container
->register('dummy', Dummy::class)
->setPublic(true)
;
$decoratorDefinition = $container
->register('dummy.extended', DummyDecorator2::class)
->setPublic(true)
;

$this->process($container);

$this->assertSame($dummyDefinition, $container->getDefinition('dummy.extended.inner'));
$this->assertFalse($container->getDefinition('dummy.extended.inner')->isPublic());

$this->assertNull($decoratorDefinition->getDecoratedService());
}

public function testProcessWithDecorationPriority()
{
$container = new ContainerBuilder();
$dummyDefinition = $container
->register(Dummy::class)
->setPublic(true)
;
$decoratorWithHighPriorityDefinition = $container
->register('dummy.extended', DummyDecorator3::class)
->setPublic(true)
;
$decoratorDefinition = $container
->register('dummy.extended.extended', DummyDecorator1::class)
->setPublic(true)
;

$this->process($container);

$this->assertSame($dummyDefinition, $container->getDefinition('dummy.extended.inner'));
$this->assertFalse($container->getDefinition('dummy.extended.inner')->isPublic());

$this->assertEquals('dummy.extended', $container->getAlias('dummy.extended.extended.inner'));
$this->assertFalse($container->getAlias('dummy.extended.extended.inner')->isPublic());

$this->assertNull($decoratorWithHighPriorityDefinition->getDecoratedService());
$this->assertNull($decoratorDefinition->getDecoratedService());
}

protected function process(ContainerBuilder $container)
{
(new ResolveClassPass())->process($container);
(new AutoDecorationServicePass())->process($container);
(new DecoratorServicePass())->process($container);
}
}
20 changes: 20 additions & 0 deletions 20 src/Symfony/Component/DependencyInjection/Tests/Fixtures/Dummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

class Dummy implements DummyInterface
{
public function sayHello(): string
{
return 'Hello Dummy';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Symfony\Component\DependencyInjection\DecoratorInterface;

class DummyDecorator1 implements DecoratorInterface, DummyInterface
{
/**
* @var DummyInterface
*/
protected $decorated;

public function __construct(DummyInterface $decorated)
{
$this->decorated = $decorated;
}

public static function getDecoratedServiceId(): string
{
return Dummy::class;
}

public function sayHello(): string
{
return sprintf('%s & Decorator1', $this->decorated->sayHello());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Symfony\Component\DependencyInjection\DecoratorInterface;

class DummyDecorator2 implements DecoratorInterface, DummyInterface
{
/**
* @var DummyInterface
*/
protected $decorated;

public function __construct(DummyInterface $decorated)
{
$this->decorated = $decorated;
}

public static function getDecoratedServiceId(): string
{
return 'dummy';
}

public function sayHello(): string
{
return sprintf('%s & Decorator2', $this->decorated->sayHello());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Symfony\Component\DependencyInjection\DecorationPriorityAwareInterface;

class DummyDecorator3 extends DummyDecorator1 implements DecorationPriorityAwareInterface, DummyInterface
{
public static function getDecorationPriority(): int
{
return 42;
}

public function sayHello(): string
{
return sprintf('%s & Decorator3', $this->decorated->sayHello());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

interface DummyInterface
{
public function sayHello(): string;
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.