vendor/symfony/framework-bundle/CacheWarmer/AnnotationsCacheWarmer.php line 35

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
  11. use Doctrine\Common\Annotations\AnnotationException;
  12. use Doctrine\Common\Annotations\PsrCachedReader;
  13. use Doctrine\Common\Annotations\Reader;
  14. use Symfony\Component\Cache\Adapter\ArrayAdapter;
  15. use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
  16. /**
  17.  * Warms up annotation caches for classes found in composer's autoload class map
  18.  * and declared in DI bundle extensions using the addAnnotatedClassesToCache method.
  19.  *
  20.  * @author Titouan Galopin <galopintitouan@gmail.com>
  21.  */
  22. class AnnotationsCacheWarmer extends AbstractPhpFileCacheWarmer
  23. {
  24.     private $annotationReader;
  25.     private $excludeRegexp;
  26.     private $debug;
  27.     /**
  28.      * @param string $phpArrayFile The PHP file where annotations are cached
  29.      */
  30.     public function __construct(Reader $annotationReaderstring $phpArrayFilestring $excludeRegexp nullbool $debug false)
  31.     {
  32.         parent::__construct($phpArrayFile);
  33.         $this->annotationReader $annotationReader;
  34.         $this->excludeRegexp $excludeRegexp;
  35.         $this->debug $debug;
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     protected function doWarmUp(string $cacheDirArrayAdapter $arrayAdapter)
  41.     {
  42.         $annotatedClassPatterns $cacheDir.'/annotations.map';
  43.         if (!is_file($annotatedClassPatterns)) {
  44.             return true;
  45.         }
  46.         $annotatedClasses = include $annotatedClassPatterns;
  47.         $reader = new PsrCachedReader($this->annotationReader$arrayAdapter$this->debug);
  48.         foreach ($annotatedClasses as $class) {
  49.             if (null !== $this->excludeRegexp && preg_match($this->excludeRegexp$class)) {
  50.                 continue;
  51.             }
  52.             try {
  53.                 $this->readAllComponents($reader$class);
  54.             } catch (\Exception $e) {
  55.                 $this->ignoreAutoloadException($class$e);
  56.             }
  57.         }
  58.         return true;
  59.     }
  60.     /**
  61.      * @return string[] A list of classes to preload on PHP 7.4+
  62.      */
  63.     protected function warmUpPhpArrayAdapter(PhpArrayAdapter $phpArrayAdapter, array $values)
  64.     {
  65.         // make sure we don't cache null values
  66.         $values array_filter($values, function ($val) { return null !== $val; });
  67.         return parent::warmUpPhpArrayAdapter($phpArrayAdapter$values);
  68.     }
  69.     private function readAllComponents(Reader $readerstring $class)
  70.     {
  71.         $reflectionClass = new \ReflectionClass($class);
  72.         try {
  73.             $reader->getClassAnnotations($reflectionClass);
  74.         } catch (AnnotationException $e) {
  75.             /*
  76.              * Ignore any AnnotationException to not break the cache warming process if an Annotation is badly
  77.              * configured or could not be found / read / etc.
  78.              *
  79.              * In particular cases, an Annotation in your code can be used and defined only for a specific
  80.              * environment but is always added to the annotations.map file by some Symfony default behaviors,
  81.              * and you always end up with a not found Annotation.
  82.              */
  83.         }
  84.         foreach ($reflectionClass->getMethods() as $reflectionMethod) {
  85.             try {
  86.                 $reader->getMethodAnnotations($reflectionMethod);
  87.             } catch (AnnotationException $e) {
  88.             }
  89.         }
  90.         foreach ($reflectionClass->getProperties() as $reflectionProperty) {
  91.             try {
  92.                 $reader->getPropertyAnnotations($reflectionProperty);
  93.             } catch (AnnotationException $e) {
  94.             }
  95.         }
  96.     }
  97. }