Verzeichnisstruktur phpBB-3.1.0
- Veröffentlicht
- 27.10.2014
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
YamlDumper.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\DependencyInjection\Dumper;
013
014 use Symfony\Component\Yaml\Dumper as YmlDumper;
015 use Symfony\Component\DependencyInjection\Alias;
016 use Symfony\Component\DependencyInjection\ContainerInterface;
017 use Symfony\Component\DependencyInjection\Definition;
018 use Symfony\Component\DependencyInjection\Parameter;
019 use Symfony\Component\DependencyInjection\Reference;
020 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
021 use Symfony\Component\DependencyInjection\ContainerBuilder;
022
023 /**
024 * YamlDumper dumps a service container as a YAML string.
025 *
026 * @author Fabien Potencier <fabien@symfony.com>
027 *
028 * @api
029 */
030 class YamlDumper extends Dumper
031 {
032 private $dumper;
033
034 /**
035 * Constructor.
036 *
037 * @param ContainerBuilder $container The service container to dump
038 *
039 * @api
040 */
041 public function __construct(ContainerBuilder $container)
042 {
043 parent::__construct($container);
044
045 $this->dumper = new YmlDumper();
046 }
047
048 /**
049 * Dumps the service container as an YAML string.
050 *
051 * @param array $options An array of options
052 *
053 * @return string A YAML string representing of the service container
054 *
055 * @api
056 */
057 public function dump(array $options = array())
058 {
059 return $this->addParameters()."\n".$this->addServices();
060 }
061
062 /**
063 * Adds a service
064 *
065 * @param string $id
066 * @param Definition $definition
067 *
068 * @return string
069 */
070 private function addService($id, $definition)
071 {
072 $code = " $id:\n";
073 if ($definition->getClass()) {
074 $code .= sprintf(" class: %s\n", $definition->getClass());
075 }
076
077 if (!$definition->isPublic()) {
078 $code .= " public: false\n";
079 }
080
081 $tagsCode = '';
082 foreach ($definition->getTags() as $name => $tags) {
083 foreach ($tags as $attributes) {
084 $att = array();
085 foreach ($attributes as $key => $value) {
086 $att[] = sprintf('%s: %s', $this->dumper->dump($key), $this->dumper->dump($value));
087 }
088 $att = $att ? ', '.implode(' ', $att) : '';
089
090 $tagsCode .= sprintf(" - { name: %s%s }\n", $this->dumper->dump($name), $att);
091 }
092 }
093 if ($tagsCode) {
094 $code .= " tags:\n".$tagsCode;
095 }
096
097 if ($definition->getFile()) {
098 $code .= sprintf(" file: %s\n", $definition->getFile());
099 }
100
101 if ($definition->isSynthetic()) {
102 $code .= sprintf(" synthetic: true\n");
103 }
104
105 if ($definition->isSynchronized()) {
106 $code .= sprintf(" synchronized: true\n");
107 }
108
109 if ($definition->getFactoryClass()) {
110 $code .= sprintf(" factory_class: %s\n", $definition->getFactoryClass());
111 }
112
113 if ($definition->isLazy()) {
114 $code .= sprintf(" lazy: true\n");
115 }
116
117 if ($definition->getFactoryMethod()) {
118 $code .= sprintf(" factory_method: %s\n", $definition->getFactoryMethod());
119 }
120
121 if ($definition->getFactoryService()) {
122 $code .= sprintf(" factory_service: %s\n", $definition->getFactoryService());
123 }
124
125 if ($definition->getArguments()) {
126 $code .= sprintf(" arguments: %s\n", $this->dumper->dump($this->dumpValue($definition->getArguments()), 0));
127 }
128
129 if ($definition->getProperties()) {
130 $code .= sprintf(" properties: %s\n", $this->dumper->dump($this->dumpValue($definition->getProperties()), 0));
131 }
132
133 if ($definition->getMethodCalls()) {
134 $code .= sprintf(" calls:\n%s\n", $this->dumper->dump($this->dumpValue($definition->getMethodCalls()), 1, 12));
135 }
136
137 if (ContainerInterface::SCOPE_CONTAINER !== $scope = $definition->getScope()) {
138 $code .= sprintf(" scope: %s\n", $scope);
139 }
140
141 if ($callable = $definition->getConfigurator()) {
142 if (is_array($callable)) {
143 if ($callable[0] instanceof Reference) {
144 $callable = array($this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]);
145 } else {
146 $callable = array($callable[0], $callable[1]);
147 }
148 }
149
150 $code .= sprintf(" configurator: %s\n", $this->dumper->dump($callable, 0));
151 }
152
153 return $code;
154 }
155
156 /**
157 * Adds a service alias
158 *
159 * @param string $alias
160 * @param Alias $id
161 *
162 * @return string
163 */
164 private function addServiceAlias($alias, $id)
165 {
166 if ($id->isPublic()) {
167 return sprintf(" %s: @%s\n", $alias, $id);
168 } else {
169 return sprintf(" %s:\n alias: %s\n public: false", $alias, $id);
170 }
171 }
172
173 /**
174 * Adds services
175 *
176 * @return string
177 */
178 private function addServices()
179 {
180 if (!$this->container->getDefinitions()) {
181 return '';
182 }
183
184 $code = "services:\n";
185 foreach ($this->container->getDefinitions() as $id => $definition) {
186 $code .= $this->addService($id, $definition);
187 }
188
189 $aliases = $this->container->getAliases();
190 foreach ($aliases as $alias => $id) {
191 while (isset($aliases[(string) $id])) {
192 $id = $aliases[(string) $id];
193 }
194 $code .= $this->addServiceAlias($alias, $id);
195 }
196
197 return $code;
198 }
199
200 /**
201 * Adds parameters
202 *
203 * @return string
204 */
205 private function addParameters()
206 {
207 if (!$this->container->getParameterBag()->all()) {
208 return '';
209 }
210
211 $parameters = $this->prepareParameters($this->container->getParameterBag()->all(), $this->container->isFrozen());
212
213 return $this->dumper->dump(array('parameters' => $parameters), 2);
214 }
215
216 /**
217 * Dumps the value to YAML format
218 *
219 * @param mixed $value
220 *
221 * @return mixed
222 *
223 * @throws RuntimeException When trying to dump object or resource
224 */
225 private function dumpValue($value)
226 {
227 if (is_array($value)) {
228 $code = array();
229 foreach ($value as $k => $v) {
230 $code[$k] = $this->dumpValue($v);
231 }
232
233 return $code;
234 } elseif ($value instanceof Reference) {
235 return $this->getServiceCall((string) $value, $value);
236 } elseif ($value instanceof Parameter) {
237 return $this->getParameterCall((string) $value);
238 } elseif (is_object($value) || is_resource($value)) {
239 throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
240 }
241
242 return $value;
243 }
244
245 /**
246 * Gets the service call.
247 *
248 * @param string $id
249 * @param Reference $reference
250 *
251 * @return string
252 */
253 private function getServiceCall($id, Reference $reference = null)
254 {
255 if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
256 return sprintf('@?%s', $id);
257 }
258
259 return sprintf('@%s', $id);
260 }
261
262 /**
263 * Gets parameter call.
264 *
265 * @param string $id
266 *
267 * @return string
268 */
269 private function getParameterCall($id)
270 {
271 return sprintf('%%%s%%', $id);
272 }
273
274 /**
275 * Prepares parameters.
276 *
277 * @param array $parameters
278 * @param bool $escape
279 *
280 * @return array
281 */
282 private function prepareParameters($parameters, $escape = true)
283 {
284 $filtered = array();
285 foreach ($parameters as $key => $value) {
286 if (is_array($value)) {
287 $value = $this->prepareParameters($value, $escape);
288 } elseif ($value instanceof Reference || is_string($value) && 0 === strpos($value, '@')) {
289 $value = '@'.$value;
290 }
291
292 $filtered[$key] = $value;
293 }
294
295 return $escape ? $this->escape($filtered) : $filtered;
296 }
297
298 /**
299 * Escapes arguments
300 *
301 * @param array $arguments
302 *
303 * @return array
304 */
305 private function escape($arguments)
306 {
307 $args = array();
308 foreach ($arguments as $k => $v) {
309 if (is_array($v)) {
310 $args[$k] = $this->escape($v);
311 } elseif (is_string($v)) {
312 $args[$k] = str_replace('%', '%%', $v);
313 } else {
314 $args[$k] = $v;
315 }
316 }
317
318 return $args;
319 }
320 }
321