[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/vendor/symfony/dependency-injection/Compiler/ -> RegisterEnvVarProcessorsPass.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\Compiler;
  13  
  14  use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
  15  use Symfony\Component\DependencyInjection\ContainerBuilder;
  16  use Symfony\Component\DependencyInjection\EnvVarProcessor;
  17  use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
  18  use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  19  use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
  20  use Symfony\Component\DependencyInjection\Reference;
  21  use Symfony\Component\DependencyInjection\ServiceLocator;
  22  
  23  /**
  24   * Creates the container.env_var_processors_locator service.
  25   *
  26   * @author Nicolas Grekas <p@tchwork.com>
  27   */
  28  class RegisterEnvVarProcessorsPass implements CompilerPassInterface
  29  {
  30      private static $allowedTypes = ['array', 'bool', 'float', 'int', 'string'];
  31  
  32      public function process(ContainerBuilder $container)
  33      {
  34          $bag = $container->getParameterBag();
  35          $types = [];
  36          $processors = [];
  37          foreach ($container->findTaggedServiceIds('container.env_var_processor') as $id => $tags) {
  38              if (!$r = $container->getReflectionClass($class = $container->getDefinition($id)->getClass())) {
  39                  throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  40              } elseif (!$r->isSubclassOf(EnvVarProcessorInterface::class)) {
  41                  throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, EnvVarProcessorInterface::class));
  42              }
  43              foreach ($class::getProvidedTypes() as $prefix => $type) {
  44                  $processors[$prefix] = new ServiceClosureArgument(new Reference($id));
  45                  $types[$prefix] = self::validateProvidedTypes($type, $class);
  46              }
  47          }
  48  
  49          if ($bag instanceof EnvPlaceholderParameterBag) {
  50              foreach (EnvVarProcessor::getProvidedTypes() as $prefix => $type) {
  51                  if (!isset($types[$prefix])) {
  52                      $types[$prefix] = self::validateProvidedTypes($type, EnvVarProcessor::class);
  53                  }
  54              }
  55              $bag->setProvidedTypes($types);
  56          }
  57  
  58          if ($processors) {
  59              $container->register('container.env_var_processors_locator', ServiceLocator::class)
  60                  ->setPublic(true)
  61                  ->setArguments([$processors])
  62              ;
  63          }
  64      }
  65  
  66      private static function validateProvidedTypes($types, $class)
  67      {
  68          $types = explode('|', $types);
  69  
  70          foreach ($types as $type) {
  71              if (!\in_array($type, self::$allowedTypes)) {
  72                  throw new InvalidArgumentException(sprintf('Invalid type "%s" returned by "%s::getProvidedTypes()", expected one of "%s".', $type, $class, implode('", "', self::$allowedTypes)));
  73              }
  74          }
  75  
  76          return $types;
  77      }
  78  }


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1