Yaffs site version 1.1
[yaffs-website] / vendor / symfony / dependency-injection / Dumper / YamlDumper.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DependencyInjection\Dumper;
13
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;
22
23 /**
24  * YamlDumper dumps a service container as a YAML string.
25  *
26  * @author Fabien Potencier <fabien@symfony.com>
27  */
28 class YamlDumper extends Dumper
29 {
30     private $dumper;
31
32     /**
33      * Dumps the service container as an YAML string.
34      *
35      * @param array $options An array of options
36      *
37      * @return string A YAML string representing of the service container
38      */
39     public function dump(array $options = array())
40     {
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.');
43         }
44
45         if (null === $this->dumper) {
46             $this->dumper = new YmlDumper();
47         }
48
49         return $this->addParameters()."\n".$this->addServices();
50     }
51
52     /**
53      * Adds a service.
54      *
55      * @param string     $id
56      * @param Definition $definition
57      *
58      * @return string
59      */
60     private function addService($id, $definition)
61     {
62         $code = "    $id:\n";
63         if ($class = $definition->getClass()) {
64             if ('\\' === substr($class, 0, 1)) {
65                 $class = substr($class, 1);
66             }
67
68             $code .= sprintf("        class: %s\n", $this->dumper->dump($class));
69         }
70
71         if (!$definition->isPublic()) {
72             $code .= "        public: false\n";
73         }
74
75         $tagsCode = '';
76         foreach ($definition->getTags() as $name => $tags) {
77             foreach ($tags as $attributes) {
78                 $att = array();
79                 foreach ($attributes as $key => $value) {
80                     $att[] = sprintf('%s: %s', $this->dumper->dump($key), $this->dumper->dump($value));
81                 }
82                 $att = $att ? ', '.implode(', ', $att) : '';
83
84                 $tagsCode .= sprintf("            - { name: %s%s }\n", $this->dumper->dump($name), $att);
85             }
86         }
87         if ($tagsCode) {
88             $code .= "        tags:\n".$tagsCode;
89         }
90
91         if ($definition->getFile()) {
92             $code .= sprintf("        file: %s\n", $this->dumper->dump($definition->getFile()));
93         }
94
95         if ($definition->isSynthetic()) {
96             $code .= "        synthetic: true\n";
97         }
98
99         if ($definition->isSynchronized(false)) {
100             $code .= "        synchronized: true\n";
101         }
102
103         if ($definition->isDeprecated()) {
104             $code .= sprintf("        deprecated: %s\n", $definition->getDeprecationMessage('%service_id%'));
105         }
106
107         if ($definition->isAutowired()) {
108             $code .= "        autowire: true\n";
109         }
110
111         $autowiringTypesCode = '';
112         foreach ($definition->getAutowiringTypes() as $autowiringType) {
113             $autowiringTypesCode .= sprintf("            - %s\n", $this->dumper->dump($autowiringType));
114         }
115         if ($autowiringTypesCode) {
116             $code .= sprintf("        autowiring_types:\n%s", $autowiringTypesCode);
117         }
118
119         if ($definition->getFactoryClass(false)) {
120             $code .= sprintf("        factory_class: %s\n", $this->dumper->dump($definition->getFactoryClass(false)));
121         }
122
123         if ($definition->isLazy()) {
124             $code .= "        lazy: true\n";
125         }
126
127         if ($definition->getFactoryMethod(false)) {
128             $code .= sprintf("        factory_method: %s\n", $this->dumper->dump($definition->getFactoryMethod(false)));
129         }
130
131         if ($definition->getFactoryService(false)) {
132             $code .= sprintf("        factory_service: %s\n", $this->dumper->dump($definition->getFactoryService(false)));
133         }
134
135         if ($definition->getArguments()) {
136             $code .= sprintf("        arguments: %s\n", $this->dumper->dump($this->dumpValue($definition->getArguments()), 0));
137         }
138
139         if ($definition->getProperties()) {
140             $code .= sprintf("        properties: %s\n", $this->dumper->dump($this->dumpValue($definition->getProperties()), 0));
141         }
142
143         if ($definition->getMethodCalls()) {
144             $code .= sprintf("        calls:\n%s\n", $this->dumper->dump($this->dumpValue($definition->getMethodCalls()), 1, 12));
145         }
146
147         if (!$definition->isShared()) {
148             $code .= "        shared: false\n";
149         }
150
151         if (ContainerInterface::SCOPE_CONTAINER !== $scope = $definition->getScope(false)) {
152             $code .= sprintf("        scope: %s\n", $this->dumper->dump($scope));
153         }
154
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);
160             }
161             if (0 !== $priority) {
162                 $code .= sprintf("        decoration_priority: %s\n", $priority);
163             }
164         }
165
166         if ($callable = $definition->getFactory()) {
167             $code .= sprintf("        factory: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
168         }
169
170         if ($callable = $definition->getConfigurator()) {
171             $code .= sprintf("        configurator: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
172         }
173
174         return $code;
175     }
176
177     /**
178      * Adds a service alias.
179      *
180      * @param string $alias
181      * @param Alias  $id
182      *
183      * @return string
184      */
185     private function addServiceAlias($alias, $id)
186     {
187         if ($id->isPublic()) {
188             return sprintf("    %s: '@%s'\n", $alias, $id);
189         }
190
191         return sprintf("    %s:\n        alias: %s\n        public: false\n", $alias, $id);
192     }
193
194     /**
195      * Adds services.
196      *
197      * @return string
198      */
199     private function addServices()
200     {
201         if (!$this->container->getDefinitions()) {
202             return '';
203         }
204
205         $code = "services:\n";
206         foreach ($this->container->getDefinitions() as $id => $definition) {
207             $code .= $this->addService($id, $definition);
208         }
209
210         $aliases = $this->container->getAliases();
211         foreach ($aliases as $alias => $id) {
212             while (isset($aliases[(string) $id])) {
213                 $id = $aliases[(string) $id];
214             }
215             $code .= $this->addServiceAlias($alias, $id);
216         }
217
218         return $code;
219     }
220
221     /**
222      * Adds parameters.
223      *
224      * @return string
225      */
226     private function addParameters()
227     {
228         if (!$this->container->getParameterBag()->all()) {
229             return '';
230         }
231
232         $parameters = $this->prepareParameters($this->container->getParameterBag()->all(), $this->container->isFrozen());
233
234         return $this->dumper->dump(array('parameters' => $parameters), 2);
235     }
236
237     /**
238      * Dumps callable to YAML format.
239      *
240      * @param callable $callable
241      *
242      * @return callable
243      */
244     private function dumpCallable($callable)
245     {
246         if (is_array($callable)) {
247             if ($callable[0] instanceof Reference) {
248                 $callable = array($this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]);
249             } else {
250                 $callable = array($callable[0], $callable[1]);
251             }
252         }
253
254         return $callable;
255     }
256
257     /**
258      * Dumps the value to YAML format.
259      *
260      * @param mixed $value
261      *
262      * @return mixed
263      *
264      * @throws RuntimeException When trying to dump object or resource
265      */
266     private function dumpValue($value)
267     {
268         if (is_array($value)) {
269             $code = array();
270             foreach ($value as $k => $v) {
271                 $code[$k] = $this->dumpValue($v);
272             }
273
274             return $code;
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.');
283         }
284
285         return $value;
286     }
287
288     /**
289      * Gets the service call.
290      *
291      * @param string    $id
292      * @param Reference $reference
293      *
294      * @return string
295      */
296     private function getServiceCall($id, Reference $reference = null)
297     {
298         if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
299             return sprintf('@?%s', $id);
300         }
301
302         return sprintf('@%s', $id);
303     }
304
305     /**
306      * Gets parameter call.
307      *
308      * @param string $id
309      *
310      * @return string
311      */
312     private function getParameterCall($id)
313     {
314         return sprintf('%%%s%%', $id);
315     }
316
317     private function getExpressionCall($expression)
318     {
319         return sprintf('@=%s', $expression);
320     }
321
322     /**
323      * Prepares parameters.
324      *
325      * @param array $parameters
326      * @param bool  $escape
327      *
328      * @return array
329      */
330     private function prepareParameters(array $parameters, $escape = true)
331     {
332         $filtered = array();
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, '@')) {
337                 $value = '@'.$value;
338             }
339
340             $filtered[$key] = $value;
341         }
342
343         return $escape ? $this->escape($filtered) : $filtered;
344     }
345
346     /**
347      * Escapes arguments.
348      *
349      * @param array $arguments
350      *
351      * @return array
352      */
353     private function escape(array $arguments)
354     {
355         $args = array();
356         foreach ($arguments as $k => $v) {
357             if (is_array($v)) {
358                 $args[$k] = $this->escape($v);
359             } elseif (is_string($v)) {
360                 $args[$k] = str_replace('%', '%%', $v);
361             } else {
362                 $args[$k] = $v;
363             }
364         }
365
366         return $args;
367     }
368 }