2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Components\DependencyInjection\Dumper;
|
|
|
|
|
2010-02-11 13:40:29 +00:00
|
|
|
use Symfony\Components\Yaml\Yaml;
|
2010-06-27 17:28:29 +01:00
|
|
|
use Symfony\Components\DependencyInjection\ContainerInterface;
|
2010-01-04 14:26:20 +00:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* YamlDumper dumps a service container as a YAML 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 YamlDumper extends Dumper
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Dumps the service container as an YAML string.
|
|
|
|
*
|
|
|
|
* @param array $options An array of options
|
|
|
|
*
|
|
|
|
* @return string A YAML string representing of the service container
|
|
|
|
*/
|
|
|
|
public function dump(array $options = array())
|
2010-02-09 08:45:23 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->addParameters()."\n".$this->addServices();
|
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 = " $id:\n";
|
|
|
|
$code .= sprintf(" class: %s\n", $definition->getClass());
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$annotationsCode = '';
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($definition->getAnnotations() as $name => $annotations) {
|
2010-05-08 14:32:30 +01:00
|
|
|
foreach ($annotations 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', Yaml::dump($key), Yaml::dump($value));
|
|
|
|
}
|
|
|
|
$att = $att ? ', '.implode(' ', $att) : '';
|
|
|
|
|
|
|
|
$annotationsCode .= sprintf(" - { name: %s%s }\n", Yaml::dump($name), $att);
|
|
|
|
}
|
|
|
|
}
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($annotationsCode) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$code .= " annotations:\n".$annotationsCode;
|
|
|
|
}
|
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\n", $definition->getFile());
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-07-05 08:47:14 +01:00
|
|
|
if ($definition->getFactoryMethod()) {
|
|
|
|
$code .= sprintf(" factory_method: %s\n", $definition->getFactoryMethod());
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2010-07-03 23:35:39 +01:00
|
|
|
if ($definition->getFactoryClass()) {
|
|
|
|
$code .= sprintf(" factoryClass: %s\n", $definition->getFactoryClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($definition->getFactoryService()) {
|
|
|
|
$code .= sprintf(" factoryService: %s\n", $definition->getFactoryService());
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($definition->getArguments()) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$code .= sprintf(" arguments: %s\n", Yaml::dump($this->dumpValue($definition->getArguments()), 0));
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($definition->getMethodCalls()) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$code .= sprintf(" calls:\n %s\n", str_replace("\n", "\n ", Yaml::dump($this->dumpValue($definition->getMethodCalls()), 1)));
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$definition->isShared()) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$code .= " shared: false\n";
|
|
|
|
}
|
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
|
|
|
$callable = array($this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$callable = array($callable[0], $callable[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$code .= sprintf(" configurator: %s\n", Yaml::dump($callable, 0));
|
|
|
|
}
|
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(" %s: @%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 = "services:\n";
|
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 $code;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function addParameters()
|
2010-01-04 14:26:20 +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-01-04 14:26:20 +00:00
|
|
|
|
2010-06-27 17:28:29 +01:00
|
|
|
return Yaml::dump(array('parameters' => $this->prepareParameters($this->container->getParameterBag()->all())), 2);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \RuntimeException When trying to dump object or resource
|
|
|
|
*/
|
|
|
|
protected function dumpValue($value)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (is_array($value)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$code = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($value as $k => $v) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$code[$k] = $this->dumpValue($v);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $code;
|
2010-05-07 15:09:11 +01:00
|
|
|
} elseif (is_object($value) && $value instanceof Reference) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->getServiceCall((string) $value, $value);
|
2010-05-07 15:09:11 +01:00
|
|
|
} elseif (is_object($value) && $value instanceof Parameter) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->getParameterCall((string) $value);
|
2010-05-07 15:09:11 +01:00
|
|
|
} elseif (is_object($value) || is_resource($value)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $value;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function getServiceCall($id, Reference $reference = null)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-06-27 17:28:29 +01:00
|
|
|
if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return sprintf('@@%s', $id);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
return sprintf('@%s', $id);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
protected function getParameterCall($id)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return sprintf('%%%s%%', $id);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function prepareParameters($parameters)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$filtered = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($parameters as $key => $value) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (is_array($value)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$value = $this->prepareParameters($value);
|
2010-05-07 15:09:11 +01:00
|
|
|
} elseif ($value instanceof Reference) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$value = '@'.$value;
|
|
|
|
}
|
|
|
|
|
|
|
|
$filtered[$key] = $value;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->escape($filtered);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function escape($arguments)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $args;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|