Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/dependency-injection/Dumper/YamlDumper.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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->container->resolveEnvPlaceholders($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->isDeprecated()) { | |
100 $code .= sprintf(" deprecated: %s\n", $definition->getDeprecationMessage('%service_id%')); | |
101 } | |
102 | |
103 if ($definition->isAutowired()) { | |
104 $code .= " autowire: true\n"; | |
105 } | |
106 | |
107 $autowiringTypesCode = ''; | |
108 foreach ($definition->getAutowiringTypes() as $autowiringType) { | |
109 $autowiringTypesCode .= sprintf(" - %s\n", $this->dumper->dump($autowiringType)); | |
110 } | |
111 if ($autowiringTypesCode) { | |
112 $code .= sprintf(" autowiring_types:\n%s", $autowiringTypesCode); | |
113 } | |
114 | |
115 if ($definition->isLazy()) { | |
116 $code .= " lazy: true\n"; | |
117 } | |
118 | |
119 if ($definition->getArguments()) { | |
120 $code .= sprintf(" arguments: %s\n", $this->dumper->dump($this->dumpValue($definition->getArguments()), 0)); | |
121 } | |
122 | |
123 if ($definition->getProperties()) { | |
124 $code .= sprintf(" properties: %s\n", $this->dumper->dump($this->dumpValue($definition->getProperties()), 0)); | |
125 } | |
126 | |
127 if ($definition->getMethodCalls()) { | |
128 $code .= sprintf(" calls:\n%s\n", $this->dumper->dump($this->dumpValue($definition->getMethodCalls()), 1, 12)); | |
129 } | |
130 | |
131 if (!$definition->isShared()) { | |
132 $code .= " shared: false\n"; | |
133 } | |
134 | |
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); | |
140 } | |
141 if (0 !== $priority) { | |
142 $code .= sprintf(" decoration_priority: %s\n", $priority); | |
143 } | |
144 } | |
145 | |
146 if ($callable = $definition->getFactory()) { | |
147 $code .= sprintf(" factory: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0)); | |
148 } | |
149 | |
150 if ($callable = $definition->getConfigurator()) { | |
151 $code .= sprintf(" configurator: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0)); | |
152 } | |
153 | |
154 return $code; | |
155 } | |
156 | |
157 /** | |
158 * Adds a service alias. | |
159 * | |
160 * @param string $alias | |
161 * @param Alias $id | |
162 * | |
163 * @return string | |
164 */ | |
165 private function addServiceAlias($alias, $id) | |
166 { | |
167 if ($id->isPublic()) { | |
168 return sprintf(" %s: '@%s'\n", $alias, $id); | |
169 } | |
170 | |
171 return sprintf(" %s:\n alias: %s\n public: false\n", $alias, $id); | |
172 } | |
173 | |
174 /** | |
175 * Adds services. | |
176 * | |
177 * @return string | |
178 */ | |
179 private function addServices() | |
180 { | |
181 if (!$this->container->getDefinitions()) { | |
182 return ''; | |
183 } | |
184 | |
185 $code = "services:\n"; | |
186 foreach ($this->container->getDefinitions() as $id => $definition) { | |
187 $code .= $this->addService($id, $definition); | |
188 } | |
189 | |
190 $aliases = $this->container->getAliases(); | |
191 foreach ($aliases as $alias => $id) { | |
192 while (isset($aliases[(string) $id])) { | |
193 $id = $aliases[(string) $id]; | |
194 } | |
195 $code .= $this->addServiceAlias($alias, $id); | |
196 } | |
197 | |
198 return $code; | |
199 } | |
200 | |
201 /** | |
202 * Adds parameters. | |
203 * | |
204 * @return string | |
205 */ | |
206 private function addParameters() | |
207 { | |
208 if (!$this->container->getParameterBag()->all()) { | |
209 return ''; | |
210 } | |
211 | |
212 $parameters = $this->prepareParameters($this->container->getParameterBag()->all(), $this->container->isFrozen()); | |
213 | |
214 return $this->dumper->dump(array('parameters' => $parameters), 2); | |
215 } | |
216 | |
217 /** | |
218 * Dumps callable to YAML format. | |
219 * | |
220 * @param callable $callable | |
221 * | |
222 * @return callable | |
223 */ | |
224 private function dumpCallable($callable) | |
225 { | |
226 if (is_array($callable)) { | |
227 if ($callable[0] instanceof Reference) { | |
228 $callable = array($this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]); | |
229 } else { | |
230 $callable = array($callable[0], $callable[1]); | |
231 } | |
232 } | |
233 | |
234 return $callable; | |
235 } | |
236 | |
237 /** | |
238 * Dumps the value to YAML format. | |
239 * | |
240 * @param mixed $value | |
241 * | |
242 * @return mixed | |
243 * | |
244 * @throws RuntimeException When trying to dump object or resource | |
245 */ | |
246 private function dumpValue($value) | |
247 { | |
248 if (is_array($value)) { | |
249 $code = array(); | |
250 foreach ($value as $k => $v) { | |
251 $code[$k] = $this->dumpValue($v); | |
252 } | |
253 | |
254 return $code; | |
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.'); | |
263 } | |
264 | |
265 return $value; | |
266 } | |
267 | |
268 /** | |
269 * Gets the service call. | |
270 * | |
271 * @param string $id | |
272 * @param Reference $reference | |
273 * | |
274 * @return string | |
275 */ | |
276 private function getServiceCall($id, Reference $reference = null) | |
277 { | |
278 if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) { | |
279 return sprintf('@?%s', $id); | |
280 } | |
281 | |
282 return sprintf('@%s', $id); | |
283 } | |
284 | |
285 /** | |
286 * Gets parameter call. | |
287 * | |
288 * @param string $id | |
289 * | |
290 * @return string | |
291 */ | |
292 private function getParameterCall($id) | |
293 { | |
294 return sprintf('%%%s%%', $id); | |
295 } | |
296 | |
297 private function getExpressionCall($expression) | |
298 { | |
299 return sprintf('@=%s', $expression); | |
300 } | |
301 | |
302 /** | |
303 * Prepares parameters. | |
304 * | |
305 * @param array $parameters | |
306 * @param bool $escape | |
307 * | |
308 * @return array | |
309 */ | |
310 private function prepareParameters(array $parameters, $escape = true) | |
311 { | |
312 $filtered = array(); | |
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, '@')) { | |
317 $value = '@'.$value; | |
318 } | |
319 | |
320 $filtered[$key] = $value; | |
321 } | |
322 | |
323 return $escape ? $this->escape($filtered) : $filtered; | |
324 } | |
325 | |
326 /** | |
327 * Escapes arguments. | |
328 * | |
329 * @param array $arguments | |
330 * | |
331 * @return array | |
332 */ | |
333 private function escape(array $arguments) | |
334 { | |
335 $args = array(); | |
336 foreach ($arguments as $k => $v) { | |
337 if (is_array($v)) { | |
338 $args[$k] = $this->escape($v); | |
339 } elseif (is_string($v)) { | |
340 $args[$k] = str_replace('%', '%%', $v); | |
341 } else { | |
342 $args[$k] = $v; | |
343 } | |
344 } | |
345 | |
346 return $args; | |
347 } | |
348 } |