[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Dumper/ -> YamlDumper.php (source)

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


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1