Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[HttpKernel] allow cache warmers to add to the list of preloaded classes and files #36209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[HttpKernel] allow cache warmers to add to the list of preloaded clas…
…ses and files
  • Loading branch information
nicolas-grekas committed Apr 4, 2020
commit 8ab75d99d4cda5237b48ee3e4a1f361fe78ae8e5
2 changes: 2 additions & 0 deletions 2 UPGRADE-5.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ HttpFoundation
HttpKernel
----------

* Made `WarmableInterface::warmUp()` return a list of classes or files to preload on PHP 7.4+
not returning an array is deprecated
* Deprecated support for `service:action` syntax to reference controllers. Use `serviceOrFqcn::method` instead.

Mailer
Expand Down
1 change: 1 addition & 0 deletions 1 UPGRADE-6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ HttpFoundation
HttpKernel
----------

* Made `WarmableInterface::warmUp()` return a list of classes or files to preload on PHP 7.4+
* Removed support for `service:action` syntax to reference controllers. Use `serviceOrFqcn::method` instead.

Messenger
Expand Down
11 changes: 11 additions & 0 deletions 11 src/Symfony/Bridge/Doctrine/CacheWarmer/ProxyCacheWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ public function isOptional()

/**
* {@inheritdoc}
*
* @return string[] A list of files to preload on PHP 7.4+
*/
public function warmUp(string $cacheDir)
{
$files = [];
foreach ($this->registry->getManagers() as $em) {
// we need the directory no matter the proxy cache generation strategy
if (!is_dir($proxyCacheDir = $em->getConfiguration()->getProxyDir())) {
Expand All @@ -64,6 +67,14 @@ public function warmUp(string $cacheDir)
$classes = $em->getMetadataFactory()->getAllMetadata();

$em->getProxyFactory()->generateProxyClasses($classes);

foreach (scandir($proxyCacheDir) as $file) {
if (!is_dir($file = $proxyCacheDir.'/'.$file)) {
$files[] = $file;
}
}
}

return $files;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function isOptional()

/**
* {@inheritdoc}
*
* @return string[] A list of classes to preload on PHP 7.4+
*/
public function warmUp(string $cacheDir)
{
Expand All @@ -61,12 +63,15 @@ public function warmUp(string $cacheDir)
// so here we un-serialize the values first
$values = array_map(function ($val) { return null !== $val ? unserialize($val) : null; }, $arrayAdapter->getValues());

$this->warmUpPhpArrayAdapter(new PhpArrayAdapter($this->phpArrayFile, new NullAdapter()), $values);
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)
{
$phpArrayAdapter->warmUp($values);
return (array) $phpArrayAdapter->warmUp($values);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ public function __construct(Psr6CacheClearer $poolClearer, array $pools = [])

/**
* {@inheritdoc}
*
* @return string[]
*/
public function warmUp($cacheDirectory): void
public function warmUp($cacheDirectory): array
{
foreach ($this->pools as $pool) {
if ($this->poolClearer->hasPool($pool)) {
$this->poolClearer->clearPool($pool);
}
}

return [];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public function __construct(ContainerInterface $container)

/**
* {@inheritdoc}
*
* @return string[]
*/
public function warmUp(string $cacheDir)
{
$router = $this->container->get('router');

if ($router instanceof WarmableInterface) {
$router->warmUp($cacheDir);

return;
return (array) $router->warmUp($cacheDir);
}

throw new \LogicException(sprintf('The router "%s" cannot be warmed up because it does not implement "%s".', get_debug_type($router), WarmableInterface::class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function __construct(ContainerInterface $container)

/**
* {@inheritdoc}
*
* @return string[]
*/
public function warmUp(string $cacheDir)
{
Expand All @@ -43,8 +45,10 @@ public function warmUp(string $cacheDir)
}

if ($this->translator instanceof WarmableInterface) {
$this->translator->warmUp($cacheDir);
return (array) $this->translator->warmUp($cacheDir);
}

return [];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter)
return true;
}

/**
* @return string[] A list of classes to preload on PHP 7.4+
*/
protected function warmUpPhpArrayAdapter(PhpArrayAdapter $phpArrayAdapter, array $values)
{
// make sure we don't cache null values
parent::warmUpPhpArrayAdapter($phpArrayAdapter, array_filter($values));
return parent::warmUpPhpArrayAdapter($phpArrayAdapter, array_filter($values));
}

/**
Expand Down
13 changes: 11 additions & 2 deletions 13 src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Dumper\Preloader;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
Expand Down Expand Up @@ -117,7 +118,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$warmer = $kernel->getContainer()->get('cache_warmer');
// non optional warmers already ran during container compilation
$warmer->enableOnlyOptionalWarmers();
$warmer->warmUp($realCacheDir);
$preload = (array) $warmer->warmUp($warmupDir);

if (file_exists($preloadFile = $warmupDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) {
Preloader::append($preloadFile, $preload);
}
}
} else {
$fs->mkdir($warmupDir);
Expand Down Expand Up @@ -193,7 +198,11 @@ private function warmup(string $warmupDir, string $realCacheDir, bool $enableOpt
$warmer = $kernel->getContainer()->get('cache_warmer');
// non optional warmers already ran during container compilation
$warmer->enableOnlyOptionalWarmers();
$warmer->warmUp($warmupDir);
$preload = (array) $warmer->warmUp($warmupDir);

if (file_exists($preloadFile = $warmupDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) {
Preloader::append($preloadFile, $preload);
}
}

// fix references to cached files with the real cache directory name
Expand Down
12 changes: 7 additions & 5 deletions 12 src/Symfony/Bundle/FrameworkBundle/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,11 @@
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Router as BaseRouter;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

// Help opcache.preload discover always-needed symbols
class_exists(RedirectableCompiledUrlMatcher::class);
class_exists(Route::class);

/**
* This Router creates the Loader only when the cache is empty.
*
Expand Down Expand Up @@ -90,6 +85,8 @@ public function getRouteCollection()

/**
* {@inheritdoc}
*
* @return string[] A list of classes to preload on PHP 7.4+
*/
public function warmUp(string $cacheDir)
{
Expand All @@ -101,6 +98,11 @@ public function warmUp(string $cacheDir)
$this->getGenerator();

$this->setOption('cache_dir', $currentDir);

return [
$this->getOption('generator_class'),
$this->getOption('matcher_class'),
];
}

/**
Expand Down
4 changes: 4 additions & 0 deletions 4 src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public function __construct(ContainerInterface $container, MessageFormatterInter

/**
* {@inheritdoc}
*
* @return string[]
*/
public function warmUp(string $cacheDir)
{
Expand All @@ -113,6 +115,8 @@ public function warmUp(string $cacheDir)

$this->loadCatalogue($locale);
}

return [];
}

public function addResource(string $format, $resource, string $locale, string $domain = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ public function isOptional()
return true;
}

/**
* @return string[]
*/
public function warmUp(string $cacheDir)
{
foreach ($this->expressions as $expression) {
$this->expressionLanguage->parse($expression, ['token', 'user', 'object', 'subject', 'roles', 'request', 'trust_resolver']);
}

return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,31 @@ public function __construct(ContainerInterface $container, iterable $iterator)

/**
* {@inheritdoc}
*
* @return string[] A list of template files to preload on PHP 7.4+
*/
public function warmUp(string $cacheDir)
{
if (null === $this->twig) {
$this->twig = $this->container->get('twig');
}

$files = [];

foreach ($this->iterator as $template) {
try {
$this->twig->load($template);
$template = $this->twig->load($template);

if (\is_callable([$template, 'unwrap'])) {
$files[] = (new \ReflectionClass($template->unwrap()))->getFileName();
}
} catch (Error $e) {
// problem during compilation, give up
// might be a syntax error or a non-Twig template
}
}

return $files;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion 7 src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ public function clear(string $prefix = '')
* Store an array of cached values.
*
* @param array $values The cached values
*
* @return string[] A list of classes to preload on PHP 7.4+
*/
public function warmUp(array $values)
{
Expand All @@ -314,6 +316,7 @@ public function warmUp(array $values)
}
}

$preload = [];
$dumpedValues = '';
$dumpedMap = [];
$dump = <<<'EOF'
Expand All @@ -334,7 +337,7 @@ public function warmUp(array $values)
$value = "'N;'";
} elseif (\is_object($value) || \is_array($value)) {
try {
$value = VarExporter::export($value, $isStaticValue);
$value = VarExporter::export($value, $isStaticValue, $preload);
} catch (\Exception $e) {
throw new InvalidArgumentException(sprintf('Cache key "%s" has non-serializable "%s" value.', $key, get_debug_type($value)), 0, $e);
}
Expand Down Expand Up @@ -376,6 +379,8 @@ public function warmUp(array $values)
unset(self::$valuesCache[$this->file]);

$this->initialize();

return $preload;
}

/**
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CHANGELOG
* updated the signature of method `DeprecateTrait::deprecate()` to `DeprecateTrait::deprecation(string $package, string $version, string $message)`
* deprecated the `Psr\Container\ContainerInterface` and `Symfony\Component\DependencyInjection\ContainerInterface` aliases of the `service_container` service,
configure them explicitly instead
* added class `Symfony\Component\DependencyInjection\Dumper\Preloader` to help with preloading on PHP 7.4+

5.0.0
-----
Expand Down
27 changes: 23 additions & 4 deletions 27 src/Symfony/Component/DependencyInjection/Dumper/Preloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,31 @@

/**
* @author Nicolas Grekas <p@tchwork.com>
*
* @internal
*/
class Preloader
final class Preloader
{
public static function preload(array $classes)
public static function append(string $file, array $list): void
{
if (!file_exists($file)) {
throw new \LogicException(sprintf('File "%s" does not exist.', $file));
}

$cacheDir = \dirname($file);
$classes = [];

foreach ($list as $item) {
if (0 === strpos($item, $cacheDir)) {
file_put_contents($file, sprintf("require __DIR__.%s;\n", var_export(substr($item, \strlen($cacheDir)), true)), FILE_APPEND);
continue;
}

$classes[] = sprintf("\$classes[] = %s;\n", var_export($item, true));
}

file_put_contents($file, sprintf("\n\$classes = [];\n%sPreloader::preload(\$classes);\n", implode('', $classes)), FILE_APPEND);
}

public static function preload(array $classes): void
{
set_error_handler(function ($t, $m, $f, $l) {
if (error_reporting() & $t) {
Expand Down
2 changes: 2 additions & 0 deletions 2 src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ CHANGELOG
5.1.0
-----

* made `WarmableInterface::warmUp()` return a list of classes or files to preload on PHP 7.4+;
not returning an array is deprecated
* deprecated support for `service:action` syntax to reference controllers, use `serviceOrFqcn::method` instead
* allowed using public aliases to reference controllers
* added session usage reporting when the `_stateless` attribute of the request is set to `true`
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.