4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Dumper;
14 use Symfony\Component\Yaml\Dumper as YmlDumper;
15 use Symfony\Component\DependencyInjection\Alias;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17 use Symfony\Component\DependencyInjection\Definition;
18 use Symfony\Component\DependencyInjection\Parameter;
19 use Symfony\Component\DependencyInjection\Reference;
20 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
21 use Symfony\Component\ExpressionLanguage\Expression;
24 * YamlDumper dumps a service container as a YAML string.
26 * @author Fabien Potencier <fabien@symfony.com>
28 class YamlDumper extends Dumper
33 * Dumps the service container as an YAML string.
35 * @param array $options An array of options
37 * @return string A YAML string representing of the service container
39 public function dump(array $options = array())
41 if (!class_exists('Symfony\Component\Yaml\Dumper')) {
42 throw new RuntimeException('Unable to dump the container as the Symfony Yaml Component is not installed.');
45 if (null === $this->dumper) {
46 $this->dumper = new YmlDumper();
49 return $this->container->resolveEnvPlaceholders($this->addParameters()."\n".$this->addServices());
56 * @param Definition $definition
60 private function addService($id, $definition)
63 if ($class = $definition->getClass()) {
64 if ('\\' === substr($class, 0, 1)) {
65 $class = substr($class, 1);
68 $code .= sprintf(" class: %s\n", $this->dumper->dump($class));
71 if (!$definition->isPublic()) {
72 $code .= " public: false\n";
76 foreach ($definition->getTags() as $name => $tags) {
77 foreach ($tags as $attributes) {
79 foreach ($attributes as $key => $value) {
80 $att[] = sprintf('%s: %s', $this->dumper->dump($key), $this->dumper->dump($value));
82 $att = $att ? ', '.implode(', ', $att) : '';
84 $tagsCode .= sprintf(" - { name: %s%s }\n", $this->dumper->dump($name), $att);
88 $code .= " tags:\n".$tagsCode;
91 if ($definition->getFile()) {
92 $code .= sprintf(" file: %s\n", $this->dumper->dump($definition->getFile()));
95 if ($definition->isSynthetic()) {
96 $code .= " synthetic: true\n";
99 if ($definition->isDeprecated()) {
100 $code .= sprintf(" deprecated: %s\n", $definition->getDeprecationMessage('%service_id%'));
103 if ($definition->isAutowired()) {
104 $code .= " autowire: true\n";
107 $autowiringTypesCode = '';
108 foreach ($definition->getAutowiringTypes() as $autowiringType) {
109 $autowiringTypesCode .= sprintf(" - %s\n", $this->dumper->dump($autowiringType));
111 if ($autowiringTypesCode) {
112 $code .= sprintf(" autowiring_types:\n%s", $autowiringTypesCode);
115 if ($definition->isLazy()) {
116 $code .= " lazy: true\n";
119 if ($definition->getArguments()) {
120 $code .= sprintf(" arguments: %s\n", $this->dumper->dump($this->dumpValue($definition->getArguments()), 0));
123 if ($definition->getProperties()) {
124 $code .= sprintf(" properties: %s\n", $this->dumper->dump($this->dumpValue($definition->getProperties()), 0));
127 if ($definition->getMethodCalls()) {
128 $code .= sprintf(" calls:\n%s\n", $this->dumper->dump($this->dumpValue($definition->getMethodCalls()), 1, 12));
131 if (!$definition->isShared()) {
132 $code .= " shared: false\n";
135 if (null !== $decorated = $definition->getDecoratedService()) {
136 list($decorated, $renamedId, $priority) = $decorated;
137 $code .= sprintf(" decorates: %s\n", $decorated);
138 if (null !== $renamedId) {
139 $code .= sprintf(" decoration_inner_name: %s\n", $renamedId);
141 if (0 !== $priority) {
142 $code .= sprintf(" decoration_priority: %s\n", $priority);
146 if ($callable = $definition->getFactory()) {
147 $code .= sprintf(" factory: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
150 if ($callable = $definition->getConfigurator()) {
151 $code .= sprintf(" configurator: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
158 * Adds a service alias.
160 * @param string $alias
165 private function addServiceAlias($alias, $id)
167 if ($id->isPublic()) {
168 return sprintf(" %s: '@%s'\n", $alias, $id);
171 return sprintf(" %s:\n alias: %s\n public: false\n", $alias, $id);
179 private function addServices()
181 if (!$this->container->getDefinitions()) {
185 $code = "services:\n";
186 foreach ($this->container->getDefinitions() as $id => $definition) {
187 $code .= $this->addService($id, $definition);
190 $aliases = $this->container->getAliases();
191 foreach ($aliases as $alias => $id) {
192 while (isset($aliases[(string) $id])) {
193 $id = $aliases[(string) $id];
195 $code .= $this->addServiceAlias($alias, $id);
206 private function addParameters()
208 if (!$this->container->getParameterBag()->all()) {
212 $parameters = $this->prepareParameters($this->container->getParameterBag()->all(), $this->container->isFrozen());
214 return $this->dumper->dump(array('parameters' => $parameters), 2);
218 * Dumps callable to YAML format.
220 * @param callable $callable
224 private function dumpCallable($callable)
226 if (is_array($callable)) {
227 if ($callable[0] instanceof Reference) {
228 $callable = array($this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]);
230 $callable = array($callable[0], $callable[1]);
238 * Dumps the value to YAML format.
240 * @param mixed $value
244 * @throws RuntimeException When trying to dump object or resource
246 private function dumpValue($value)
248 if (is_array($value)) {
250 foreach ($value as $k => $v) {
251 $code[$k] = $this->dumpValue($v);
255 } elseif ($value instanceof Reference) {
256 return $this->getServiceCall((string) $value, $value);
257 } elseif ($value instanceof Parameter) {
258 return $this->getParameterCall((string) $value);
259 } elseif ($value instanceof Expression) {
260 return $this->getExpressionCall((string) $value);
261 } elseif (is_object($value) || is_resource($value)) {
262 throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
269 * Gets the service call.
272 * @param Reference $reference
276 private function getServiceCall($id, Reference $reference = null)
278 if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
279 return sprintf('@?%s', $id);
282 return sprintf('@%s', $id);
286 * Gets parameter call.
292 private function getParameterCall($id)
294 return sprintf('%%%s%%', $id);
297 private function getExpressionCall($expression)
299 return sprintf('@=%s', $expression);
303 * Prepares parameters.
305 * @param array $parameters
306 * @param bool $escape
310 private function prepareParameters(array $parameters, $escape = true)
313 foreach ($parameters as $key => $value) {
314 if (is_array($value)) {
315 $value = $this->prepareParameters($value, $escape);
316 } elseif ($value instanceof Reference || is_string($value) && 0 === strpos($value, '@')) {
320 $filtered[$key] = $value;
323 return $escape ? $this->escape($filtered) : $filtered;
329 * @param array $arguments
333 private function escape(array $arguments)
336 foreach ($arguments as $k => $v) {
338 $args[$k] = $this->escape($v);
339 } elseif (is_string($v)) {
340 $args[$k] = str_replace('%', '%%', $v);