2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\DependencyInjection\Dumper;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2012-05-01 13:46:26 +01:00
|
|
|
use Symfony\Component\Yaml\Dumper as YmlDumper;
|
2012-09-07 23:56:45 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Alias;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
2012-09-07 23:56:45 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Definition;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Parameter;
|
|
|
|
use Symfony\Component\DependencyInjection\Reference;
|
2011-12-04 23:51:22 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
2012-05-01 13:46:26 +01:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* YamlDumper dumps a service container as a YAML string.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-01-04 14:26:20 +00:00
|
|
|
*/
|
|
|
|
class YamlDumper extends Dumper
|
|
|
|
{
|
2012-05-01 13:46:26 +01:00
|
|
|
private $dumper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param ContainerBuilder $container The service container to dump
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
|
|
|
public function __construct(ContainerBuilder $container)
|
|
|
|
{
|
|
|
|
parent::__construct($container);
|
|
|
|
|
|
|
|
$this->dumper = new YmlDumper();
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Dumps the service container as an YAML string.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param array $options An array of options
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return string A YAML string representing of the service container
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function dump(array $options = array())
|
2010-02-09 08:45:23 +00:00
|
|
|
{
|
2011-04-23 10:42:31 +01:00
|
|
|
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
|
|
|
|
*
|
2012-05-18 18:41:48 +01:00
|
|
|
* @param string $id
|
2011-03-10 14:31:00 +00:00
|
|
|
* @param Definition $definition
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-02-13 18:06:41 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private function addService($id, $definition)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-09-23 07:05:09 +01:00
|
|
|
$code = " $id:\n";
|
2010-07-04 17:56:48 +01:00
|
|
|
if ($definition->getClass()) {
|
2011-09-23 07:05:09 +01:00
|
|
|
$code .= sprintf(" class: %s\n", $definition->getClass());
|
2010-07-04 17:56:48 +01:00
|
|
|
}
|
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) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$att = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($attributes as $key => $value) {
|
2012-05-01 13:46:26 +01:00
|
|
|
$att[] = sprintf('%s: %s', $this->dumper->dump($key), $this->dumper->dump($value));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
$att = $att ? ', '.implode(' ', $att) : '';
|
|
|
|
|
2012-05-01 13:46:26 +01:00
|
|
|
$tagsCode .= sprintf(" - { name: %s%s }\n", $this->dumper->dump($name), $att);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
2010-08-05 06:34:53 +01:00
|
|
|
if ($tagsCode) {
|
2011-09-23 07:05:09 +01:00
|
|
|
$code .= " tags:\n".$tagsCode;
|
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()) {
|
2011-09-23 07:05:09 +01:00
|
|
|
$code .= sprintf(" file: %s\n", $definition->getFile());
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2013-02-06 20:42:01 +00:00
|
|
|
if ($definition->isSynthetic()) {
|
|
|
|
$code .= sprintf(" synthetic: true\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($definition->isSynchronized()) {
|
|
|
|
$code .= sprintf(" synchronized: true\n");
|
|
|
|
}
|
|
|
|
|
2013-04-08 02:18:40 +01:00
|
|
|
if ($definition->getFactoryClass()) {
|
|
|
|
$code .= sprintf(" factory_class: %s\n", $definition->getFactoryClass());
|
|
|
|
}
|
|
|
|
|
2013-03-29 23:21:12 +00:00
|
|
|
if ($definition->isLazy()) {
|
|
|
|
$code .= sprintf(" lazy: true\n");
|
|
|
|
}
|
|
|
|
|
2010-07-05 08:47:14 +01:00
|
|
|
if ($definition->getFactoryMethod()) {
|
2011-09-23 07:05:09 +01:00
|
|
|
$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->getFactoryService()) {
|
2011-09-23 07:05:09 +01:00
|
|
|
$code .= sprintf(" factory_service: %s\n", $definition->getFactoryService());
|
2010-07-03 23:35:39 +01:00
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($definition->getArguments()) {
|
2012-05-01 13:46:26 +01:00
|
|
|
$code .= sprintf(" arguments: %s\n", $this->dumper->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()) {
|
2012-05-01 13:46:26 +01:00
|
|
|
$code .= sprintf(" properties: %s\n", $this->dumper->dump($this->dumpValue($definition->getProperties()), 0));
|
2011-03-10 14:31:00 +00:00
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($definition->getMethodCalls()) {
|
2012-05-01 13:46:26 +01:00
|
|
|
$code .= sprintf(" calls:\n%s\n", $this->dumper->dump($this->dumpValue($definition->getMethodCalls()), 1, 12));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2011-01-17 22:28:59 +00:00
|
|
|
if (ContainerInterface::SCOPE_CONTAINER !== $scope = $definition->getScope()) {
|
2011-09-23 07:05:09 +01:00
|
|
|
$code .= sprintf(" scope: %s\n", $scope);
|
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 ($callable = $definition->getConfigurator()) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (is_array($callable)) {
|
2012-11-18 20:27:35 +00:00
|
|
|
if ($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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-01 13:46:26 +01:00
|
|
|
$code .= sprintf(" configurator: %s\n", $this->dumper->dump($callable, 0));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Adds a service alias
|
|
|
|
*
|
2011-03-10 14:31:00 +00:00
|
|
|
* @param string $alias
|
2012-09-07 23:56:45 +01:00
|
|
|
* @param Alias $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
|
|
|
*/
|
2011-03-11 13:50:46 +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()) {
|
2011-09-23 07:05:09 +01:00
|
|
|
return sprintf(" %s: @%s\n", $alias, $id);
|
2011-01-07 14:44:29 +00:00
|
|
|
} else {
|
2011-09-23 07:05:09 +01:00
|
|
|
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
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private 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
|
|
|
|
2013-06-05 10:51:05 +01:00
|
|
|
$aliases = $this->container->getAliases();
|
|
|
|
foreach ($aliases as $alias => $id) {
|
|
|
|
while (isset($aliases[(string) $id])) {
|
|
|
|
$id = $aliases[(string) $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
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Adds parameters
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private 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
|
|
|
|
2013-01-17 21:13:26 +00:00
|
|
|
$parameters = $this->prepareParameters($this->container->getParameterBag()->all(), $this->container->isFrozen());
|
2010-07-16 08:15:22 +01:00
|
|
|
|
2012-05-01 13:46:26 +01:00
|
|
|
return $this->dumper->dump(array('parameters' => $parameters), 2);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01: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
|
|
|
*
|
2012-09-07 23:56:45 +01:00
|
|
|
* @return mixed
|
|
|
|
*
|
2011-12-04 23:51:22 +00:00
|
|
|
* @throws RuntimeException When trying to dump object or resource
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private 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;
|
2012-11-18 20:27:35 +00:00
|
|
|
} elseif ($value instanceof Reference) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->getServiceCall((string) $value, $value);
|
2012-11-18 20:27:35 +00:00
|
|
|
} elseif ($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)) {
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2011-02-27 17:27:22 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
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()) {
|
2010-11-15 09:00:59 +00:00
|
|
|
return sprintf('@?%s', $id);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2011-02-27 17:27:22 +00:00
|
|
|
|
|
|
|
return sprintf('@%s', $id);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01: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
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private 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
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
2013-04-21 13:26:17 +01:00
|
|
|
* Prepares parameters.
|
2011-02-13 18:06:41 +00:00
|
|
|
*
|
2013-04-21 13:26:17 +01:00
|
|
|
* @param array $parameters
|
|
|
|
* @param Boolean $escape
|
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
|
|
|
*/
|
2013-01-17 21:13:26 +00:00
|
|
|
private function prepareParameters($parameters, $escape = true)
|
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)) {
|
2013-01-17 21:13:26 +00:00
|
|
|
$value = $this->prepareParameters($value, $escape);
|
2013-03-08 15:38:09 +00:00
|
|
|
} elseif ($value instanceof Reference || is_string($value) && 0 === strpos($value, '@')) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$value = '@'.$value;
|
|
|
|
}
|
|
|
|
|
|
|
|
$filtered[$key] = $value;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2013-01-17 21:13:26 +00:00
|
|
|
return $escape ? $this->escape($filtered) : $filtered;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
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
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private 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
|
|
|
}
|