Skip to content

Commit

Permalink
[DependencyInjection] added support for anonymous services in XmlDumper
Browse files Browse the repository at this point in the history
  • Loading branch information
hason authored and fabpot committed Jan 14, 2011
1 parent 3a6f556 commit 2a3d94a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php
Expand Up @@ -5,6 +5,7 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\InterfaceInjector;

/*
Expand Down Expand Up @@ -81,10 +82,12 @@ protected function addInterfaceInjectors()
return sprintf(" <interfaces>\n%s </interfaces>\n", $code);
}

protected function addService($id, $definition)
protected function addService($definition, $id = null, $depth = 4)
{
$code = sprintf(" <service id=\"%s\"%s%s%s%s>\n",
$id,
$white = str_repeat(' ', $depth);
$code = sprintf("%s<service%s%s%s%s%s>\n",
$white,
(null !== $id ? sprintf(' id="%s"', $id): ''),
$definition->getClass() ? sprintf(' class="%s"', $definition->getClass()) : '',
$definition->getFactoryMethod() ? sprintf(' factory-method="%s"', $definition->getFactoryMethod()) : '',
$definition->getFactoryService() ? sprintf(' factory-service="%s"', $definition->getFactoryService()) : '',
Expand All @@ -99,39 +102,39 @@ protected function addService($id, $definition)
}
$att = $att ? ' '.implode(' ', $att) : '';

$code .= sprintf(" <tag name=\"%s\"%s />\n", $name, $att);
$code .= sprintf("%s <tag name=\"%s\"%s />\n", $white, $name, $att);
}
}

if ($definition->getFile()) {
$code .= sprintf(" <file>%s</file>\n", $definition->getFile());
$code .= sprintf("%s <file>%s</file>\n", $white, $definition->getFile());
}

if ($definition->getArguments()) {
$code .= $this->convertParameters($definition->getArguments(), 'argument', 6);
$code .= $this->convertParameters($definition->getArguments(), 'argument', $depth + 2);
}

foreach ($definition->getMethodCalls() as $call) {
if (count($call[1])) {
$code .= sprintf(" <call method=\"%s\">\n%s </call>\n", $call[0], $this->convertParameters($call[1], 'argument', 8));
$code .= sprintf("%s <call method=\"%s\">\n%s%s </call>\n", $white, $call[0], $this->convertParameters($call[1], 'argument', $depth + 4), $white);
} else {
$code .= sprintf(" <call method=\"%s\" />\n", $call[0]);
$code .= sprintf("%s <call method=\"%s\" />\n", $white, $call[0]);
}
}

if ($callable = $definition->getConfigurator()) {
if (is_array($callable)) {
if (is_object($callable[0]) && $callable[0] instanceof Reference) {
$code .= sprintf(" <configurator service=\"%s\" method=\"%s\" />\n", $callable[0], $callable[1]);
$code .= sprintf("%s <configurator service=\"%s\" method=\"%s\" />\n", $white, $callable[0], $callable[1]);
} else {
$code .= sprintf(" <configurator class=\"%s\" method=\"%s\" />\n", $callable[0], $callable[1]);
$code .= sprintf("%s <configurator class=\"%s\" method=\"%s\" />\n", $white, $callable[0], $callable[1]);
}
} else {
$code .= sprintf(" <configurator function=\"%s\" />\n", $callable);
$code .= sprintf("%s <configurator function=\"%s\" />\n", $white, $callable);
}
}

$code .= " </service>\n";
$code .= $white . "</service>\n";

return $code;
}
Expand All @@ -152,7 +155,7 @@ protected function addServices()

$code = '';
foreach ($this->container->getDefinitions() as $id => $definition) {
$code .= $this->addService($id, $definition);
$code .= $this->addService($definition, $id);
}

foreach ($this->container->getAliases() as $alias => $id) {
Expand All @@ -177,6 +180,9 @@ protected function convertParameters($parameters, $type='parameter', $depth = 2)

if (is_object($value) && $value instanceof Reference) {
$xml .= sprintf("%s<%s%s type=\"service\" id=\"%s\" %s/>\n", $white, $type, $key, (string) $value, $this->getXmlInvalidBehavior($value));

} else if (is_object($value) && $value instanceof Definition) {
$xml .= sprintf("%s<%s%s type=\"service\">\n%s%s</%s>\n", $white, $type, $key, $this->addService($value, null, $depth + 2), $white, $type);
} else {
if (in_array($value, array('null', 'true', 'false'), true)) {
$attributes = ' type="string"';
Expand Down
Expand Up @@ -100,4 +100,29 @@ public function testInterfaceInjectors()
</container>
", $classBody);
}

public function testDumpAnonymousServices()
{
include self::$fixturesPath.'/containers/container11.php';
$dumper = new XmlDumper($container);
$this->assertEquals("<?xml version=\"1.0\" ?>
<container xmlns=\"http://www.symfony-project.org/schema/dic/services\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xsi:schemaLocation=\"http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd\">
<services>
<service id=\"foo\" class=\"FooClass\">
<argument type=\"service\">
<service class=\"BarClass\">
<argument type=\"service\">
<service class=\"BazClass\">
</service>
</argument>
</service>
</argument>
</service>
</services>
</container>
", $dumper->dump());
}
}
@@ -0,0 +1,12 @@
<?php

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

$container = new ContainerBuilder();
$container->
register('foo', 'FooClass')->
addArgument(new Definition('BarClass', array(new Definition('BazClass'))))
;

return $container;

0 comments on commit 2a3d94a

Please sign in to comment.