Skip to content

Commit 18913e1

Browse files
committed
[DependencyInjection] Add autowiring support to dumpers
1 parent 72c6c61 commit 18913e1

File tree

10 files changed

+131
-0
lines changed

10 files changed

+131
-0
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,15 @@ private function addService($id, $definition)
616616
EOF;
617617
}
618618

619+
if ($definition->isAutowired()) {
620+
$doc = <<<EOF
621+
622+
*
623+
* This service is autowired.
624+
EOF;
625+
626+
}
627+
619628
if ($definition->isLazy()) {
620629
$lazyInitialization = '$lazyLoad = true';
621630
$lazyInitializationDoc = "\n * @param bool \$lazyLoad whether to try lazy-loading the service with a proxy\n *";

src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@ private function addService($definition, $id, \DOMElement $parent)
204204
$service->appendChild($deprecated);
205205
}
206206

207+
if ($definition->isAutowired()) {
208+
$service->setAttribute('autowire', 'true');
209+
}
210+
211+
foreach ($definition->getAutowiringTypes() as $autowiringTypeValue) {
212+
$autowiringType = $this->document->createElement('autowiring-type');
213+
$autowiringType->appendChild($this->document->createTextNode($autowiringTypeValue));
214+
215+
$service->appendChild($autowiringType);
216+
}
217+
207218
if ($callable = $definition->getConfigurator()) {
208219
$configurator = $this->document->createElement('configurator');
209220

src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ private function addService($id, $definition)
104104
$code .= sprintf(" deprecated: %s\n", $definition->getDeprecationMessage('%service_id%'));
105105
}
106106

107+
if ($definition->isAutowired()) {
108+
$code .= " autowire: true\n";
109+
}
110+
111+
$autowiringTypesCode = '';
112+
foreach ($definition->getAutowiringTypes() as $autowiringType) {
113+
$autowiringTypesCode .= sprintf(" - %s\n", $this->dumper->dump($autowiringType));
114+
}
115+
if ($autowiringTypesCode) {
116+
$code .= sprintf(" autowiring_types:\n%s", $autowiringTypesCode);
117+
}
118+
107119
if ($definition->getFactoryClass(false)) {
108120
$code .= sprintf(" factory_class: %s\n", $definition->getFactoryClass(false));
109121
}

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,12 @@ public function testCircularReference()
218218
$dumper = new PhpDumper($container);
219219
$dumper->dump();
220220
}
221+
222+
public function testDumpAutowireData()
223+
{
224+
$container = include self::$fixturesPath.'/containers/container24.php';
225+
$dumper = new PhpDumper($container);
226+
227+
$this->assertEquals(file_get_contents(self::$fixturesPath.'/php/services24.php'), $dumper->dump());
228+
}
221229
}

src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,12 @@ public function testDumpInlinedServices()
181181

182182
$this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services21.xml'), $dumper->dump());
183183
}
184+
185+
public function testDumpAutowireData()
186+
{
187+
$container = include self::$fixturesPath.'/containers/container24.php';
188+
$dumper = new XmlDumper($container);
189+
190+
$this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services24.xml'), $dumper->dump());
191+
}
184192
}

src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,11 @@ public function testAddService()
7777
$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');
7878
}
7979
}
80+
81+
public function testDumpAutowireData()
82+
{
83+
$container = include self::$fixturesPath.'/containers/container24.php';
84+
$dumper = new YamlDumper($container);
85+
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services24.yml', $dumper->dump());
86+
}
8087
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\ContainerBuilder;
4+
use Symfony\Component\DependencyInjection\Definition;
5+
6+
$container = new ContainerBuilder();
7+
8+
$container
9+
->register('foo', 'Foo')
10+
->setAutowired(true)
11+
->addAutowiringType('A')
12+
->addAutowiringType('B')
13+
;
14+
15+
return $container;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\ContainerInterface;
4+
use Symfony\Component\DependencyInjection\Container;
5+
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
6+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
7+
use Symfony\Component\DependencyInjection\Exception\LogicException;
8+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
9+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
10+
11+
/**
12+
* ProjectServiceContainer.
13+
*
14+
* This class has been auto-generated
15+
* by the Symfony Dependency Injection Component.
16+
*/
17+
class ProjectServiceContainer extends Container
18+
{
19+
private $parameters;
20+
private $targetDirs = array();
21+
22+
/**
23+
* Constructor.
24+
*/
25+
public function __construct()
26+
{
27+
parent::__construct();
28+
$this->methodMap = array(
29+
'foo' => 'getFooService',
30+
);
31+
}
32+
33+
/**
34+
* Gets the 'foo' service.
35+
*
36+
* This service is autowired.
37+
*
38+
* @return \Foo A Foo instance.
39+
*/
40+
protected function getFooService()
41+
{
42+
return $this->services['foo'] = new \Foo();
43+
}
44+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<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">
3+
<services>
4+
<service id="foo" class="Foo" autowire="true">
5+
<autowiring-type>A</autowiring-type>
6+
<autowiring-type>B</autowiring-type>
7+
</service>
8+
</services>
9+
</container>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
services:
3+
foo:
4+
class: Foo
5+
autowire: true
6+
autowiring_types:
7+
- A
8+
- B

0 commit comments

Comments
 (0)