[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/vendor/symfony/dependency-injection/Loader/ -> FileLoader.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\Loader;
  13  
  14  use Symfony\Component\Config\FileLocatorInterface;
  15  use Symfony\Component\Config\Loader\FileLoader as BaseFileLoader;
  16  use Symfony\Component\Config\Resource\GlobResource;
  17  use Symfony\Component\DependencyInjection\ChildDefinition;
  18  use Symfony\Component\DependencyInjection\ContainerBuilder;
  19  use Symfony\Component\DependencyInjection\Definition;
  20  use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  21  
  22  /**
  23   * FileLoader is the abstract class used by all built-in loaders that are file based.
  24   *
  25   * @author Fabien Potencier <fabien@symfony.com>
  26   */
  27  abstract class FileLoader extends BaseFileLoader
  28  {
  29      protected $container;
  30      protected $isLoadingInstanceof = false;
  31      protected $instanceof = [];
  32  
  33      public function __construct(ContainerBuilder $container, FileLocatorInterface $locator)
  34      {
  35          $this->container = $container;
  36  
  37          parent::__construct($locator);
  38      }
  39  
  40      /**
  41       * Registers a set of classes as services using PSR-4 for discovery.
  42       *
  43       * @param Definition $prototype A definition to use as template
  44       * @param string     $namespace The namespace prefix of classes in the scanned directory
  45       * @param string     $resource  The directory to look for classes, glob-patterns allowed
  46       * @param string     $exclude   A globed path of files to exclude
  47       */
  48      public function registerClasses(Definition $prototype, $namespace, $resource, $exclude = null)
  49      {
  50          if ('\\' !== substr($namespace, -1)) {
  51              throw new InvalidArgumentException(sprintf('Namespace prefix must end with a "\\": "%s".', $namespace));
  52          }
  53          if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/', $namespace)) {
  54              throw new InvalidArgumentException(sprintf('Namespace is not a valid PSR-4 prefix: "%s".', $namespace));
  55          }
  56  
  57          $classes = $this->findClasses($namespace, $resource, $exclude);
  58          // prepare for deep cloning
  59          $serializedPrototype = serialize($prototype);
  60          $interfaces = [];
  61          $singlyImplemented = [];
  62  
  63          foreach ($classes as $class => $errorMessage) {
  64              if (interface_exists($class, false)) {
  65                  $interfaces[] = $class;
  66              } else {
  67                  $this->setDefinition($class, $definition = unserialize($serializedPrototype));
  68                  if (null !== $errorMessage) {
  69                      $definition->addError($errorMessage);
  70  
  71                      continue;
  72                  }
  73                  foreach (class_implements($class, false) as $interface) {
  74                      $singlyImplemented[$interface] = isset($singlyImplemented[$interface]) ? false : $class;
  75                  }
  76              }
  77          }
  78          foreach ($interfaces as $interface) {
  79              if (!empty($singlyImplemented[$interface])) {
  80                  $this->container->setAlias($interface, $singlyImplemented[$interface])
  81                      ->setPublic(false);
  82              }
  83          }
  84      }
  85  
  86      /**
  87       * Registers a definition in the container with its instanceof-conditionals.
  88       *
  89       * @param string $id
  90       */
  91      protected function setDefinition($id, Definition $definition)
  92      {
  93          $this->container->removeBindings($id);
  94  
  95          if ($this->isLoadingInstanceof) {
  96              if (!$definition instanceof ChildDefinition) {
  97                  throw new InvalidArgumentException(sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, \get_class($definition)));
  98              }
  99              $this->instanceof[$id] = $definition;
 100          } else {
 101              $this->container->setDefinition($id, $definition instanceof ChildDefinition ? $definition : $definition->setInstanceofConditionals($this->instanceof));
 102          }
 103      }
 104  
 105      private function findClasses($namespace, $pattern, $excludePattern)
 106      {
 107          $parameterBag = $this->container->getParameterBag();
 108  
 109          $excludePaths = [];
 110          $excludePrefix = null;
 111          if ($excludePattern) {
 112              $excludePattern = $parameterBag->unescapeValue($parameterBag->resolveValue($excludePattern));
 113              foreach ($this->glob($excludePattern, true, $resource, true) as $path => $info) {
 114                  if (null === $excludePrefix) {
 115                      $excludePrefix = $resource->getPrefix();
 116                  }
 117  
 118                  // normalize Windows slashes
 119                  $excludePaths[str_replace('\\', '/', $path)] = true;
 120              }
 121          }
 122  
 123          $pattern = $parameterBag->unescapeValue($parameterBag->resolveValue($pattern));
 124          $classes = [];
 125          $extRegexp = \defined('HHVM_VERSION') ? '/\\.(?:php|hh)$/' : '/\\.php$/';
 126          $prefixLen = null;
 127          foreach ($this->glob($pattern, true, $resource) as $path => $info) {
 128              if (null === $prefixLen) {
 129                  $prefixLen = \strlen($resource->getPrefix());
 130  
 131                  if ($excludePrefix && 0 !== strpos($excludePrefix, $resource->getPrefix())) {
 132                      throw new InvalidArgumentException(sprintf('Invalid "exclude" pattern when importing classes for "%s": make sure your "exclude" pattern (%s) is a subset of the "resource" pattern (%s).', $namespace, $excludePattern, $pattern));
 133                  }
 134              }
 135  
 136              if (isset($excludePaths[str_replace('\\', '/', $path)])) {
 137                  continue;
 138              }
 139  
 140              if (!preg_match($extRegexp, $path, $m) || !$info->isReadable()) {
 141                  continue;
 142              }
 143              $class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, -\strlen($m[0]))), '\\');
 144  
 145              if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $class)) {
 146                  continue;
 147              }
 148  
 149              try {
 150                  $r = $this->container->getReflectionClass($class);
 151              } catch (\ReflectionException $e) {
 152                  $classes[$class] = $e->getMessage();
 153                  continue;
 154              }
 155              // check to make sure the expected class exists
 156              if (!$r) {
 157                  throw new InvalidArgumentException(sprintf('Expected to find class "%s" in file "%s" while importing services from resource "%s", but it was not found! Check the namespace prefix used with the resource.', $class, $path, $pattern));
 158              }
 159  
 160              if ($r->isInstantiable() || $r->isInterface()) {
 161                  $classes[$class] = null;
 162              }
 163          }
 164  
 165          // track only for new & removed files
 166          if ($resource instanceof GlobResource) {
 167              $this->container->addResource($resource);
 168          } else {
 169              foreach ($resource as $path) {
 170                  $this->container->fileExists($path, false);
 171              }
 172          }
 173  
 174          return $classes;
 175      }
 176  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1