123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- namespace Symfony\Components\DependencyInjection\Dumper;
- use Symfony\Components\YAML\YAML;
- use Symfony\Components\DependencyInjection\Container;
- use Symfony\Components\DependencyInjection\Parameter;
- use Symfony\Components\DependencyInjection\Reference;
- /*
- * This file is part of the symfony framework.
- *
- * (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.
- *
- * @package symfony
- * @subpackage dependency_injection
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
- 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
- */
- public function dump(array $options = array())
- {
- return $this->addParameters()."\n".$this->addServices();
- }
- protected function addService($id, $definition)
- {
- $code = " $id:\n";
- $code .= sprintf(" class: %s\n", $definition->getClass());
- if ($definition->getFile())
- {
- $code .= sprintf(" file: %s\n", $definition->getFile());
- }
- if ($definition->getConstructor())
- {
- $code .= sprintf(" constructor: %s\n", $definition->getConstructor());
- }
- if ($definition->getArguments())
- {
- $code .= sprintf(" arguments: %s\n", YAML::dump($this->dumpValue($definition->getArguments()), 0));
- }
- if ($definition->getMethodCalls())
- {
- $code .= sprintf(" calls:\n %s\n", str_replace("\n", "\n ", YAML::dump($this->dumpValue($definition->getMethodCalls()), 1)));
- }
- if (!$definition->isShared())
- {
- $code .= " shared: false\n";
- }
- 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));
- }
- return $code;
- }
- protected function addServiceAlias($alias, $id)
- {
- return sprintf(" %s: @%s\n", $alias, $id);
- }
- protected function addServices()
- {
- if (!$this->container->getDefinitions())
- {
- return '';
- }
- $code = "services:\n";
- foreach ($this->container->getDefinitions() as $id => $definition)
- {
- $code .= $this->addService($id, $definition);
- }
- foreach ($this->container->getAliases() as $alias => $id)
- {
- $code .= $this->addServiceAlias($alias, $id);
- }
- return $code;
- }
- protected function addParameters()
- {
- if (!$this->container->getParameters())
- {
- return '';
- }
- return YAML::dump(array('parameters' => $this->prepareParameters($this->container->getParameters())), 2);
- }
- protected function dumpValue($value)
- {
- 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.');
- }
- else
- {
- return $value;
- }
- }
- protected function getServiceCall($id, Reference $reference = null)
- {
- if (null !== $reference && Container::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior())
- {
- return sprintf('@@%s', $id);
- }
- else
- {
- return sprintf('@%s', $id);
- }
- }
- protected function getParameterCall($id)
- {
- return sprintf('%%%s%%', $id);
- }
- protected function prepareParameters($parameters)
- {
- $filtered = array();
- foreach ($parameters as $key => $value)
- {
- if (is_array($value))
- {
- $value = $this->prepareParameters($value);
- }
- elseif ($value instanceof Reference)
- {
- $value = '@'.$value;
- }
- $filtered[$key] = $value;
- }
- return $this->escape($filtered);
- }
- 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;
- }
- }
|