8000 YamlFileLoader handles anonymous services · symfony/symfony@455375f · GitHub
[go: up one dir, main page]

Skip to content

Commit 455375f

Browse files
committed
YamlFileLoader handles anonymous services
1 parent 4c824cd commit 455375f

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
/**
2727
* YamlFileLoader loads YAML files service definitions.
2828
*
29-
* The YAML format does not support anonymous services (cf. the XML loader).
30-
*
3129
* @author Fabien Potencier <fabien@symfony.com>
3230
*/
3331
class YamlFileLoader extends FileLoader
@@ -50,6 +48,9 @@ public function load($resource, $type = null)
5048
return;
5149
}
5250

51+
// anonymous services
52+
$this->processAnonymousServices($content, $path);
53+
5354
// imports
5455
$this->parseImports($content, $path);
5556

@@ -360,6 +361,63 @@ protected function loadFile($file)
360361
return $this->validate($configuration, $file);
361362
}
362363

364+
/**
365+
* Processes anonymous services.
366+
*
367+
* @param array $content
368+
* @param string $path
369+
*/
370+
private function processAnonymousServices(array &$content, $path)
371+
{
372+
$definitions = array();
373+
$count = 0;
374+
375+
if (false === array_key_exists('services', $content) || false === is_array($content['services']))
376+
{
377+
return;
378+
}
379+
380+
foreach ($content['services'] as $id => &$service)
381+
{
382+
if (false === is_array($service))
383+
{
384+
continue;
385+
}
386+
387+
// TODO covert "properties" key also
388+
if (false === array_key_exists('arguments', (array) $service))
389+
{
390+
continue;
391+
}
392+
393+
foreach ($service['arguments'] as &$argument)
394+
{
395+
if (false === is_array($argument))
396+
{
397+
continue;
398+
}
399+
400+
if (false === array_key_exists('type', $argument) || 'service' !== $argument['type'])
401+
{
402+
continue;
403+
}
404+
405+
$id = sprintf('%s_%d', hash('sha256', $path), ++$count);
406+
$argument['id'] = $id;
407+
$definitions[$id] = array(&$argument, $path);
408+
}
409+
}
410+
411+
// resolve definitions
412+
ksort($definitions);
413+
foreach ($definitions as $id => $def) {
414+
$serviceDef = &$def[0];
415+
$serviceDef['public'] = false;
416+
$this->parseDefinition($id, (array) $def[0], $def[1]);
417+
$serviceDef = '@'.$id;
418+
}
419+
}
420+
363421
/**
364422
* Validates a YAML file.
365423
*
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
services:
2+
service_with_anonymous_services:
3+
class: FooClass
4+
arguments: [{type: service, class: BazClass}]
5+
another_service_with_anonymous_services:
6+
class: FooClass
7+
arguments:
8+
- type: service
9+
class: BazClass
10+
- type: service
11+
class: BarClass

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,24 @@ public function testLoadServices()
177177
$this->assertEquals(array('decorated', 'decorated.pif-pouf', 5), $services['decorator_service_with_name_and_priority']->getDecoratedService());
178178
}
179179

180+
public function testAnonymousServices()
181+
{
182+
$container = new ContainerBuilder();
183+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
184+
$loader->load('anonymous_services.yml');
185+
$services = $container->getDefinitions();
186+
$arguments = $services['service_with_anonymous_services']->getArguments();
187+
$arguments2 = $services['another_service_with_anonymous_services']->getArguments();
188+
$this->assertCount(1, $arguments);
189+
$this->assertCount(2, $arguments2);
190+
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Reference', $arguments[0]);
191+
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Reference', $arguments2[0]);
192+
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Reference', $arguments2[1]);
193+
$this->assertEquals('BazClass', $services[(string) $arguments[0]]->getClass());
194+
$this->assertEquals('BazClass', $services[(string) $arguments2[0]]->getClass());
195+
$this->assertEquals('BarClass', $services[(string) $arguments2[1]]->getClass());
196+
}
197+
180198
public function testLoadFactoryShortSyntax()
181199
{
182200
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)
0