vendor/symfony/framework-bundle/CacheWarmer/AbstractPhpFileCacheWarmer.php line 58

  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 Symfony\Component\Cache\Adapter\ArrayAdapter;
  12. use Symfony\Component\Cache\Adapter\NullAdapter;
  13. use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
  14. use Symfony\Component\Config\Resource\ClassExistenceResource;
  15. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
  16. abstract class AbstractPhpFileCacheWarmer implements CacheWarmerInterface
  17. {
  18.     private string $phpArrayFile;
  19.     /**
  20.      * @param string $phpArrayFile The PHP file where metadata are cached
  21.      */
  22.     public function __construct(string $phpArrayFile)
  23.     {
  24.         $this->phpArrayFile $phpArrayFile;
  25.     }
  26.     public function isOptional(): bool
  27.     {
  28.         return true;
  29.     }
  30.     /**
  31.      * @return string[] A list of classes to preload on PHP 7.4+
  32.      */
  33.     public function warmUp(string $cacheDir): array
  34.     {
  35.         $arrayAdapter = new ArrayAdapter();
  36.         spl_autoload_register([ClassExistenceResource::class, 'throwOnRequiredClass']);
  37.         try {
  38.             if (!$this->doWarmUp($cacheDir$arrayAdapter)) {
  39.                 return [];
  40.             }
  41.         } finally {
  42.             spl_autoload_unregister([ClassExistenceResource::class, 'throwOnRequiredClass']);
  43.         }
  44.         // the ArrayAdapter stores the values serialized
  45.         // to avoid mutation of the data after it was written to the cache
  46.         // so here we un-serialize the values first
  47.         $values array_map(function ($val) { return null !== $val unserialize($val) : null; }, $arrayAdapter->getValues());
  48.         return $this->warmUpPhpArrayAdapter(new PhpArrayAdapter($this->phpArrayFile, new NullAdapter()), $values);
  49.     }
  50.     /**
  51.      * @return string[] A list of classes to preload on PHP 7.4+
  52.      */
  53.     protected function warmUpPhpArrayAdapter(PhpArrayAdapter $phpArrayAdapter, array $values): array
  54.     {
  55.         return (array) $phpArrayAdapter->warmUp($values);
  56.     }
  57.     /**
  58.      * @internal
  59.      */
  60.     final protected function ignoreAutoloadException(string $class\Exception $exception): void
  61.     {
  62.         try {
  63.             ClassExistenceResource::throwOnRequiredClass($class$exception);
  64.         } catch (\ReflectionException) {
  65.         }
  66.     }
  67.     /**
  68.      * @return bool false if there is nothing to warm-up
  69.      */
  70.     abstract protected function doWarmUp(string $cacheDirArrayAdapter $arrayAdapter): bool;
  71. }