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

301 lines
9.7 KiB
PHP
Raw Normal View History

2010-01-04 14:26:20 +00:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
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;
2010-01-04 14:26:20 +00:00
/**
* XmlDumper dumps a service container as an XML string.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Martin Hasoň <martin.hason@gmail.com>
2010-01-04 14:26:20 +00:00
*/
class XmlDumper extends Dumper
{
/**
* @var \DOMDocument
*/
private $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://symfony.com/schema/dic/services', 'container');
$container->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$container->setAttribute('xsi:schemaLocation', 'http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd');
$this->addParameters($container);
$this->addServices($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
}
2011-02-13 18:06:41 +00:00
/**
* Adds parameters.
*
2011-03-10 14:31:00 +00:00
* @param DOMElement $parent
2011-02-13 18:06:41 +00:00
* @return void
*/
private 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
}
2011-02-13 18:06:41 +00:00
/**
* Adds method calls.
*
2011-03-10 14:31:00 +00:00
* @param array $methodcalls
* @param DOMElement $parent
2011-02-13 18:06:41 +00:00
* @return void
*/
private 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);
}
}
2011-02-13 18:06:41 +00:00
/**
* Adds a service.
*
2011-03-10 14:31:00 +00:00
* @param Definition $definition
* @param string $id
* @param DOMElement $parent
2011-02-13 18:06:41 +00:00
* @return void
*/
private 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()) {
2011-02-27 19:56:29 +00:00
$service->setAttribute('factory-method', $definition->getFactoryMethod());
2011-01-14 16:00:43 +00:00
}
if ($definition->getFactoryService()) {
2011-02-27 19:56:29 +00:00
$service->setAttribute('factory-service', $definition->getFactoryService());
2011-01-14 16:00:43 +00:00
}
2011-01-17 22:28:59 +00:00
if (ContainerInterface::SCOPE_CONTAINER !== $scope = $definition->getScope()) {
2011-02-27 19:56:29 +00:00
$service->setAttribute('scope', $scope);
2011-01-14 16:00:43 +00:00
}
if (!$definition->isPublic()) {
$service->setAttribute('public', 'false');
}
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
}
2011-03-10 14:31:00 +00:00
if ($parameters = $definition->getProperties()) {
$this->convertParameters($parameters, 'property', $service, 'name');
}
$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
}
2011-02-13 18:06:41 +00:00
/**
* Adds a service alias.
*
2011-03-10 14:31:00 +00:00
* @param string $alias
* @param string $id
* @param DOMElement $parent
2011-02-13 18:06:41 +00:00
* @return void
*/
private 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
}
2011-02-13 18:06:41 +00:00
/**
* Adds services.
*
2011-03-10 14:31:00 +00:00
* @param DOMElement $parent
2011-02-13 18:06:41 +00:00
* @return void
*/
private 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
}
2011-02-13 18:06:41 +00:00
/**
* Converts parameters.
*
* @param array $parameters
2011-03-10 14:31:00 +00:00
* @param string $type
* @param DOMElement $parent
2011-02-13 18:06:41 +00:00
* @return void
*/
private function convertParameters($parameters, $type, \DOMElement $parent, $keyAttribute = 'key')
{
$withKeys = array_keys($parameters) !== range(0, count($parameters) - 1);
foreach ($parameters as $key => $value) {
$element = $this->document->createElement($type);
if ($withKeys) {
2011-03-10 14:31:00 +00:00
$element->setAttribute($keyAttribute, $key);
}
if (is_array($value)) {
$element->setAttribute('type', 'collection');
2011-03-10 14:31:00 +00:00
$this->convertParameters($value, $type, $element, 'key');
} else if (is_object($value) && $value instanceof Reference) {
$element->setAttribute('type', 'service');
$element->setAttribute('id', (string) $value);
$behaviour = $value->getInvalidBehavior();
2011-01-18 15:41:21 +00:00
if ($behaviour == ContainerInterface::NULL_ON_INVALID_REFERENCE) {
$element->setAttribute('on-invalid', 'null');
2011-01-18 15:41:21 +00:00
} else if ($behaviour == ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
$element->setAttribute('on-invalid', 'ignore');
2011-01-18 15:41:21 +00:00
}
} 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
}
2011-02-13 18:06:41 +00:00
/**
* Escapes arguments
*
2011-03-10 14:31:00 +00:00
* @param array $arguments
2011-02-13 18:06:41 +00:00
* @return array
*/
private 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
/**
2011-02-13 18:06:41 +00:00
* Converts php types to xml types.
*
* @param mixed $value Value to convert
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
}
}