Skip to content

[DependencyInjection] add ServiceSubscriberTrait #27077

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
Jun 4, 2018
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.2.0
-----

* added `ServiceSubscriberTrait`

4.1.0
-----

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Symfony\Component\DependencyInjection\Definition;

class TestDefinition1 extends Definition
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Symfony\Component\DependencyInjection\Definition;

class TestDefinition2 extends Definition
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Symfony\Component\DependencyInjection\Definition;

class TestDefinition3 extends Definition
{
}
Original file line number Diff line number Diff line change
@@ -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
{
}
}
Original file line number Diff line number Diff line change
@@ -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__);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

trait TestServiceSubscriberTrait
{
private function testDefinition3(): TestDefinition3
{
return $this->container->get(__CLASS__.'::'.__FUNCTION__);
}
}
Original file line number Diff line number Diff line change
@@ -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
{
}
}