[DependencyInjection] Add autowiring support to dumpers

This commit is contained in:
Kévin Dunglas 2015-11-03 15:41:24 -08:00
parent 72c6c61dbe
commit 18913e15d5
10 changed files with 131 additions and 0 deletions

View File

@ -616,6 +616,15 @@ EOF;
EOF;
}
if ($definition->isAutowired()) {
$doc = <<<EOF
*
* This service is autowired.
EOF;
}
if ($definition->isLazy()) {
$lazyInitialization = '$lazyLoad = true';
$lazyInitializationDoc = "\n * @param bool \$lazyLoad whether to try lazy-loading the service with a proxy\n *";

View File

@ -204,6 +204,17 @@ class XmlDumper extends Dumper
$service->appendChild($deprecated);
}
if ($definition->isAutowired()) {
$service->setAttribute('autowire', 'true');
}
foreach ($definition->getAutowiringTypes() as $autowiringTypeValue) {
$autowiringType = $this->document->createElement('autowiring-type');
$autowiringType->appendChild($this->document->createTextNode($autowiringTypeValue));
$service->appendChild($autowiringType);
}
if ($callable = $definition->getConfigurator()) {
$configurator = $this->document->createElement('configurator');

View File

@ -104,6 +104,18 @@ class YamlDumper extends Dumper
$code .= sprintf(" deprecated: %s\n", $definition->getDeprecationMessage('%service_id%'));
}
if ($definition->isAutowired()) {
$code .= " autowire: true\n";
}
$autowiringTypesCode = '';
foreach ($definition->getAutowiringTypes() as $autowiringType) {
$autowiringTypesCode .= sprintf(" - %s\n", $this->dumper->dump($autowiringType));
}
if ($autowiringTypesCode) {
$code .= sprintf(" autowiring_types:\n%s", $autowiringTypesCode);
}
if ($definition->getFactoryClass(false)) {
$code .= sprintf(" factory_class: %s\n", $definition->getFactoryClass(false));
}

View File

@ -218,4 +218,12 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase
$dumper = new PhpDumper($container);
$dumper->dump();
}
public function testDumpAutowireData()
{
$container = include self::$fixturesPath.'/containers/container24.php';
$dumper = new PhpDumper($container);
$this->assertEquals(file_get_contents(self::$fixturesPath.'/php/services24.php'), $dumper->dump());
}
}

View File

@ -181,4 +181,12 @@ class XmlDumperTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services21.xml'), $dumper->dump());
}
public function testDumpAutowireData()
{
$container = include self::$fixturesPath.'/containers/container24.php';
$dumper = new XmlDumper($container);
$this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services24.xml'), $dumper->dump());
}
}

View File

@ -77,4 +77,11 @@ class YamlDumperTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('Unable to dump a service container if a parameter is an object or a resource.', $e->getMessage(), '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
}
}
public function testDumpAutowireData()
{
$container = include self::$fixturesPath.'/containers/container24.php';
$dumper = new YamlDumper($container);
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services24.yml', $dumper->dump());
}
}

View File

@ -0,0 +1,15 @@
<?php
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
$container = new ContainerBuilder();
$container
->register('foo', 'Foo')
->setAutowired(true)
->addAutowiringType('A')
->addAutowiringType('B')
;
return $container;

View File

@ -0,0 +1,44 @@
<?php
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
/**
* ProjectServiceContainer.
*
* This class has been auto-generated
* by the Symfony Dependency Injection Component.
*/
class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();
/**
* Constructor.
*/
public function __construct()
{
parent::__construct();
$this->methodMap = array(
'foo' => 'getFooService',
);
}
/**
* Gets the 'foo' service.
*
* This service is autowired.
*
* @return \Foo A Foo instance.
*/
protected function getFooService()
{
return $this->services['foo'] = new \Foo();
}
}

View File

@ -0,0 +1,9 @@
<?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="foo" class="Foo" autowire="true">
<autowiring-type>A</autowiring-type>
<autowiring-type>B</autowiring-type>
</service>
</services>
</container>

View File

@ -0,0 +1,8 @@
services:
foo:
class: Foo
autowire: true
autowiring_types:
- A
- B