|
0
|
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\Yaml; |
|
|
15 |
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
|
16 |
use Symfony\Component\DependencyInjection\Parameter; |
|
|
17 |
use Symfony\Component\DependencyInjection\Reference; |
|
|
18 |
|
|
|
19 |
/** |
|
|
20 |
* YamlDumper dumps a service container as a YAML string. |
|
|
21 |
* |
|
|
22 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
23 |
* |
|
|
24 |
* @api |
|
|
25 |
*/ |
|
|
26 |
class YamlDumper extends Dumper |
|
|
27 |
{ |
|
|
28 |
/** |
|
|
29 |
* Dumps the service container as an YAML string. |
|
|
30 |
* |
|
|
31 |
* @param array $options An array of options |
|
|
32 |
* |
|
|
33 |
* @return string A YAML string representing of the service container |
|
|
34 |
* |
|
|
35 |
* @api |
|
|
36 |
*/ |
|
|
37 |
public function dump(array $options = array()) |
|
|
38 |
{ |
|
|
39 |
return $this->addParameters()."\n".$this->addServices(); |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
/** |
|
|
43 |
* Adds a service |
|
|
44 |
* |
|
|
45 |
* @param string $id |
|
|
46 |
* @param Definition $definition |
|
|
47 |
* @return string |
|
|
48 |
*/ |
|
|
49 |
private function addService($id, $definition) |
|
|
50 |
{ |
|
|
51 |
$code = " $id:\n"; |
|
|
52 |
if ($definition->getClass()) { |
|
|
53 |
$code .= sprintf(" class: %s\n", $definition->getClass()); |
|
|
54 |
} |
|
|
55 |
|
|
|
56 |
$tagsCode = ''; |
|
|
57 |
foreach ($definition->getTags() as $name => $tags) { |
|
|
58 |
foreach ($tags as $attributes) { |
|
|
59 |
$att = array(); |
|
|
60 |
foreach ($attributes as $key => $value) { |
|
|
61 |
$att[] = sprintf('%s: %s', Yaml::dump($key), Yaml::dump($value)); |
|
|
62 |
} |
|
|
63 |
$att = $att ? ', '.implode(' ', $att) : ''; |
|
|
64 |
|
|
|
65 |
$tagsCode .= sprintf(" - { name: %s%s }\n", Yaml::dump($name), $att); |
|
|
66 |
} |
|
|
67 |
} |
|
|
68 |
if ($tagsCode) { |
|
|
69 |
$code .= " tags:\n".$tagsCode; |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
if ($definition->getFile()) { |
|
|
73 |
$code .= sprintf(" file: %s\n", $definition->getFile()); |
|
|
74 |
} |
|
|
75 |
|
|
|
76 |
if ($definition->getFactoryMethod()) { |
|
|
77 |
$code .= sprintf(" factory_method: %s\n", $definition->getFactoryMethod()); |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
if ($definition->getFactoryService()) { |
|
|
81 |
$code .= sprintf(" factory_service: %s\n", $definition->getFactoryService()); |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
if ($definition->getArguments()) { |
|
|
85 |
$code .= sprintf(" arguments: %s\n", Yaml::dump($this->dumpValue($definition->getArguments()), 0)); |
|
|
86 |
} |
|
|
87 |
|
|
|
88 |
if ($definition->getProperties()) { |
|
|
89 |
$code .= sprintf(" properties: %s\n", Yaml::dump($this->dumpValue($definition->getProperties()), 0)); |
|
|
90 |
} |
|
|
91 |
|
|
|
92 |
if ($definition->getMethodCalls()) { |
|
|
93 |
$code .= sprintf(" calls:\n %s\n", str_replace("\n", "\n ", Yaml::dump($this->dumpValue($definition->getMethodCalls()), 1))); |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
if (ContainerInterface::SCOPE_CONTAINER !== $scope = $definition->getScope()) { |
|
|
97 |
$code .= sprintf(" scope: %s\n", $scope); |
|
|
98 |
} |
|
|
99 |
|
|
|
100 |
if ($callable = $definition->getConfigurator()) { |
|
|
101 |
if (is_array($callable)) { |
|
|
102 |
if (is_object($callable[0]) && $callable[0] instanceof Reference) { |
|
|
103 |
$callable = array($this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]); |
|
|
104 |
} else { |
|
|
105 |
$callable = array($callable[0], $callable[1]); |
|
|
106 |
} |
|
|
107 |
} |
|
|
108 |
|
|
|
109 |
$code .= sprintf(" configurator: %s\n", Yaml::dump($callable, 0)); |
|
|
110 |
} |
|
|
111 |
|
|
|
112 |
return $code; |
|
|
113 |
} |
|
|
114 |
|
|
|
115 |
/** |
|
|
116 |
* Adds a service alias |
|
|
117 |
* |
|
|
118 |
* @param string $alias |
|
|
119 |
* @param string $id |
|
|
120 |
* @return string |
|
|
121 |
*/ |
|
|
122 |
private function addServiceAlias($alias, $id) |
|
|
123 |
{ |
|
|
124 |
if ($id->isPublic()) { |
|
|
125 |
return sprintf(" %s: @%s\n", $alias, $id); |
|
|
126 |
} else { |
|
|
127 |
return sprintf(" %s:\n alias: %s\n public: false", $alias, $id); |
|
|
128 |
} |
|
|
129 |
} |
|
|
130 |
|
|
|
131 |
/** |
|
|
132 |
* Adds services |
|
|
133 |
* |
|
|
134 |
* @return string |
|
|
135 |
*/ |
|
|
136 |
private function addServices() |
|
|
137 |
{ |
|
|
138 |
if (!$this->container->getDefinitions()) { |
|
|
139 |
return ''; |
|
|
140 |
} |
|
|
141 |
|
|
|
142 |
$code = "services:\n"; |
|
|
143 |
foreach ($this->container->getDefinitions() as $id => $definition) { |
|
|
144 |
$code .= $this->addService($id, $definition); |
|
|
145 |
} |
|
|
146 |
|
|
|
147 |
foreach ($this->container->getAliases() as $alias => $id) { |
|
|
148 |
$code .= $this->addServiceAlias($alias, $id); |
|
|
149 |
} |
|
|
150 |
|
|
|
151 |
return $code; |
|
|
152 |
} |
|
|
153 |
|
|
|
154 |
/** |
|
|
155 |
* Adds parameters |
|
|
156 |
* |
|
|
157 |
* @return string |
|
|
158 |
*/ |
|
|
159 |
private function addParameters() |
|
|
160 |
{ |
|
|
161 |
if (!$this->container->getParameterBag()->all()) { |
|
|
162 |
return ''; |
|
|
163 |
} |
|
|
164 |
|
|
|
165 |
if ($this->container->isFrozen()) { |
|
|
166 |
$parameters = $this->prepareParameters($this->container->getParameterBag()->all()); |
|
|
167 |
} else { |
|
|
168 |
$parameters = $this->container->getParameterBag()->all(); |
|
|
169 |
} |
|
|
170 |
|
|
|
171 |
return Yaml::dump(array('parameters' => $parameters), 2); |
|
|
172 |
} |
|
|
173 |
|
|
|
174 |
/** |
|
|
175 |
* Dumps the value to YAML format |
|
|
176 |
* |
|
|
177 |
* @param mixed $value |
|
|
178 |
* @throws \RuntimeException When trying to dump object or resource |
|
|
179 |
*/ |
|
|
180 |
private function dumpValue($value) |
|
|
181 |
{ |
|
|
182 |
if (is_array($value)) { |
|
|
183 |
$code = array(); |
|
|
184 |
foreach ($value as $k => $v) { |
|
|
185 |
$code[$k] = $this->dumpValue($v); |
|
|
186 |
} |
|
|
187 |
|
|
|
188 |
return $code; |
|
|
189 |
} elseif (is_object($value) && $value instanceof Reference) { |
|
|
190 |
return $this->getServiceCall((string) $value, $value); |
|
|
191 |
} elseif (is_object($value) && $value instanceof Parameter) { |
|
|
192 |
return $this->getParameterCall((string) $value); |
|
|
193 |
} elseif (is_object($value) || is_resource($value)) { |
|
|
194 |
throw new \RuntimeException('Unable to dump a service container if a parameter is an object or a resource.'); |
|
|
195 |
} |
|
|
196 |
|
|
|
197 |
return $value; |
|
|
198 |
} |
|
|
199 |
|
|
|
200 |
/** |
|
|
201 |
* Gets the service call. |
|
|
202 |
* |
|
|
203 |
* @param string $id |
|
|
204 |
* @param Reference $reference |
|
|
205 |
* @return string |
|
|
206 |
*/ |
|
|
207 |
private function getServiceCall($id, Reference $reference = null) |
|
|
208 |
{ |
|
|
209 |
if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) { |
|
|
210 |
return sprintf('@?%s', $id); |
|
|
211 |
} |
|
|
212 |
|
|
|
213 |
return sprintf('@%s', $id); |
|
|
214 |
} |
|
|
215 |
|
|
|
216 |
/** |
|
|
217 |
* Gets parameter call. |
|
|
218 |
* |
|
|
219 |
* @param string $id |
|
|
220 |
* @return string |
|
|
221 |
*/ |
|
|
222 |
private function getParameterCall($id) |
|
|
223 |
{ |
|
|
224 |
return sprintf('%%%s%%', $id); |
|
|
225 |
} |
|
|
226 |
|
|
|
227 |
/** |
|
|
228 |
* Prepares parameters |
|
|
229 |
* |
|
|
230 |
* @param array $parameters |
|
|
231 |
* @return array |
|
|
232 |
*/ |
|
|
233 |
private function prepareParameters($parameters) |
|
|
234 |
{ |
|
|
235 |
$filtered = array(); |
|
|
236 |
foreach ($parameters as $key => $value) { |
|
|
237 |
if (is_array($value)) { |
|
|
238 |
$value = $this->prepareParameters($value); |
|
|
239 |
} elseif ($value instanceof Reference) { |
|
|
240 |
$value = '@'.$value; |
|
|
241 |
} |
|
|
242 |
|
|
|
243 |
$filtered[$key] = $value; |
|
|
244 |
} |
|
|
245 |
|
|
|
246 |
return $this->escape($filtered); |
|
|
247 |
} |
|
|
248 |
|
|
|
249 |
/** |
|
|
250 |
* Escapes arguments |
|
|
251 |
* |
|
|
252 |
* @param array $arguments |
|
|
253 |
* @return array |
|
|
254 |
*/ |
|
|
255 |
private function escape($arguments) |
|
|
256 |
{ |
|
|
257 |
$args = array(); |
|
|
258 |
foreach ($arguments as $k => $v) { |
|
|
259 |
if (is_array($v)) { |
|
|
260 |
$args[$k] = $this->escape($v); |
|
|
261 |
} elseif (is_string($v)) { |
|
|
262 |
$args[$k] = str_replace('%', '%%', $v); |
|
|
263 |
} else { |
|
|
264 |
$args[$k] = $v; |
|
|
265 |
} |
|
|
266 |
} |
|
|
267 |
|
|
|
268 |
return $args; |
|
|
269 |
} |
|
|
270 |
} |