2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\DependencyInjection\Dumper;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
use Symfony\Component\DependencyInjection\Parameter;
|
|
|
|
use Symfony\Component\DependencyInjection\Reference;
|
2010-10-02 15:47:35 +01:00
|
|
|
use Symfony\Component\DependencyInjection\InterfaceInjector;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01: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.
|
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-01-04 14:26:20 +00:00
|
|
|
*/
|
|
|
|
class XmlDumper extends Dumper
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2010-10-02 15:47:35 +01:00
|
|
|
return $this->startXml().$this->addParameters().$this->addInterfaceInjectors().$this->addServices().$this->endXml();
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function addParameters()
|
2010-02-09 08:45:23 +00:00
|
|
|
{
|
2010-06-27 17:28:29 +01:00
|
|
|
if (!$this->container->getParameterBag()->all()) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return '';
|
2010-02-09 08:45:23 +00:00
|
|
|
}
|
|
|
|
|
2010-07-16 08:15:22 +01:00
|
|
|
if ($this->container->isFrozen()) {
|
|
|
|
$parameters = $this->escape($this->container->getParameterBag()->all());
|
|
|
|
} else {
|
|
|
|
$parameters = $this->container->getParameterBag()->all();
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf(" <parameters>\n%s </parameters>\n", $this->convertParameters($parameters, 'parameter', 4));
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-10-02 15:47:35 +01:00
|
|
|
protected function addInterfaceInjector(InterfaceInjector $injector)
|
|
|
|
{
|
|
|
|
$code = \sprintf(" <interface class=\"%s\">\n", $injector->getClass());
|
|
|
|
|
|
|
|
foreach ($injector->getMethodCalls() as $call) {
|
|
|
|
if (count($call[1])) {
|
|
|
|
$code .= sprintf(" <call method=\"%s\">\n%s </call>\n", $call[0], $this->convertParameters($call[1], 'argument', 8));
|
|
|
|
} else {
|
|
|
|
$code .= sprintf(" <call method=\"%s\" />\n", $call[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$code .= " </interface>\n";
|
|
|
|
|
|
|
|
return $code;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function addInterfaceInjectors()
|
|
|
|
{
|
|
|
|
if (!$this->container->getInterfaceInjectors()) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$code = '';
|
|
|
|
foreach ($this->container->getInterfaceInjectors() as $injector) {
|
|
|
|
$code .= $this->addInterfaceInjector($injector);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf(" <interfaces>\n%s </interfaces>\n", $code);
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function addService($id, $definition)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-07-04 17:56:48 +01:00
|
|
|
$code = sprintf(" <service id=\"%s\"%s%s%s%s>\n",
|
2010-05-06 12:25:53 +01:00
|
|
|
$id,
|
2010-07-04 17:56:48 +01:00
|
|
|
$definition->getClass() ? sprintf(' class="%s"', $definition->getClass()) : '',
|
2010-07-05 08:47:14 +01:00
|
|
|
$definition->getFactoryMethod() ? sprintf(' factory-method="%s"', $definition->getFactoryMethod()) : '',
|
2010-07-04 17:56:48 +01:00
|
|
|
$definition->getFactoryService() ? sprintf(' factory-service="%s"', $definition->getFactoryService()) : '',
|
2010-05-06 12:25:53 +01:00
|
|
|
!$definition->isShared() ? ' shared="false"' : ''
|
|
|
|
);
|
|
|
|
|
2010-08-05 06:34:53 +01:00
|
|
|
foreach ($definition->getTags() as $name => $tags) {
|
|
|
|
foreach ($tags as $attributes) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$att = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($attributes as $key => $value) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$att[] = sprintf('%s="%s"', $key, $value);
|
|
|
|
}
|
|
|
|
$att = $att ? ' '.implode(' ', $att) : '';
|
|
|
|
|
2010-08-05 06:34:53 +01:00
|
|
|
$code .= sprintf(" <tag name=\"%s\"%s />\n", $name, $att);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($definition->getFile()) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$code .= sprintf(" <file>%s</file>\n", $definition->getFile());
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($definition->getArguments()) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$code .= $this->convertParameters($definition->getArguments(), 'argument', 6);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($definition->getMethodCalls() as $call) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (count($call[1])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$code .= sprintf(" <call method=\"%s\">\n%s </call>\n", $call[0], $this->convertParameters($call[1], 'argument', 8));
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$code .= sprintf(" <call method=\"%s\" />\n", $call[0]);
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($callable = $definition->getConfigurator()) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (is_array($callable)) {
|
2010-05-07 15:09:11 +01:00
|
|
|
if (is_object($callable[0]) && $callable[0] instanceof Reference) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$code .= sprintf(" <configurator service=\"%s\" method=\"%s\" />\n", $callable[0], $callable[1]);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$code .= sprintf(" <configurator class=\"%s\" method=\"%s\" />\n", $callable[0], $callable[1]);
|
|
|
|
}
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$code .= sprintf(" <configurator function=\"%s\" />\n", $callable);
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$code .= " </service>\n";
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $code;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function addServiceAlias($alias, $id)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return sprintf(" <service id=\"%s\" alias=\"%s\" />\n", $alias, $id);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function addServices()
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$this->container->getDefinitions()) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return '';
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$code = '';
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($this->container->getDefinitions() as $id => $definition) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$code .= $this->addService($id, $definition);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($this->container->getAliases() as $alias => $id) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$code .= $this->addServiceAlias($alias, $id);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return sprintf(" <services>\n%s </services>\n", $code);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function convertParameters($parameters, $type='parameter', $depth = 2)
|
|
|
|
{
|
|
|
|
$white = str_repeat(' ', $depth);
|
|
|
|
$xml = '';
|
|
|
|
$withKeys = array_keys($parameters) !== range(0, count($parameters) - 1);
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($parameters as $key => $value) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$attributes = '';
|
|
|
|
$key = $withKeys ? sprintf(' key="%s"', $key) : '';
|
2010-05-07 15:09:11 +01:00
|
|
|
if (is_array($value)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$value = "\n".$this->convertParameters($value, $type, $depth + 2).$white;
|
|
|
|
$attributes = ' type="collection"';
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (is_object($value) && $value instanceof Reference) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$xml .= sprintf("%s<%s%s type=\"service\" id=\"%s\" %s/>\n", $white, $type, $key, (string) $value, $this->getXmlInvalidBehavior($value));
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (in_array($value, array('null', 'true', 'false'), true)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$attributes = ' type="string"';
|
|
|
|
}
|
|
|
|
|
|
|
|
$xml .= sprintf("%s<%s%s%s>%s</%s>\n", $white, $type, $key, $attributes, self::phpToXml($value), $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $xml;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function startXml()
|
|
|
|
{
|
|
|
|
return <<<EOF
|
2010-01-04 14:26:20 +00:00
|
|
|
<?xml version="1.0" ?>
|
|
|
|
|
2010-01-15 14:53:07 +00:00
|
|
|
<container xmlns="http://www.symfony-project.org/schema/dic/services"
|
2010-01-11 14:56:49 +00:00
|
|
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
2010-01-15 14:53:07 +00:00
|
|
|
xsi:schemaLocation="http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd">
|
2010-01-04 14:26:20 +00:00
|
|
|
|
|
|
|
EOF;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function endXml()
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return "</container>\n";
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function getXmlInvalidBehavior(Reference $reference)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
switch ($reference->getInvalidBehavior()) {
|
2010-06-27 17:28:29 +01:00
|
|
|
case ContainerInterface::NULL_ON_INVALID_REFERENCE:
|
2010-05-06 12:25:53 +01:00
|
|
|
return 'on-invalid="null" ';
|
2010-06-27 17:28:29 +01:00
|
|
|
case ContainerInterface::IGNORE_ON_INVALID_REFERENCE:
|
2010-05-06 12:25:53 +01:00
|
|
|
return 'on-invalid="ignore" ';
|
|
|
|
default:
|
|
|
|
return '';
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function escape($arguments)
|
|
|
|
{
|
|
|
|
$args = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($arguments as $k => $v) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (is_array($v)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$args[$k] = $this->escape($v);
|
2010-05-07 15:09:11 +01:00
|
|
|
} elseif (is_string($v)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$args[$k] = str_replace('%', '%%', $v);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$args[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $args;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2010-10-16 11:32:57 +01:00
|
|
|
* @throws \RuntimeException When trying to dump object or resource
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
static public function phpToXml($value)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
switch (true) {
|
2010-05-06 12:25:53 +01:00
|
|
|
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 $value;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
}
|