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