initial commit

This commit is contained in:
boris
2025-09-30 09:24:25 +01:00
committed by boris
parent a783a12c97
commit c7770ea03b
4695 changed files with 525784 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\NullAdapter;
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
use Symfony\Component\Config\Resource\ClassExistenceResource;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
abstract class AbstractPhpFileCacheWarmer implements CacheWarmerInterface
{
/**
* @param string $phpArrayFile The PHP file where metadata are cached
*/
public function __construct(
private string $phpArrayFile,
) {
}
public function isOptional(): bool
{
return true;
}
public function warmUp(string $cacheDir, ?string $buildDir = null): array
{
$arrayAdapter = new ArrayAdapter();
spl_autoload_register([ClassExistenceResource::class, 'throwOnRequiredClass']);
try {
if (!$this->doWarmUp($cacheDir, $arrayAdapter, $buildDir)) {
return [];
}
} finally {
spl_autoload_unregister([ClassExistenceResource::class, 'throwOnRequiredClass']);
}
// the ArrayAdapter stores the values serialized
// to avoid mutation of the data after it was written to the cache
// so here we un-serialize the values first
$values = array_map(fn ($val) => null !== $val ? unserialize($val) : null, $arrayAdapter->getValues());
return $this->warmUpPhpArrayAdapter(new PhpArrayAdapter($this->phpArrayFile, new NullAdapter()), $values);
}
/**
* @return string[] A list of classes to preload on PHP 7.4+
*/
protected function warmUpPhpArrayAdapter(PhpArrayAdapter $phpArrayAdapter, array $values): array
{
return $phpArrayAdapter->warmUp($values);
}
/**
* @internal
*/
final protected function ignoreAutoloadException(string $class, \Exception $exception): void
{
try {
ClassExistenceResource::throwOnRequiredClass($class, $exception);
} catch (\ReflectionException) {
}
}
/**
* @return bool false if there is nothing to warm-up
*/
abstract protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter, ?string $buildDir = null): bool;
}

View File

@@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
/**
* Clears the cache pools when warming up the cache.
*
* Do not use in production!
*
* @author Teoh Han Hui <teohhanhui@gmail.com>
*
* @internal
*/
final class CachePoolClearerCacheWarmer implements CacheWarmerInterface
{
/**
* @param string[] $pools
*/
public function __construct(
private Psr6CacheClearer $poolClearer,
private array $pools = [],
) {
}
public function warmUp(string $cacheDir, ?string $buildDir = null): array
{
foreach ($this->pools as $pool) {
if ($this->poolClearer->hasPool($pool)) {
$this->poolClearer->clearPool($pool);
}
}
return [];
}
public function isOptional(): bool
{
// optional cache warmers are not run when handling the request
return false;
}
}

View File

@@ -0,0 +1,104 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
use Psr\Log\LoggerInterface;
use Symfony\Component\Config\Builder\ConfigBuilderGenerator;
use Symfony\Component\Config\Builder\ConfigBuilderGeneratorInterface;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* Generate all config builders.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*
* @final since Symfony 7.1
*/
class ConfigBuilderCacheWarmer implements CacheWarmerInterface
{
public function __construct(
private KernelInterface $kernel,
private ?LoggerInterface $logger = null,
) {
}
public function warmUp(string $cacheDir, ?string $buildDir = null): array
{
if (!$buildDir) {
return [];
}
$generator = new ConfigBuilderGenerator($buildDir);
if ($this->kernel instanceof Kernel) {
/** @var ContainerBuilder $container */
$container = \Closure::bind(function (Kernel $kernel) {
$containerBuilder = $kernel->getContainerBuilder();
$kernel->prepareContainer($containerBuilder);
return $containerBuilder;
}, null, $this->kernel)($this->kernel);
$extensions = $container->getExtensions();
} else {
$extensions = [];
foreach ($this->kernel->getBundles() as $bundle) {
$extension = $bundle->getContainerExtension();
if (null !== $extension) {
$extensions[] = $extension;
}
}
}
foreach ($extensions as $extension) {
try {
$this->dumpExtension($extension, $generator);
} catch (\Exception $e) {
$this->logger?->warning('Failed to generate ConfigBuilder for extension {extensionClass}: '.$e->getMessage(), ['exception' => $e, 'extensionClass' => $extension::class]);
}
}
// No need to preload anything
return [];
}
private function dumpExtension(ExtensionInterface $extension, ConfigBuilderGeneratorInterface $generator): void
{
$configuration = null;
if ($extension instanceof ConfigurationInterface) {
$configuration = $extension;
} elseif ($extension instanceof ConfigurationExtensionInterface) {
$container = $this->kernel->getContainer();
$configuration = $extension->getConfiguration([], new ContainerBuilder($container instanceof Container ? new ContainerBag($container) : new ParameterBag()));
}
if (!$configuration) {
return;
}
$generator->build($configuration);
}
public function isOptional(): bool
{
return false;
}
}

View File

@@ -0,0 +1,63 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
/**
* Generates the router matcher and generator classes.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @final
*/
class RouterCacheWarmer implements CacheWarmerInterface, ServiceSubscriberInterface
{
/**
* As this cache warmer is optional, dependencies should be lazy-loaded, that's why a container should be injected.
*/
public function __construct(
private ContainerInterface $container,
) {
}
public function warmUp(string $cacheDir, ?string $buildDir = null): array
{
if (!$buildDir) {
return [];
}
$router = $this->container->get('router');
if ($router instanceof WarmableInterface) {
return $router->warmUp($cacheDir, $buildDir);
}
throw new \LogicException(\sprintf('The router "%s" cannot be warmed up because it does not implement "%s".', get_debug_type($router), WarmableInterface::class));
}
public function isOptional(): bool
{
return true;
}
public static function getSubscribedServices(): array
{
return [
'router' => RouterInterface::class,
];
}
}

