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/YamlDumper.php

279 lines
7.6 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\Yaml\Yaml;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2010-01-04 14:26:20 +00:00
/**
* YamlDumper dumps a service container as a YAML string.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @api
2010-01-04 14:26:20 +00:00
*/
class YamlDumper extends Dumper
{
/**
* 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
*
* @api
*/
public function dump(array $options = array())
{
return $this->addParameters()."\n".$this->addServices();
2010-01-04 14:26:20 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Adds a service
*
2011-03-10 14:31:00 +00:00
* @param string $id
* @param Definition $definition
2011-12-13 07:50:54 +00:00
*
2011-02-13 18:06:41 +00:00
* @return string
*/
private function addService($id, $definition)
2010-01-04 14:26:20 +00:00
{
$code = " $id:\n";
if ($definition->getClass()) {
$code .= sprintf(" class: %s\n", $definition->getClass());
}
2010-01-04 14:26:20 +00:00
2010-08-05 06:34:53 +01:00
$tagsCode = '';
foreach ($definition->getTags() as $name => $tags) {
foreach ($tags as $attributes) {
$att = array();
foreach ($attributes as $key => $value) {
$att[] = sprintf('%s: %s', Yaml::dump($key), Yaml::dump($value));
}
$att = $att ? ', '.implode(' ', $att) : '';
$tagsCode .= sprintf(" - { name: %s%s }\n", Yaml::dump($name), $att);
}
}
2010-08-05 06:34:53 +01:00
if ($tagsCode) {
$code .= " tags:\n".$tagsCode;
}
2010-01-04 14:26:20 +00:00
if ($definition->getFile()) {
$code .= sprintf(" file: %s\n", $definition->getFile());
}
2010-01-04 14:26:20 +00:00
if ($definition->getFactoryMethod()) {
$code .= sprintf(" factory_method: %s\n", $definition->getFactoryMethod());
2010-01-04 14:26:20 +00:00
}
if ($definition->getFactoryService()) {
$code .= sprintf(" factory_service: %s\n", $definition->getFactoryService());
}
if ($definition->getArguments()) {
$code .= sprintf(" arguments: %s\n", Yaml::dump($this->dumpValue($definition->getArguments()), 0));
2010-01-04 14:26:20 +00:00
}
2011-03-10 14:31:00 +00:00
if ($definition->getProperties()) {
$code .= sprintf(" properties: %s\n", Yaml::dump($this->dumpValue($definition->getProperties()), 0));
2011-03-10 14:31:00 +00:00
}
if ($definition->getMethodCalls()) {
$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
2011-01-17 22:28:59 +00:00
if (ContainerInterface::SCOPE_CONTAINER !== $scope = $definition->getScope()) {
$code .= sprintf(" scope: %s\n", $scope);
}
2010-01-04 14:26:20 +00:00
if ($callable = $definition->getConfigurator()) {
if (is_array($callable)) {
if (is_object($callable[0]) && $callable[0] instanceof Reference) {
$callable = array($this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]);
} else {
$callable = array($callable[0], $callable[1]);
}
}
$code .= sprintf(" configurator: %s\n", Yaml::dump($callable, 0));
}
2010-01-04 14:26:20 +00:00
return $code;
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
2011-12-13 07:50:54 +00:00
*
2011-06-04 16:30:56 +01:00
* @return string
2011-02-13 18:06:41 +00:00
*/
private function addServiceAlias($alias, $id)
2010-01-04 14:26:20 +00:00
{
2011-01-07 14:44:29 +00:00
if ($id->isPublic()) {
return sprintf(" %s: @%s\n", $alias, $id);
2011-01-07 14:44:29 +00:00
} else {
return sprintf(" %s:\n alias: %s\n public: false", $alias, $id);
2011-01-07 14:44:29 +00:00
}
2010-01-04 14:26:20 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Adds services
*
* @return string
*/
private function addServices()
2010-01-04 14:26:20 +00:00
{
if (!$this->container->getDefinitions()) {
return '';
}
2010-01-04 14:26:20 +00:00
$code = "services:\n";
foreach ($this->container->getDefinitions() as $id => $definition) {
$code .= $this->addService($id, $definition);
}
2010-01-04 14:26:20 +00:00
foreach ($this->container->getAliases() as $alias => $id) {
$code .= $this->addServiceAlias($alias, $id);
}
2010-01-04 14:26:20 +00:00
return $code;
}
2010-01-04 14:26:20 +00:00
2011-02-13 18:06:41 +00:00
/**
* Adds parameters
*
* @return string
*/
private function addParameters()
2010-01-04 14:26:20 +00:00
{
2010-06-27 17:28:29 +01:00
if (!$this->container->getParameterBag()->all()) {
return '';
}
2010-01-04 14:26:20 +00:00
if ($this->container->isFrozen()) {
$parameters = $this->prepareParameters($this->container->getParameterBag()->all());
} else {
$parameters = $this->container->getParameterBag()->all();
}
return Yaml::dump(array('parameters' => $parameters), 2);
2010-01-04 14:26:20 +00:00
}
/**
2011-02-13 18:06:41 +00:00
* Dumps the value to YAML format
*
* @param mixed $value
2011-12-13 07:50:54 +00:00
*
* @throws RuntimeException When trying to dump object or resource
*/
private function dumpValue($value)
2010-01-04 14:26:20 +00:00
{
if (is_array($value)) {
$code = array();
foreach ($value as $k => $v) {
$code[$k] = $this->dumpValue($v);
}
return $code;
} elseif (is_object($value) && $value instanceof Reference) {
return $this->getServiceCall((string) $value, $value);
} elseif (is_object($value) && $value instanceof Parameter) {
return $this->getParameterCall((string) $value);
} elseif (is_object($value) || is_resource($value)) {
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
}
return $value;
2010-01-04 14:26:20 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Gets the service call.
*
2011-04-23 16:05:44 +01:00
* @param string $id
2011-03-10 14:31:00 +00:00
* @param Reference $reference
2011-12-13 07:50:54 +00:00
*
2011-02-13 18:06:41 +00:00
* @return string
*/
private 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()) {
return sprintf('@?%s', $id);
}
return sprintf('@%s', $id);
2010-01-04 14:26:20 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Gets parameter call.
*
2011-03-10 14:31:00 +00:00
* @param string $id
2011-12-13 07:50:54 +00:00
*
2011-02-13 18:06:41 +00:00
* @return string
*/
private function getParameterCall($id)
2010-01-04 14:26:20 +00:00
{
return sprintf('%%%s%%', $id);
2010-01-04 14:26:20 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Prepares parameters
*
* @param array $parameters
2011-12-13 07:50:54 +00:00
*
2011-03-10 14:31:00 +00:00
* @return array
2011-02-13 18:06:41 +00:00
*/
private function prepareParameters($parameters)
2010-01-04 14:26:20 +00:00
{
$filtered = array();
foreach ($parameters as $key => $value) {
if (is_array($value)) {
$value = $this->prepareParameters($value);
} elseif ($value instanceof Reference) {
$value = '@'.$value;
}
$filtered[$key] = $value;
}
2010-01-04 14:26:20 +00:00
return $this->escape($filtered);
}
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-12-13 07:50:54 +00:00
*
2011-02-13 18:06:41 +00:00
* @return array
*/
private function escape($arguments)
2010-01-04 14:26:20 +00:00
{
$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;
}
}
2010-01-04 14:26:20 +00:00
return $args;
}
2010-01-04 14:26:20 +00:00
}