This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php

264 lines
9.3 KiB
PHP
Raw Normal View History

2010-01-04 14:26:20 +00:00
<?php
namespace Symfony\Component\DependencyInjection\Dumper;
2010-01-04 14:26:20 +00:00
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;
2010-01-04 14:26:20 +00:00
/*
* This file is part of the Symfony framework.
2010-01-04 14:26:20 +00:00
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* XmlDumper dumps a service container as an XML string.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Martin Hasoň <martin.hason@gmail.com>
2010-01-04 14:26:20 +00:00
*/
class XmlDumper extends Dumper
{
/**
* @var \DOMDocument
*/
protected $document;
/**
* Dumps the service container as an XML string.
*
* @param array $options An array of options
*
* @return string An xml string representing of the service container
*/
public function dump(array $options = array())
2010-01-04 14:26:20 +00:00
{
$this->document = new \DOMDocument('1.0', 'utf-8');
$this->document->formatOutput = true;
$container = $this->document->createElementNS('http://www.symfony-project.org/schema/dic/services', 'container');
$container->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$container->setAttribute('xsi:schemaLocation', 'http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd');
$this->addParameters($container);
$this->addServices($container);
$this->addInterfaceInjectors($container);
$this->document->appendChild($container);
$xml = $this->document->saveXML();
$this->document = null;
2011-01-14 16:00:43 +00:00
return $xml;
2010-01-04 14:26:20 +00:00
}
protected function addParameters(\DOMElement $parent)
{
$data = $this->container->getParameterBag()->all();
if (!$data) {
return;
}
if ($this->container->isFrozen()) {
$data = $this->escape($data);
}
$parameters = $this->document->createElement('parameters');
$parent->appendChild($parameters);
$this->convertParameters($data, 'parameter', $parameters);
2010-01-04 14:26:20 +00:00
}
protected function addMethodCalls(array $methodcalls, \DOMElement $parent)
{
foreach ($methodcalls as $methodcall) {
$call = $this->document->createElement('call');
$call->setAttribute('method', $methodcall[0]);
if (count($methodcall[1])) {
$this->convertParameters($methodcall[1], 'argument', $call);
}
$parent->appendChild($call);
}
}
protected function addInterfaceInjector(InterfaceInjector $injector, \DOMElement $parent)
{
$interface = $this->document->createElement('interface');
$interface->setAttribute('class', $injector->getClass());
$this->addMethodCalls($injector->getMethodCalls(), $interface);
$parent->appendChild($interface);
}
protected function addInterfaceInjectors(\DOMElement $parent)
{
if (!$this->container->getInterfaceInjectors()) {
return;
}
$interfaces = $this->document->createElement('interfaces');
foreach ($this->container->getInterfaceInjectors() as $injector) {
$this->addInterfaceInjector($injector, $interfaces);
}
$parent->appendChild($interfaces);
}
protected function addService($definition, $id, \DOMElement $parent)
2010-01-04 14:26:20 +00:00
{
$service = $this->document->createElement('service');
2011-01-14 16:00:43 +00:00
if (null !== $id) {
$service->setAttribute('id', $id);
2011-01-14 16:00:43 +00:00
}
if ($definition->getClass()) {
$service->setAttribute('class', $definition->getClass());
2011-01-14 16:00:43 +00:00
}
if ($definition->getFactoryMethod()) {
$service->setAttribute ('factory-method', $definition->getFactoryMethod());
2011-01-14 16:00:43 +00:00
}
if ($definition->getFactoryService()) {
$service->setAttribute ('factory-service', $definition->getFactoryService());
2011-01-14 16:00:43 +00:00
}
if (!$definition->isShared()) {
$service->setAttribute ('shared', 'false');
2011-01-14 16:00:43 +00:00
}
2010-08-05 06:34:53 +01:00
foreach ($definition->getTags() as $name => $tags) {
foreach ($tags as $attributes) {
$tag = $this->document->createElement('tag');
$tag->setAttribute('name', $name);
foreach ($attributes as $key => $value) {
$tag->setAttribute($key, $value);
}
$service->appendChild($tag);
}
}
2010-01-04 14:26:20 +00:00
if ($definition->getFile()) {
$file = $this->document->createElement('file', $definition->getFile());
$service->appendChild($file);
2010-01-04 14:26:20 +00:00
}
if ($parameters = $definition->getArguments()) {
$this->convertParameters($parameters, 'argument', $service);
2010-01-04 14:26:20 +00:00
}
$this->addMethodCalls($definition->getMethodCalls(), $service);
2010-01-04 14:26:20 +00:00
if ($callable = $definition->getConfigurator()) {
$configurator = $this->document->createElement('configurator');
if (is_array($callable)) {
$configurator->setAttribute((is_object($callable[0]) && $callable[0] instanceof Reference ? 'service' : 'class'), $callable[0]);
$configurator->setAttribute('method', $callable[1]);
} else {
$configurator->setAttribute('function', $callable);
}
$service->appendChild($configurator);
}
2010-01-04 14:26:20 +00:00
$parent->appendChild($service);
2010-01-04 14:26:20 +00:00
}
protected function addServiceAlias($alias, $id, \DOMElement $parent)
2010-01-04 14:26:20 +00:00
{
$service = $this->document->createElement('service');
$service->setAttribute('id', $alias);
$service->setAttribute('alias', $id);
if (!$id->isPublic()) {
$service->setAttribute('public', 'false');
2011-01-07 14:44:29 +00:00
}
$parent->appendChild($service);
2010-01-04 14:26:20 +00:00
}
protected function addServices(\DOMElement $parent)
2010-01-04 14:26:20 +00:00
{
$definitions = $this->container->getDefinitions();
if (!$definitions) {
return;
}
2010-01-04 14:26:20 +00:00
$services = $this->document->createElement('services');
foreach ($definitions as $id => $definition) {
$this->addService($definition, $id, $services);
}
2010-01-04 14:26:20 +00:00
foreach ($this->container->getAliases() as $alias => $id) {
$this->addServiceAlias($alias, $id, $services);
2010-01-04 14:26:20 +00:00
}
$parent->appendChild($services);
2010-01-04 14:26:20 +00:00
}
protected function convertParameters($parameters, $type, \DOMElement $parent)
{
$withKeys = array_keys($parameters) !== range(0, count($parameters) - 1);
foreach ($parameters as $key => $value) {
$element = $this->document->createElement($type);
if ($withKeys) {
$element->setAttribute('key', $key);
}
if (is_array($value)) {
$element->setAttribute('type', 'collection');
$this->convertParameters($value, $type, $element);
} else if (is_object($value) && $value instanceof Reference) {
$element->setAttribute('type', 'service');
$element->setAttribute('id', (string) $value);
$behaviour = $value->getInvalidBehavior();
if ($behaviour == ContainerInterface::NULL_ON_INVALID_REFERENCE)
$element->setAttribute('on-invalid', 'null');
else if ($behaviour == ContainerInterface::IGNORE_ON_INVALID_REFERENCE)
$element->setAttribute('on-invalid', 'ignore');
} else if (is_object($value) && $value instanceof Definition) {
$element->setAttribute('type', 'service');
$this->addService($value, null, $element);
} else {
if (in_array($value, array('null', 'true', 'false'), true)) {
$element->setAttribute('type', 'string');
}
$text = $this->document->createTextNode(self::phpToXml($value));
$element->appendChild($text);
}
$parent->appendChild($element);
}
2010-01-04 14:26:20 +00:00
}
protected function escape($arguments)
{
$args = array();
foreach ($arguments as $k => $v) {
if (is_array($v)) {
$args[$k] = $this->escape($v);
} elseif (is_string($v)) {
$args[$k] = str_replace('%', '%%', $v);
} else {
$args[$k] = $v;
}
}
return $args;
}
2010-01-04 14:26:20 +00:00
/**
2010-10-16 11:32:57 +01:00
* @throws \RuntimeException When trying to dump object or resource
*/
static public function phpToXml($value)
2010-01-04 14:26:20 +00:00
{
switch (true) {
case null === $value:
return 'null';
case true === $value:
return 'true';
case false === $value:
return 'false';
case is_object($value) && $value instanceof Parameter:
return '%'.$value.'%';
case is_object($value) || is_resource($value):
throw new \RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
default:
return (string) $value;
}
2010-01-04 14:26:20 +00:00
}
}