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->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->isSynchronized(false)) {
100 $code .= " synchronized: true\n";
103 if ($definition->isDeprecated()) {
104 $code .= sprintf(" deprecated: %s\n", $definition->getDeprecationMessage('%service_id%'));
107 if ($definition->isAutowired()) {
108 $code .= " autowire: true\n";
111 $autowiringTypesCode = '';
112 foreach ($definition->getAutowiringTypes() as $autowiringType) {
113 $autowiringTypesCode .= sprintf(" - %s\n", $this->dumper->dump($autowiringType));
115 if ($autowiringTypesCode) {
116 $code .= sprintf(" autowiring_types:\n%s", $autowiringTypesCode);
119 if ($definition->getFactoryClass(false)) {
120 $code .= sprintf(" factory_class: %s\n", $this->dumper->dump($definition->getFactoryClass(false)));
123 if ($definition->isLazy()) {
124 $code .= " lazy: true\n";
127 if ($definition->getFactoryMethod(false)) {
128 $code .= sprintf(" factory_method: %s\n", $this->dumper->dump($definition->getFactoryMethod(false)));
131 if ($definition->getFactoryService(false)) {
132 $code .= sprintf(" factory_service: %s\n", $this->dumper->dump($definition->getFactoryService(false)));
135 if ($definition->getArguments()) {
136 $code .= sprintf(" arguments: %s\n", $this->dumper->dump($this->dumpValue($definition->getArguments()), 0));
139 if ($definition->getProperties()) {
140 $code .= sprintf(" properties: %s\n", $this->dumper->dump($this->dumpValue($definition->getProperties()), 0));
143 if ($definition->getMethodCalls()) {
144 $code .= sprintf(" calls:\n%s\n", $this->dumper->dump($this->dumpValue($definition->getMethodCalls()), 1, 12));
147 if (!$definition->isShared()) {
148 $code .= " shared: false\n";
151 if (ContainerInterface::SCOPE_CONTAINER !== $scope = $definition->getScope(false)) {
152 $code .= sprintf(" scope: %s\n", $this->dumper->dump($scope));
155 if (null !== $decorated = $definition->getDecoratedService()) {
156 list($decorated, $renamedId, $priority) = $decorated;
157 $code .= sprintf(" decorates: %s\n", $decorated);
158 if (null !== $renamedId) {
159 $code .= sprintf(" decoration_inner_name: %s\n", $renamedId);
161 if (0 !== $priority) {
162 $code .= sprintf(" decoration_priority: %s\n", $priority);
166 if ($callable = $definition->getFactory()) {
167 $code .= sprintf(" factory: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
170 if ($callable = $definition->getConfigurator()) {
171 $code .= sprintf(" configurator: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
178 * Adds a service alias.
180 * @param string $alias
185 private function addServiceAlias($alias, $id)
187 if ($id->isPublic()) {
188 return sprintf(" %s: '@%s'\n", $alias, $id);
191 return sprintf(" %s:\n alias: %s\n public: false\n", $alias, $id);
199 private function addServices()
201 if (!$this->container->getDefinitions()) {
205 $code = "services:\n";
206 foreach ($this->container->getDefinitions() as $id => $definition) {
207 $code .= $this->addService($id, $definition);
210 $aliases = $this->container->getAliases();
211 foreach ($aliases as $alias => $id) {
212 while (isset($aliases[(string) $id])) {
213 $id = $aliases[(string) $id];
215 $code .= $this->addServiceAlias($alias, $id);
226 private function addParameters()
228 if (!$this->container->getParameterBag()->all()) {
232 $parameters = $this->prepareParameters($this->container->getParameterBag()->all(), $this->container->isFrozen());
234 return $this->dumper->dump(array('parameters' => $parameters), 2);
238 * Dumps callable to YAML format.
240 * @param callable $callable
244 private function dumpCallable($callable)
246 if (is_array($callable)) {
247 if ($callable[0] instanceof Reference) {
248 $callable = array($this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]);
250 $callable = array($callable[0], $callable[1]);
258 * Dumps the value to YAML format.
260 * @param mixed $value
264 * @throws RuntimeException When trying to dump object or resource
266 private function dumpValue($value)
268 if (is_array($value)) {
270 foreach ($value as $k => $v) {
271 $code[$k] = $this->dumpValue($v);
275 } elseif ($value instanceof Reference) {
276 return $this->getServiceCall((string) $value, $value);
277 } elseif ($value instanceof Parameter) {
278 return $this->getParameterCall((string) $value);
279 } elseif ($value instanceof Expression) {
280 return $this->getExpressionCall((string) $value);
281 } elseif (is_object($value) || is_resource($value)) {
282 throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
289 * Gets the service call.
292 * @param Reference $reference
296 private function getServiceCall($id, Reference $reference = null)
298 if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
299 return sprintf('@?%s', $id);
302 return sprintf('@%s', $id);
306 * Gets parameter call.
312 private function getParameterCall($id)
314 return sprintf('%%%s%%', $id);
317 private function getExpressionCall($expression)
319 return sprintf('@=%s', $expression);
323 * Prepares parameters.
325 * @param array $parameters
326 * @param bool $escape
330 private function prepareParameters(array $parameters, $escape = true)
333 foreach ($parameters as $key => $value) {
334 if (is_array($value)) {
335 $value = $this->prepareParameters($value, $escape);
336 } elseif ($value instanceof Reference || is_string($value) && 0 === strpos($value, '@')) {
340 $filtered[$key] = $value;
343 return $escape ? $this->escape($filtered) : $filtered;
349 * @param array $arguments
353 private function escape(array $arguments)
356 foreach ($arguments as $k => $v) {
358 $args[$k] = $this->escape($v);
359 } elseif (is_string($v)) {
360 $args[$k] = str_replace('%', '%%', $v);