[ Index ] |
PHP Cross Reference of phpBB-3.3.12-deutsch |
[Summary view] [Print] [Text view]
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\DependencyInjection\Alias; 15 use Symfony\Component\DependencyInjection\Argument\ArgumentInterface; 16 use Symfony\Component\DependencyInjection\Argument\IteratorArgument; 17 use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; 18 use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; 19 use Symfony\Component\DependencyInjection\ContainerInterface; 20 use Symfony\Component\DependencyInjection\Definition; 21 use Symfony\Component\DependencyInjection\Exception\RuntimeException; 22 use Symfony\Component\DependencyInjection\Parameter; 23 use Symfony\Component\DependencyInjection\Reference; 24 use Symfony\Component\ExpressionLanguage\Expression; 25 use Symfony\Component\Yaml\Dumper as YmlDumper; 26 use Symfony\Component\Yaml\Parser; 27 use Symfony\Component\Yaml\Tag\TaggedValue; 28 use Symfony\Component\Yaml\Yaml; 29 30 /** 31 * YamlDumper dumps a service container as a YAML string. 32 * 33 * @author Fabien Potencier <fabien@symfony.com> 34 */ 35 class YamlDumper extends Dumper 36 { 37 private $dumper; 38 39 /** 40 * Dumps the service container as an YAML string. 41 * 42 * @return string A YAML string representing of the service container 43 */ 44 public function dump(array $options = []) 45 { 46 if (!class_exists('Symfony\Component\Yaml\Dumper')) { 47 throw new RuntimeException('Unable to dump the container as the Symfony Yaml Component is not installed.'); 48 } 49 50 if (null === $this->dumper) { 51 $this->dumper = new YmlDumper(); 52 } 53 54 return $this->container->resolveEnvPlaceholders($this->addParameters()."\n".$this->addServices()); 55 } 56 57 /** 58 * Adds a service. 59 * 60 * @param string $id 61 * 62 * @return string 63 */ 64 private function addService($id, Definition $definition) 65 { 66 $code = " $id:\n"; 67 if ($class = $definition->getClass()) { 68 if ('\\' === substr($class, 0, 1)) { 69 $class = substr($class, 1); 70 } 71 72 $code .= sprintf(" class: %s\n", $this->dumper->dump($class)); 73 } 74 75 if (!$definition->isPrivate()) { 76 $code .= sprintf(" public: %s\n", $definition->isPublic() ? 'true' : 'false'); 77 } 78 79 $tagsCode = ''; 80 foreach ($definition->getTags() as $name => $tags) { 81 foreach ($tags as $attributes) { 82 $att = []; 83 foreach ($attributes as $key => $value) { 84 $att[] = sprintf('%s: %s', $this->dumper->dump($key), $this->dumper->dump($value)); 85 } 86 $att = $att ? ', '.implode(', ', $att) : ''; 87 88 $tagsCode .= sprintf(" - { name: %s%s }\n", $this->dumper->dump($name), $att); 89 } 90 } 91 if ($tagsCode) { 92 $code .= " tags:\n".$tagsCode; 93 } 94 95 if ($definition->getFile()) { 96 $code .= sprintf(" file: %s\n", $this->dumper->dump($definition->getFile())); 97 } 98 99 if ($definition->isSynthetic()) { 100 $code .= " synthetic: true\n"; 101 } 102 103 if ($definition->isDeprecated()) { 104 $code .= sprintf(" deprecated: %s\n", $this->dumper->dump($definition->getDeprecationMessage('%service_id%'))); 105 } 106 107 if ($definition->isAutowired()) { 108 $code .= " autowire: true\n"; 109 } 110 111 $autowiringTypesCode = ''; 112 foreach ($definition->getAutowiringTypes(false) 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->isAutoconfigured()) { 120 $code .= " autoconfigure: true\n"; 121 } 122 123 if ($definition->isAbstract()) { 124 $code .= " abstract: true\n"; 125 } 126 127 if ($definition->isLazy()) { 128 $code .= " lazy: true\n"; 129 } 130 131 if ($definition->getArguments()) { 132 $code .= sprintf(" arguments: %s\n", $this->dumper->dump($this->dumpValue($definition->getArguments()), 0)); 133 } 134 135 if ($definition->getProperties()) { 136 $code .= sprintf(" properties: %s\n", $this->dumper->dump($this->dumpValue($definition->getProperties()), 0)); 137 } 138 139 if ($definition->getMethodCalls()) { 140 $code .= sprintf(" calls:\n%s\n", $this->dumper->dump($this->dumpValue($definition->getMethodCalls()), 1, 12)); 141 } 142 143 if (!$definition->isShared()) { 144 $code .= " shared: false\n"; 145 } 146 147 if (null !== $decorated = $definition->getDecoratedService()) { 148 list($decorated, $renamedId, $priority) = $decorated; 149 $code .= sprintf(" decorates: %s\n", $decorated); 150 if (null !== $renamedId) { 151 $code .= sprintf(" decoration_inner_name: %s\n", $renamedId); 152 } 153 if (0 !== $priority) { 154 $code .= sprintf(" decoration_priority: %s\n", $priority); 155 } 156 } 157 158 if ($callable = $definition->getFactory()) { 159 $code .= sprintf(" factory: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0)); 160 } 161 162 if ($callable = $definition->getConfigurator()) { 163 $code .= sprintf(" configurator: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0)); 164 } 165 166 return $code; 167 } 168 169 /** 170 * Adds a service alias. 171 * 172 * @param string $alias 173 * 174 * @return string 175 */ 176 private function addServiceAlias($alias, Alias $id) 177 { 178 if ($id->isPrivate()) { 179 return sprintf(" %s: '@%s'\n", $alias, $id); 180 } 181 182 return sprintf(" %s:\n alias: %s\n public: %s\n", $alias, $id, $id->isPublic() ? 'true' : 'false'); 183 } 184 185 /** 186 * Adds services. 187 * 188 * @return string 189 */ 190 private function addServices() 191 { 192 if (!$this->container->getDefinitions()) { 193 return ''; 194 } 195 196 $code = "services:\n"; 197 foreach ($this->container->getDefinitions() as $id => $definition) { 198 $code .= $this->addService($id, $definition); 199 } 200 201 $aliases = $this->container->getAliases(); 202 foreach ($aliases as $alias => $id) { 203 while (isset($aliases[(string) $id])) { 204 $id = $aliases[(string) $id]; 205 } 206 $code .= $this->addServiceAlias($alias, $id); 207 } 208 209 return $code; 210 } 211 212 /** 213 * Adds parameters. 214 * 215 * @return string 216 */ 217 private function addParameters() 218 { 219 if (!$this->container->getParameterBag()->all()) { 220 return ''; 221 } 222 223 $parameters = $this->prepareParameters($this->container->getParameterBag()->all(), $this->container->isCompiled()); 224 225 return $this->dumper->dump(['parameters' => $parameters], 2); 226 } 227 228 /** 229 * Dumps callable to YAML format. 230 * 231 * @param mixed $callable 232 */ 233 private function dumpCallable($callable) 234 { 235 if (\is_array($callable)) { 236 if ($callable[0] instanceof Reference) { 237 $callable = [$this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]]; 238 } else { 239 $callable = [$callable[0], $callable[1]]; 240 } 241 } 242 243 return $callable; 244 } 245 246 /** 247 * Dumps the value to YAML format. 248 * 249 * @param mixed $value 250 * 251 * @return mixed 252 * 253 * @throws RuntimeException When trying to dump object or resource 254 */ 255 private function dumpValue($value) 256 { 257 if ($value instanceof ServiceClosureArgument) { 258 $value = $value->getValues()[0]; 259 } 260 if ($value instanceof ArgumentInterface) { 261 if ($value instanceof TaggedIteratorArgument) { 262 return new TaggedValue('tagged', $value->getTag()); 263 } 264 if ($value instanceof IteratorArgument) { 265 $tag = 'iterator'; 266 } else { 267 throw new RuntimeException(sprintf('Unspecified Yaml tag for type "%s".', \get_class($value))); 268 } 269 270 return new TaggedValue($tag, $this->dumpValue($value->getValues())); 271 } 272 273 if (\is_array($value)) { 274 $code = []; 275 foreach ($value as $k => $v) { 276 $code[$k] = $this->dumpValue($v); 277 } 278 279 return $code; 280 } elseif ($value instanceof Reference) { 281 return $this->getServiceCall((string) $value, $value); 282 } elseif ($value instanceof Parameter) { 283 return $this->getParameterCall((string) $value); 284 } elseif ($value instanceof Expression) { 285 return $this->getExpressionCall((string) $value); 286 } elseif ($value instanceof Definition) { 287 return new TaggedValue('service', (new Parser())->parse("_:\n".$this->addService('_', $value), Yaml::PARSE_CUSTOM_TAGS)['_']['_']); 288 } elseif (\is_object($value) || \is_resource($value)) { 289 throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.'); 290 } 291 292 return $value; 293 } 294 295 /** 296 * Gets the service call. 297 * 298 * @param string $id 299 * @param Reference $reference 300 * 301 * @return string 302 */ 303 private function getServiceCall($id, Reference $reference = null) 304 { 305 if (null !== $reference) { 306 switch ($reference->getInvalidBehavior()) { 307 case ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE: break; 308 case ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE: return sprintf('@!%s', $id); 309 default: return sprintf('@?%s', $id); 310 } 311 } 312 313 return sprintf('@%s', $id); 314 } 315 316 /** 317 * Gets parameter call. 318 * 319 * @param string $id 320 * 321 * @return string 322 */ 323 private function getParameterCall($id) 324 { 325 return sprintf('%%%s%%', $id); 326 } 327 328 private function getExpressionCall($expression) 329 { 330 return sprintf('@=%s', $expression); 331 } 332 333 /** 334 * Prepares parameters. 335 * 336 * @param bool $escape 337 * 338 * @return array 339 */ 340 private function prepareParameters(array $parameters, $escape = true) 341 { 342 $filtered = []; 343 foreach ($parameters as $key => $value) { 344 if (\is_array($value)) { 345 $value = $this->prepareParameters($value, $escape); 346 } elseif ($value instanceof Reference || \is_string($value) && 0 === strpos($value, '@')) { 347 $value = '@'.$value; 348 } 349 350 $filtered[$key] = $value; 351 } 352 353 return $escape ? $this->escape($filtered) : $filtered; 354 } 355 356 /** 357 * Escapes arguments. 358 * 359 * @return array 360 */ 361 private function escape(array $arguments) 362 { 363 $args = []; 364 foreach ($arguments as $k => $v) { 365 if (\is_array($v)) { 366 $args[$k] = $this->escape($v); 367 } elseif (\is_string($v)) { 368 $args[$k] = str_replace('%', '%%', $v); 369 } else { 370 $args[$k] = $v; 371 } 372 } 373 374 return $args; 375 } 376 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Jun 23 12:25:44 2024 | Cross-referenced by PHPXref 0.7.1 |