View File

@@ -0,0 +1,85 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\LoaderChain;
use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface;
use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
/**
* Warms up XML and YAML serializer metadata.
*
* @author Titouan Galopin <galopintitouan@gmail.com>
*
* @final since Symfony 7.1
*/
class SerializerCacheWarmer extends AbstractPhpFileCacheWarmer
{
/**
* @param LoaderInterface[] $loaders The serializer metadata loaders
* @param string $phpArrayFile The PHP file where metadata are cached
*/
public function __construct(
private array $loaders,
string $phpArrayFile,
) {
parent::__construct($phpArrayFile);
}
protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter, ?string $buildDir = null): bool
{
if (!$buildDir) {
return false;
}
if (!$this->loaders) {
return true;
}
$metadataFactory = new CacheClassMetadataFactory(new ClassMetadataFactory(new LoaderChain($this->loaders)), $arrayAdapter);
foreach ($this->extractSupportedLoaders($this->loaders) as $loader) {
foreach ($loader->getMappedClasses() as $mappedClass) {
try {
$metadataFactory->getMetadataFor($mappedClass);
} catch (\Exception $e) {
$this->ignoreAutoloadException($mappedClass, $e);
}
}
}
return true;
}
/**
* @param LoaderInterface[] $loaders
*
* @return XmlFileLoader[]|YamlFileLoader[]
*/
private function extractSupportedLoaders(array $loaders): array
{
$supportedLoaders = [];
foreach ($loaders as $loader) {
if ($loader instanceof XmlFileLoader || $loader instanceof YamlFileLoader) {
$supportedLoaders[] = $loader;
} elseif ($loader instanceof LoaderChain) {
$supportedLoaders = array_merge($supportedLoaders, $this->extractSupportedLoaders($loader->getLoaders()));
}
}
return $supportedLoaders;
}
}

View File

@@ -0,0 +1,61 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Generates the catalogues for translations.
*
* @author Xavier Leune <xavier.leune@gmail.com>
*
* @final since Symfony 7.1
*/
class TranslationsCacheWarmer implements CacheWarmerInterface, ServiceSubscriberInterface
{
private TranslatorInterface $translator;
/**
* As this cache warmer is optional, dependencies should be lazy-loaded, that's why a container should be injected.
*/
public function __construct(
private ContainerInterface $container,
) {
}
public function warmUp(string $cacheDir, ?string $buildDir = null): array
{
$this->translator ??= $this->container->get('translator');
if ($this->translator instanceof WarmableInterface) {
return $this->translator->warmUp($cacheDir, $buildDir);
}
return [];
}
public function isOptional(): bool
{
return true;
}
public static function getSubscribedServices(): array
{
return [
'translator' => TranslatorInterface::class,
];
}
}

View File

@@ -0,0 +1,96 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
use Symfony\Component\Validator\Mapping\Loader\LoaderChain;
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
use Symfony\Component\Validator\ValidatorBuilder;
/**
* Warms up XML and YAML validator metadata.
*
* @author Titouan Galopin <galopintitouan@gmail.com>
*
* @final since Symfony 7.1
*/
class ValidatorCacheWarmer extends AbstractPhpFileCacheWarmer
{
/**
* @param string $phpArrayFile The PHP file where metadata are cached
*/
public function __construct(
private ValidatorBuilder $validatorBuilder,
string $phpArrayFile,
) {
parent::__construct($phpArrayFile);
}
protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter, ?string $buildDir = null): bool
{
if (!$buildDir) {
return false;
}
$loaders = $this->validatorBuilder->getLoaders();
$metadataFactory = new LazyLoadingMetadataFactory(new LoaderChain($loaders), $arrayAdapter);
foreach ($this->extractSupportedLoaders($loaders) as $loader) {
foreach ($loader->getMappedClasses() as $mappedClass) {
try {
if ($metadataFactory->hasMetadataFor($mappedClass)) {
$metadataFactory->getMetadataFor($mappedClass);
}
} catch (\Exception $e) {
$this->ignoreAutoloadException($mappedClass, $e);
}
}
}
return true;
}
/**
* @return string[] A list of classes to preload on PHP 7.4+
*/
protected function warmUpPhpArrayAdapter(PhpArrayAdapter $phpArrayAdapter, array $values): array
{
// make sure we don't cache null values
$values = array_filter($values, fn ($val) => null !== $val);
return parent::warmUpPhpArrayAdapter($phpArrayAdapter, $values);
}
/**
* @param LoaderInterface[] $loaders
*
* @return XmlFileLoader[]|YamlFileLoader[]
*/
private function extractSupportedLoaders(array $loaders): array
{
$supportedLoaders = [];
foreach ($loaders as $loader) {
if ($loader instanceof XmlFileLoader || $loader instanceof YamlFileLoader) {
$supportedLoaders[] = $loader;
} elseif ($loader instanceof LoaderChain) {
$supportedLoaders = array_merge($supportedLoaders, $this->extractSupportedLoaders($loader->getLoaders()));
}
}
return $supportedLoaders;
}
}