8000 [DependencyInjection] Add autowiring capabilities by dunglas · Pull Request #15613 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Add autowiring capabilities #15613

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 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[DependencyInjection] Add the new 'autowire' syntax.
  • Loading branch information
dunglas committed Oct 1, 2015
commit 585616dc4189614d92d3fcc1d64e20dad25452f4
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class AutowiringPass implements CompilerPassInterface
class AutowirePass implements CompilerPassInterface
{
const AUTOWIRING = 'autowiring';

private $container;
private $reflectionClasses = array();
private $definedTypes = array();
Expand All @@ -38,7 +36,7 @@ public function process(ContainerBuilder $container)
{
$this->container = $container;
foreach ($container->getDefinitions() as $id => $definition) {
if ($definition->hasTag(self::AUTOWIRING)) {
if ($definition->isAutowired()) {
$this->completeDefinition($id, $definition);
}
}
Expand All @@ -64,6 +62,7 @@ private function completeDefinition($id, Definition $definition)
if (!($reflectionClass = $this->getReflectionClass($id, $definition))) {
Copy link
Member

Choose a reason for hiding this comment

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

extra parenthesis are not needed.

return;
}
$this->container->addClassResource($reflectionClass);

if (!($constructor = $reflectionClass->getConstructor())) {
Copy link
Member

Choose a reason for hiding this comment

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

same here

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct()
new CheckDefinitionValidityPass(),
new ResolveReferencesToAliasesPass(),
new ResolveInvalidReferencesPass(),
new AutowiringPass(),
new AutowirePass(),
new AnalyzeServiceReferencesPass(true),
new CheckCircularReferencesPass(),
new CheckReferenceValidityPass(),
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/DependencyInjection/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Definition
private $synchronized = false;
private $lazy = false;
private $decoratedService;
private $autowired = false;
private $autowiringTypes = array();
Copy link
Contributor

Choose a reason for hiding this comment

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

you might want change this prop. name with autowireTypesaccording to @weaverryan suggestion

Copy link
Member Author

Choose a reason for hiding this comment

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

$definition->isAutowire() looks weirder than $definition->isAutowired(). It's why I've kept autowired here but not in the loaders.


protected $arguments;
Expand Down Expand Up @@ -838,6 +839,30 @@ public function setTypes(array $types)
return $this;
}

/**
* Is the definition autowired?
*
* @return bool
*/
public function isAutowired()
{
return $this->autowired;
}

/**
* Sets autowired.
*
* @param $autowired
*
* @return Definition The current instance
*/
public function setAutowired($autowired)
{
$this->autowired = $autowired;

return $this;
}

/**
* Gets autowiring types that will default to this definition.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ private function parseDefinition(\DOMElement $service, $file)
}
}

if ($value = $service->getAttribute('autowire')) {
$definition->setAutowired(XmlUtils::phpize($value));
}

if ($value = $service->getAttribute('scope')) {
$triggerDeprecation = 'request' !== (string) $service->getAttribute('id');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ private function parseDefinition($id, $service, $file)
$definition->setDecoratedService($service['decorates'], $renameId, $priority);
}

if (isset($service['autowire'])) {
$definition->setAutowired($service['autowire']);
}

if (isset($service['autowiring_types'])) {
if (!is_array($service['autowiring_types'])) {
throw new InvalidArgumentException(sprintf('Parameter "autowiring_types" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
Copy link
Member

Choose a reason for hiding this comment

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

As having only one type is quite common, I would allow a single string to be passed here.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
<xsd:attribute name="decorates" type="xsd:string" />
<xsd:attribute name="decoration-inner-name" type="xsd:string" />
<xsd:attribute name="decoration-priority" type="xsd:integer" />
<xsd:attribute name="autowire" type="boolean" />
</xsd:complexType>

<xsd:complexType name="tag">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@

namespace Symfony\Component\DependencyInjection\Tests\Compiler;

use Symfony\Component\DependencyInjection\Compiler\AutowiringPass;
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class AutowiringPassTest extends \PHPUnit_Framework_TestCase
class AutowirePassTest extends \PHPUnit_Framework_TestCase
{
public function testProcess()
{
$container = new ContainerBuilder();

$container->register('foo', __NAMESPACE__.'\Foo');
$barDefinition = $container->register('bar', __NAMESPACE__.'\Bar');
$barDefinition->addTag(AutowiringPass::AUTOWIRING);
$barDefinition->setAutowired(true);

$pass = new AutowiringPass();
$pass = new AutowirePass();
$pass->process($container);

$this->assertCount(1, $container->getDefinition('bar')->getArguments());
Expand All @@ -41,9 +41,9 @@ public function testProcessAutowireParent()

$container->register('b', __NAMESPACE__.'\B');
$cDefinition = $container->register('c', __NAMESPACE__.'\C');
$cDefinition->addTag(AutowiringPass::AUTOWIRING);
$cDefinition->setAutowired(true);

$pass = new AutowiringPass();
$pass = new AutowirePass();
$pass->process($container);

$this->assertCount(1, $container->getDefinition('c')->getArguments());
Expand All @@ -56,9 +56,9 @@ public function testProcessAutowireInterface()

$container->register('f', __NAMESPACE__.'\F');
$gDefinition = $container->register('g', __NAMESPACE__.'\G');
$gDefinition->addTag(AutowiringPass::AUTOWIRING);
$gDefinition->setAutowired(true);

$pass = new AutowiringPass();
$pass = new AutowirePass();
$pass->process($container);

$this->assertCount(2, $container->getDefinition('g')->getArguments());
Expand All @@ -73,9 +73,9 @@ public function testCompleteExistingDefinition()
$container->register('b', __NAMESPACE__.'\B');
$container->register('f', __NAMESPACE__.'\F');
$hDefinition = $container->register('h', __NAMESPACE__.'\H')->addArgument(new Reference('b'));
$hDefinition->addTag(AutowiringPass::AUTOWIRING);
$hDefinition->setAutowired(true);

$pass = new AutowiringPass();
$pass = new AutowirePass();
$pass->process($container);

$this->assertCount(2, $container->getDefinition('h')->getArguments());
Expand All @@ -90,9 +90,9 @@ public function testCompleteExistingDefinitionWithNotDefinedArguments()
$container->register('b', __NAMESPACE__.'\B');
$container->register('f', __NAMESPACE__.'\F');
$hDefinition = $container->register('h', __NAMESPACE__.'\H')->addArgument('')->addArgument('');
$hDefinition->addTag(AutowiringPass::AUTOWIRING);
$hDefinition->setAutowired(true);

$pass = new AutowiringPass();
$pass = new AutowirePass();
$pass->process($container);

$this->assertCount(2, $container->getDefinition('h')->getArguments());
Expand All @@ -111,9 +111,9 @@ public function testTypeCollision()
$container->register('c1', __NAMESPACE__.'\CollisionA');
$container->register('c2', __NAMESPACE__.'\CollisionB');
$aDefinition = $container->register('a', __NAMESPACE__.'\CannotBeAutowired');
$aDefinition->addTag(AutowiringPass::AUTOWIRING);
$aDefinition->setAutowired(true);

$pass = new AutowiringPass();
$pass = new AutowirePass();
$pass->process($container);
}

Expand All @@ -124,9 +124,9 @@ public function testWithTypeSet()
$container->register('c1', __NAMESPACE__.'\CollisionA');
$container->register('c2', __NAMESPACE__.'\CollisionB')->addAutowiringType(__NAMESPACE__.'\CollisionInterface');
$aDefinition = $container->register('a', __NAMESPACE__.'\CannotBeAutowired');
$aDefinition->addTag(AutowiringPass::AUTOWIRING);
$aDefinition->setAutowired(true);

$pass = new AutowiringPass();
$pass = new AutowirePass();
$pass->process($container);

$this->assertCount(1, $container->getDefinition('a')->getArguments());
Expand All @@ -138,9 +138,9 @@ public function testCreateDefinition()
$container = new ContainerBuilder();

$coopTilleulsDefinition = $container->register('coop_tilleuls', __NAMESPACE__.'\LesTilleuls');
$coopTilleulsDefinition->addTag(AutowiringPass::AUTOWIRING);
$coopTilleulsDefinition->setAutowired(true);

$pass = new AutowiringPass();
$pass = new AutowirePass();
$pass->process($container);

$this->assertCount(1, $container->getDefinition('coop_tilleuls')->getArguments());
Expand All @@ -163,9 +163,9 @@ public function testResolveParameter()
$container->setParameter('class_name', __NAMESPACE__.'\Foo');
$container->register('foo', '%class_name%');
$barDefinition = $container->register('bar', __NAMESPACE__.'\Bar');
$barDefinition->addTag(AutowiringPass::AUTOWIRING);
$barDefinition->setAutowired(true);

$pass = new AutowiringPass();
$pass = new AutowirePass();
$pass->process($container);

$this->assertEquals('foo', $container->getDefinition('bar')->getArgument(0));
Expand All @@ -178,9 +178,9 @@ public function testOptionalParameter()
$container->register('a', __NAMESPACE__.'\A');
$container->register('foo', __NAMESPACE__.'\Foo');
$optDefinition = $container->register('opt', __NAMESPACE__.'\OptionalParameter');
$optDefinition->addTag(AutowiringPass::AUTOWIRING);
$optDefinition->setAutowired(true);

$pass = new AutowiringPass();
$pass = new AutowirePass();
$pass->process($container);

$definition = $container->getDefinition('opt');
Expand All @@ -196,7 +196,7 @@ public function testDontTriggeruAutowiring()
$container->register('foo', __NAMESPACE__.'\Foo');
$container->register('bar', __NAMESPACE__.'\Bar');

$pass = new AutowiringPass();
$pass = new AutowirePass();
$pass->process($container);

$this->assertCount(0, $container->getDefinition('bar')->getArguments());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,14 @@ public function testSetProperty()
$this->assertEquals(array('foo' => 'bar'), $def->getProperties());
}

public function testAutowired()
{
$def = new Definition('stdClass');
$this->assertFalse($def->isAutowired());
$def->setAutowired(true);
$this->assertTrue($def->isAutowired());
}

public function testTypes()
{
$def = new Definition('stdClass');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="bar" class="Bar" autowire="true" />
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
bar_service:
class: BarClass
autowire: true
Original file line number Diff line number Diff line change
Expand Up @@ -503,4 +503,13 @@ public function testType()

$this->assertEquals(array('Bar', 'Baz'), $container->getDefinition('foo')->getAutowiringTypes());
}

public function testAutowire()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('services23.xml');

$this->assertTrue($container->getDefinition('bar')->isAutowired());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,13 @@ public function testTypes()

$this->assertEquals(array('Foo', 'Bar'), $container->getDefinition('foo_service')->getAutowiringTypes());
}

public function testAutowire()
{
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('services23.yml');

$this->assertTrue($container->getDefinition('bar_service')->isAutowired());
}
}
0