[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/DependencyInjection/ -> AddAnnotatedClassesToCachePass.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\HttpKernel\DependencyInjection;
  13  
  14  use Composer\Autoload\ClassLoader;
  15  use Symfony\Component\Debug\DebugClassLoader;
  16  use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  17  use Symfony\Component\DependencyInjection\ContainerBuilder;
  18  use Symfony\Component\HttpKernel\Kernel;
  19  
  20  /**
  21   * Sets the classes to compile in the cache for the container.
  22   *
  23   * @author Fabien Potencier <fabien@symfony.com>
  24   */
  25  class AddAnnotatedClassesToCachePass implements CompilerPassInterface
  26  {
  27      private $kernel;
  28  
  29      public function __construct(Kernel $kernel)
  30      {
  31          $this->kernel = $kernel;
  32      }
  33  
  34      /**
  35       * {@inheritdoc}
  36       */
  37      public function process(ContainerBuilder $container)
  38      {
  39          $classes = [];
  40          $annotatedClasses = [];
  41          foreach ($container->getExtensions() as $extension) {
  42              if ($extension instanceof Extension) {
  43                  if (\PHP_VERSION_ID < 70000) {
  44                      $classes = array_merge($classes, $extension->getClassesToCompile());
  45                  }
  46                  $annotatedClasses = array_merge($annotatedClasses, $extension->getAnnotatedClassesToCompile());
  47              }
  48          }
  49  
  50          $existingClasses = $this->getClassesInComposerClassMaps();
  51  
  52          if (\PHP_VERSION_ID < 70000) {
  53              $classes = $container->getParameterBag()->resolveValue($classes);
  54              $this->kernel->setClassCache($this->expandClasses($classes, $existingClasses));
  55          }
  56          $annotatedClasses = $container->getParameterBag()->resolveValue($annotatedClasses);
  57          $this->kernel->setAnnotatedClassCache($this->expandClasses($annotatedClasses, $existingClasses));
  58      }
  59  
  60      /**
  61       * Expands the given class patterns using a list of existing classes.
  62       *
  63       * @param array $patterns The class patterns to expand
  64       * @param array $classes  The existing classes to match against the patterns
  65       *
  66       * @return array A list of classes derived from the patterns
  67       */
  68      private function expandClasses(array $patterns, array $classes)
  69      {
  70          $expanded = [];
  71  
  72          // Explicit classes declared in the patterns are returned directly
  73          foreach ($patterns as $key => $pattern) {
  74              if ('\\' !== substr($pattern, -1) && false === strpos($pattern, '*')) {
  75                  unset($patterns[$key]);
  76                  $expanded[] = ltrim($pattern, '\\');
  77              }
  78          }
  79  
  80          // Match patterns with the classes list
  81          $regexps = $this->patternsToRegexps($patterns);
  82  
  83          foreach ($classes as $class) {
  84              $class = ltrim($class, '\\');
  85  
  86              if ($this->matchAnyRegexps($class, $regexps)) {
  87                  $expanded[] = $class;
  88              }
  89          }
  90  
  91          return array_unique($expanded);
  92      }
  93  
  94      private function getClassesInComposerClassMaps()
  95      {
  96          $classes = [];
  97  
  98          foreach (spl_autoload_functions() as $function) {
  99              if (!\is_array($function)) {
 100                  continue;
 101              }
 102  
 103              if ($function[0] instanceof DebugClassLoader) {
 104                  $function = $function[0]->getClassLoader();
 105              }
 106  
 107              if (\is_array($function) && $function[0] instanceof ClassLoader) {
 108                  $classes += array_filter($function[0]->getClassMap());
 109              }
 110          }
 111  
 112          return array_keys($classes);
 113      }
 114  
 115      private function patternsToRegexps($patterns)
 116      {
 117          $regexps = [];
 118  
 119          foreach ($patterns as $pattern) {
 120              // Escape user input
 121              $regex = preg_quote(ltrim($pattern, '\\'));
 122  
 123              // Wildcards * and **
 124              $regex = strtr($regex, ['\\*\\*' => '.*?', '\\*' => '[^\\\\]*?']);
 125  
 126              // If this class does not end by a slash, anchor the end
 127              if ('\\' !== substr($regex, -1)) {
 128                  $regex .= '$';
 129              }
 130  
 131              $regexps[] = '{^\\\\'.$regex.'}';
 132          }
 133  
 134          return $regexps;
 135      }
 136  
 137      private function matchAnyRegexps($class, $regexps)
 138      {
 139          $isTest = false !== strpos($class, 'Test');
 140  
 141          foreach ($regexps as $regex) {
 142              if ($isTest && false === strpos($regex, 'Test')) {
 143                  continue;
 144              }
 145  
 146              if (preg_match($regex, '\\'.$class)) {
 147                  return true;
 148              }
 149          }
 150  
 151          return false;
 152      }
 153  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1