Skip to content

Commit

Permalink
[DependencyInjection] added a way to inject an anonymous service in a…
Browse files Browse the repository at this point in the history
…n extension configuration

    <foo:bar>
        <service class="Foo" />
        <service class="Bar" />
    </foo:bar>

In the foo:bar extension method, you can retrieve the services with:

    // always an array of services
    $config['_services']
  • Loading branch information
fabpot committed Sep 2, 2010
1 parent 9e4aebf commit 7c1b42e
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php
Expand Up @@ -179,12 +179,24 @@ protected function processAnonymousServices($xml, $file)

// find anonymous service definitions
$xml->registerXPathNamespace('container', 'http://www.symfony-project.org/schema/dic/services');

// anonymous services as arguments
$nodes = $xml->xpath('//container:argument[@type="service"][not(@id)]');
foreach ($nodes as $node) {
// give it a unique names
// give it a unique name
$node['id'] = sprintf('_%s_%d', md5($file), ++$count);

$definitions[(string) $node['id']] = array($node->service, $file, false);
$node->service['id'] = (string) $node['id'];
}

// anonymous services "in the wild"
$nodes = $xml->xpath('//container:service[not(@id)]');
foreach ($nodes as $node) {
// give it a unique name
$node['id'] = sprintf('_%s_%d', md5($file), ++$count);

$definitions[(string) $node['id']] = array($node->service, $file);
$definitions[(string) $node['id']] = array($node, $file, true);
$node->service['id'] = (string) $node['id'];
}

Expand All @@ -194,7 +206,13 @@ protected function processAnonymousServices($xml, $file)
$this->parseDefinition($id, $def[0], $def[1]);

$oNode = dom_import_simplexml($def[0]);
$oNode->parentNode->removeChild($oNode);
if (true === $def[2]) {
$nNode = new \DOMElement('_services');
$oNode->parentNode->replaceChild($nNode, $oNode);
$nNode->setAttribute('id', $id);
} else {
$oNode->parentNode->removeChild($oNode);
}
}

return $xml;
Expand Down Expand Up @@ -350,13 +368,20 @@ static public function convertDomElementToArray(\DomElement $element)
$empty = false;
}
} elseif (!$node instanceof \DOMComment) {
if (isset($config[$node->localName])) {
if (!is_array($config[$node->localName]) || !is_int(key($config[$node->localName]))) {
$config[$node->localName] = array($config[$node->localName]);
if ($node instanceof \DOMElement && '_services' === $node->nodeName) {
$value = new Reference($node->getAttribute('id'));
} else {
$value = static::convertDomElementToArray($node);
}

$key = $node->localName;
if (isset($config[$key])) {
if (!is_array($config[$key]) || !is_int(key($config[$key]))) {
$config[$key] = array($config[$key]);
}
$config[$node->localName][] = static::convertDomElementToArray($node);
$config[$key][] = $value;
} else {
$config[$node->localName] = static::convertDomElementToArray($node);
$config[$key] = $value;
}

$empty = false;
Expand Down

0 comments on commit 7c1b42e

Please sign in to comment.