Permalink
Browse files

feature #27077 [DependencyInjection] add ServiceSubscriberTrait (kbond)

This PR was squashed before being merged into the 4.2-dev branch (closes #27077).

Discussion
----------

[DependencyInjection] add ServiceSubscriberTrait

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #23898
| License       | MIT
| Doc PR        | symfony/symfony-docs#9809

This allows you to easily configure Service Subscribers with the following convention:

```php
class MyService implements ServiceSubscriberInterface
{
    use ServiceSubscriberTrait;

    public function doSomething()
    {
        // $this->router() ...
    }

    private function router(): RouterInterface
    {
        return $this->container->get(__METHOD__);
    }
}
```

This also allows you to create helper traits like `RouterAware`, `LoggerAware` etc... and compose your services with them (*not* using `__METHOD__` in traits because it doesn't behave as expected.).

```php
trait LoggerAware
{
    private function logger(): LoggerInterface
    {
        return $this->container->get(__CLASS__.'::'.__FUNCTION__);
    }
}
```

```php
trait RouterAware
{
    private function router(): RouterInterface
    {
        return $this->container->get(__CLASS__.'::'.__FUNCTION__);
    }
}
```

```php
class MyService implements ServiceSubscriberInterface
{
    use ServiceSubscriberTrait, LoggerAware, RouterAware;

    public function doSomething()
    {
        // $this->router() ...
        // $this->logger() ...
    }
}
```

Commits
-------

238e793 [DependencyInjection] add ServiceSubscriberTrait
  • Loading branch information...
nicolas-grekas committed Jun 4, 2018
2 parents b51b17d + 238e793 commit fa022f05be75aacc8afcdc11b63333685c719e13
@@ -1,6 +1,11 @@
CHANGELOG
=========
4.2.0
-----
* added `ServiceSubscriberTrait`
4.1.0
-----
@@ -0,0 +1,61 @@
<?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;
use Psr\Container\ContainerInterface;
/**
* Implementation of ServiceSubscriberInterface that determines subscribed services from
* private method return types. Service ids are available as "ClassName::methodName".
*
* @author Kevin Bond <kevinbond@gmail.com>
*/
trait ServiceSubscriberTrait
{
/** @var ContainerInterface */
private $container;
public static function getSubscribedServices(): array
{
static $services;
if (null !== $services) {
return $services;
}
$services = \is_callable(array('parent', __FUNCTION__)) ? parent::getSubscribedServices() : array();
foreach ((new \ReflectionClass(self::class))->getMethods() as $method) {
if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
continue;
}
if (self::class === $method->getDeclaringClass()->name && ($returnType = $method->getReturnType()) && !$returnType->isBuiltin()) {
$services[self::class.'::'.$method->name] = '?'.$returnType->getName();
}
}
return $services;
}
/**
* @required
*/
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
if (\is_callable(array('parent', __FUNCTION__))) {
return parent::setContainer($container);
}
}
}
@@ -21,7 +21,12 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition2;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition3;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriberChild;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriberParent;
use Symfony\Component\DependencyInjection\TypedReference;
require_once __DIR__.'/../Fixtures/includes/classes.php';
@@ -136,4 +141,29 @@ public function testExtraServiceSubscriber()
$container->register(TestServiceSubscriber::class, TestServiceSubscriber::class);
$container->compile();
}
public function testServiceSubscriberTrait()
{
$container = new ContainerBuilder();
$container->register('foo', TestServiceSubscriberChild::class)
->addMethodCall('setContainer', array(new Reference(PsrContainerInterface::class)))
->addTag('container.service_subscriber')
;
(new RegisterServiceSubscribersPass())->process($container);
(new ResolveServiceSubscribersPass())->process($container);
$foo = $container->getDefinition('foo');
$locator = $container->getDefinition((string) $foo->getMethodCalls()[0][1][0]);
$expected = array(
TestServiceSubscriberChild::class.'::invalidDefinition' => new ServiceClosureArgument(new TypedReference('Symfony\Component\DependencyInjection\Tests\Fixtures\InvalidDefinition', 'Symfony\Component\DependencyInjection\Tests\Fixtures\InvalidDefinition', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
TestServiceSubscriberChild::class.'::testDefinition2' => new ServiceClosureArgument(new TypedReference(TestDefinition2::class, TestDefinition2::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
TestServiceSubscriberChild::class.'::testDefinition3' => new ServiceClosureArgument(new TypedReference(TestDefinition3::class, TestDefinition3::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
TestServiceSubscriberParent::class.'::testDefinition1' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class, TestDefinition1::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
);
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
}
}
@@ -0,0 +1,9 @@
<?php
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
use Symfony\Component\DependencyInjection\Definition;
class TestDefinition1 extends Definition
{
}
@@ -0,0 +1,9 @@
<?php
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
use Symfony\Component\DependencyInjection\Definition;
class TestDefinition2 extends Definition
{
}
@@ -0,0 +1,9 @@
<?php
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
use Symfony\Component\DependencyInjection\Definition;
class TestDefinition3 extends Definition
{
}
@@ -0,0 +1,28 @@
<?php
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
use Symfony\Component\DependencyInjection\ServiceSubscriberTrait;
class TestServiceSubscriberChild extends TestServiceSubscriberParent
{
use ServiceSubscriberTrait, TestServiceSubscriberTrait;
private function testDefinition2(): TestDefinition2
{
return $this->container->get(__METHOD__);
}
private function invalidDefinition(): InvalidDefinition
{
return $this->container->get(__METHOD__);
}
private function privateFunction1(): string
{
}
private function privateFunction2(): string
{
}
}
@@ -0,0 +1,16 @@
<?php
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\DependencyInjection\ServiceSubscriberTrait;
class TestServiceSubscriberParent implements ServiceSubscriberInterface
{
use ServiceSubscriberTrait;
private function testDefinition1(): TestDefinition1
{
return $this->container->get(__METHOD__);
}
}
@@ -0,0 +1,11 @@
<?php
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
trait TestServiceSubscriberTrait
{
private function testDefinition3(): TestDefinition3
{
return $this->container->get(__CLASS__.'::'.__FUNCTION__);
}
}
@@ -0,0 +1,63 @@
<?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;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\DependencyInjection\ServiceSubscriberTrait;
class ServiceSubscriberTraitTest extends TestCase
{
public function testMethodsOnParentsAndChildrenAreIgnoredInGetSubscribedServices()
{
$expected = array(TestService::class.'::aService' => '?Symfony\Component\DependencyInjection\Tests\Service2');
$this->assertEquals($expected, ChildTestService::getSubscribedServices());
}
public function testSetContainerIsCalledOnParent()
{
$container = new Container();
$this->assertSame($container, (new TestService())->setContainer($container));
}
}
class ParentTestService
{
public function aParentService(): Service1
{
}
public function setContainer(ContainerInterface $container)
{
return $container;
}
}
class TestService extends ParentTestService implements ServiceSubscriberInterface
{
use ServiceSubscriberTrait;
public function aService(): Service2
{
}
}
class ChildTestService extends TestService
{
public function aChildService(): Service3
{
}
}

0 comments on commit fa022f0

Please sign in to comment.