2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Components\DependencyInjection\Dumper;
|
|
|
|
|
|
|
|
use Symfony\Components\DependencyInjection\Container;
|
|
|
|
use Symfony\Components\DependencyInjection\Parameter;
|
|
|
|
use Symfony\Components\DependencyInjection\Reference;
|
|
|
|
|
|
|
|
/*
|
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-04-07 01:51:29 +01:00
|
|
|
* @package Symfony
|
|
|
|
* @subpackage Components_DependencyInjection
|
2010-01-04 14:26:20 +00:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
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-05-06 12:25:53 +01:00
|
|
|
return $this->startXml().$this->addParameters().$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-05-06 12:25:53 +01:00
|
|
|
if (!$this->container->getParameters())
|
2010-02-09 08:45:23 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return '';
|
2010-02-09 08:45:23 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return sprintf(" <parameters>\n%s </parameters>\n", $this->convertParameters($this->escape($this->container->getParameters()), 'parameter', 4));
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function addService($id, $definition)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$code = sprintf(" <service id=\"%s\" class=\"%s\"%s%s>\n",
|
|
|
|
$id,
|
|
|
|
$definition->getClass(),
|
|
|
|
$definition->getConstructor() ? sprintf(' constructor="%s"', $definition->getConstructor()) : '',
|
|
|
|
!$definition->isShared() ? ' shared="false"' : ''
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($definition->getAnnotations() as $name => $annotations)
|
|
|
|
{
|
|
|
|
foreach ($annotations as $attributes)
|
|
|
|
{
|
|
|
|
$att = array();
|
|
|
|
foreach ($attributes as $key => $value)
|
|
|
|
{
|
|
|
|
$att[] = sprintf('%s="%s"', $key, $value);
|
|
|
|
}
|
|
|
|
$att = $att ? ' '.implode(' ', $att) : '';
|
|
|
|
|
|
|
|
$code .= sprintf(" <annotation name=\"%s\"%s />\n", $name, $att);
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
if ($definition->getFile())
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
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
|
|
|
|
|
|
|
if ($definition->getArguments())
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
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-06 12:25:53 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$code .= sprintf(" <call method=\"%s\" />\n", $call[0]);
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$code .= sprintf(" <configurator class=\"%s\" method=\"%s\" />\n", $callable[0], $callable[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$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-06 12:25:53 +01:00
|
|
|
if (!$this->container->getDefinitions())
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$code = '';
|
|
|
|
foreach ($this->container->getDefinitions() as $id => $definition)
|
|
|
|
{
|
|
|
|
$code .= $this->addService($id, $definition);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
foreach ($this->container->getAliases() as $alias => $id)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
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);
|
|
|
|
foreach ($parameters as $key => $value)
|
|
|
|
{
|
|
|
|
$attributes = '';
|
|
|
|
$key = $withKeys ? sprintf(' key="%s"', $key) : '';
|
|
|
|
if (is_array($value))
|
|
|
|
{
|
|
|
|
$value = "\n".$this->convertParameters($value, $type, $depth + 2).$white;
|
|
|
|
$attributes = ' type="collection"';
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (in_array($value, array('null', 'true', 'false'), true))
|
|
|
|
{
|
|
|
|
$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-06 12:25:53 +01:00
|
|
|
switch ($reference->getInvalidBehavior())
|
|
|
|
{
|
|
|
|
case Container::NULL_ON_INVALID_REFERENCE:
|
|
|
|
return 'on-invalid="null" ';
|
|
|
|
case Container::IGNORE_ON_INVALID_REFERENCE:
|
|
|
|
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();
|
|
|
|
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-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* @throws \RuntimeException When trying to dump object or ressource
|
|
|
|
*/
|
|
|
|
static public function phpToXml($value)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01: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 $value;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
}
|