8000 also apply for other cache warmers + fix array_filter · symfony/symfony@d8e5fa7 · GitHub
[go: up one dir, main page]

Skip to content

Commit d8e5fa7

Browse files
committed
also apply for other cache warmers + fix array_filter
1 parent 9b84d72 commit d8e5fa7

File tree

5 files changed

+150
-155
lines changed

5 files changed

+150
-155
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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\Bundle\FrameworkBundle\CacheWarmer;
13+
14+
use Psr\Cache\CacheItemPoolInterface;
15+
use Symfony\Component\Cache\Adapter\AdapterInterface;
16+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
17+
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
18+
use Symfony\Component\Cache\Adapter\ProxyAdapter;
19+
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
20+
21+
abstract class AbstractPhpFileCacheWarmer implements CacheWarmerInterface
22+
{
23+
protected $phpArrayFile;
24+
protected $fallbackPool;
25+
26+
/**
27+
* @param string $phpArrayFile The PHP file where metadata are cached
28+
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered metadata are cached
29+
*/
30+
public function __construct($phpArrayFile, CacheItemPoolInterface $fallbackPool)
31+
{
32+
$this->phpArrayFile = $phpArrayFile;
33+
if (!$fallbackPool instanceof AdapterInterface) {
34+
$fallbackPool = new ProxyAdapter($fallbackPool);
35+
}
36+
$this->fallbackPool = $fallbackPool;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function warmUp($cacheDir)
43+
{
44+
$phpArrayAdapter = new PhpArrayAdapter($this->phpArrayFile, $this->fallbackPool);
45+
$arrayAdapter = new ArrayAdapter();
46+
47+
spl_autoload_register(array($phpArrayAdapter, 'throwOnRequiredClass'));
48+
try {
49+
if (false === $this->doWarmUp($cacheDir, $phpArrayAdapter, $arrayAdapter)) {
50+
return;
51+
}
52+
} finally {
53+
spl_autoload_unregister(array($phpArrayAdapter, 'throwOnRequiredClass'));
54+
}
55+
56+
// the ArrayAdapter stores the values serialized as the MetaData objects
57+
// are mutated inside LazyLoadingMetadataFactory after being written into the cache
58+
// so here we un-serialize the values first
59+
$values = array_map(function ($val) { return unserialize($val); }, array_filter($arrayAdapter->getValues()));
60+
$phpArrayAdapter->warmUp($values);
61+
62+
foreach ($values as $k => $v) {
63+
$item = $this->fallbackPool->getItem($k);
64+
$this->fallbackPool->saveDeferred($item->set($v));
65+
}
66+
$this->fallbackPool->commit();
67+
}
68+
69+
/**
70+
* @param string $cacheDir
71+
* @param PhpArrayAdapter $phpArrayAdapter
72+
* @param ArrayAdapter $arrayAdapter
73+
*
74+
* @return bool|void false if there is nothing to warm-up
75+
*/
76+
abstract protected function doWarmUp($cacheDir, PhpArrayAdapter $phpArrayAdapter, ArrayAdapter $arrayAdapter);
77+
}

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,19 @@
1515
use Doctrine\Common\Annotations\CachedReader;
1616
use Doctrine\Common\Annotations\Reader;
1717
use Psr\Cache\CacheItemPoolInterface;
18-
use Symfony\Component\Cache\Adapter\AdapterInterface;
1918
use Symfony\Component\Cache\Adapter\ArrayAdapter;
2019
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
21-
use Symfony\Component\Cache\Adapter\ProxyAdapter;
2220
use Symfony\Component\Cache\DoctrineProvider;
23-
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
2421

2522
/**
2623
* Warms up annotation caches for classes found in composer's autoload class map
2724
* and declared in DI bundle extensions using the addAnnotatedClassesToCache method.
2825
*
2926
* @author Titouan Galopin <galopintitouan@gmail.com>
3027
*/
31-
class AnnotationsCacheWarmer implements CacheWarmerInterface
28+
class AnnotationsCacheWarmer extends AbstractPhpFileCacheWarmer
3229
{
3330
private $annotationReader;
34-
private $phpArrayFile;
35-
private $fallbackPool;
3631

3732
/**
3833
* @param Reader $annotationReader
@@ -41,71 +36,50 @@ class AnnotationsCacheWarmer implements CacheWarmerInterface
4136
*/
4237
public function __construct(Reader $annotationReader, $phpArrayFile, CacheItemPoolInterface $fallbackPool)
4338
{
39+
parent::__construct($phpArrayFile, $fallbackPool);
4440
$this->annotationReader = $annotationReader;
45-
$this->phpArrayFile = $phpArrayFile;
46-
if (!$fallbackPool instanceof AdapterInterface) {
47-
$fallbackPool = new ProxyAdapter($fallbackPool);
48-
}
49-
$this->fallbackPool = $fallbackPool;
5041
}
5142

5243
/**
5344
* {@inheritdoc}
5445
*/
55-
public function warmUp($cacheDir)
46+
public function isOptional()
47+
{
48+
return true;
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
protected function doWarmUp($cacheDir, PhpArrayAdapter $phpArrayAdapter, ArrayAdapter $arrayAdapter)
5655
{
57-
$adapter = new PhpArrayAdapter($this->phpArrayFile, $this->fallbackPool);
5856
$annotatedClassPatterns = $cacheDir.'/annotations.map';
5957

6058
if (!is_file($annotatedClassPatterns)) {
61-
$adapter->warmUp(array());
59+
$phpArrayAdapter->warmUp(array());
6260

63-
return;
61+
return false;
6462
}
6563

6664
$annotatedClasses = include $annotatedClassPatterns;
67-
68-
$arrayPool = new ArrayAdapter(0, false);
69-
$reader = new CachedReader($this->annotationReader, new DoctrineProvider($arrayPool));
70-
71-
spl_autoload_register(array($adapter, 'throwOnRequiredClass'));
72-
try {
73-
foreach ($annotatedClasses as $class) {
74-
try {
75-
$this->readAllComponents($reader, $class);
76-
} catch (\ReflectionException $e) {
77-
// ignore failing reflection
78-
} catch (AnnotationException $e) {
79-
/*
80-
* Ignore any AnnotationException to not break the cache warming process if an Annotation is badly
81-
* configured or could not be found / read / etc.
82-
*
83-
* In particular cases, an Annotation in your code can be used and defined only for a specific
84-
* environment but is always added to the annotations.map file by some Symfony default behaviors,
85-
* and you always end up with a not found Annotation.
86-
*/
87-
}
65+
$reader = new CachedReader($this->annotationReader, new DoctrineProvider($arrayAdapter));
66+
67+
foreach ($annotatedClasses as $class) {
68+
try {
69+
$this->readAllComponents($reader, $class);
70+
} catch (\ReflectionException $e) {
71+
// ignore failing reflection
72+
} catch (AnnotationException $e) {
73+
/*
74+
* Ignore any AnnotationException to not break the cache warming process if an Annotation is badly
75+
* configured or could not be found / read / etc.
76+
*
77+
* In particular cases, an Annotation in your code can be used and defined only for a specific
78+
* environment but is always added to the annotations.map file by some Symfony default behaviors,
79+
* and you always end up with a not found Annotation.
80+
*/
8881
}
89-
} finally {
90-
spl_autoload_unregister(array($adapter, 'throwOnRequiredClass'));
9182
}
92-
93-
$values = $arrayPool->getValues();
94-
$adapter->warmUp($values);
95-
96-
foreach ($values as $k => $v) {
97-
$item = $this->fallbackPool->getItem($k);
98-
$this->fallbackPool->saveDeferred($item->set($v));
99-
}
100-
$this->fallbackPool->commit();
101-
}
102-
103-
/**
104-
* {@inheritdoc}
105-
*/
106-
public function isOptional()
107-
{
108-
return true;
10983
}
11084

11185
private function readAllComponents(Reader $reader, $class)

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerializerCacheWarmer.php

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313

1414
use Doctrine\Common\Annotations\AnnotationException;
1515
use Psr\Cache\CacheItemPoolInterface;
16-
use Symfony\Component\Cache\Adapter\AdapterInterface;
1716
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1817
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
19-
use Symfony\Component\Cache\Adapter\ProxyAdapter;
20-
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
2118
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
2219
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
2320
use Symfony\Component\Serializer\Mapping\Loader\LoaderChain;
@@ -30,11 +27,9 @@
3027
*
3128
* @author Titouan Galopin <galopintitouan@gmail.com>
3229
*/
33-
class SerializerCacheWarmer implements CacheWarmerInterface
30+
class SerializerCacheWarmer extends AbstractPhpFileCacheWarmer
3431
{
3532
private $loaders;
36-
private $phpArrayFile;
37-
private $fallbackPool;
3833

3934
/**
4035
* @param LoaderInterface[] $loaders The serializer metadata loaders
@@ -43,61 +38,40 @@ class SerializerCacheWarmer implements CacheWarmerInterface
4338
*/
4439
public function __construct(array $loaders, $phpArrayFile, CacheItemPoolInterface $fallbackPool)
4540
{
41+
parent::__construct($phpArrayFile, $fallbackPool);
4642
$this->loaders = $loaders;
47-
$this->phpArrayFile = $phpArrayFile;
48-
if (!$fallbackPool instanceof AdapterInterface) {
49-
$fallbackPool = new ProxyAdapter($fallbackPool);
50-
}
51-
$this->fallbackPool = $fallbackPool;
5243
}
5344

5445
/**
5546
* {@inheritdoc}
5647
*/
57-
public function warmUp($cacheDir)
48+
public function isOptional()
49+
{
50+
return true;
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
protected function doWarmUp($cacheDir, PhpArrayAdapter $phpArrayAdapter, ArrayAdapter $arrayAdapter)
5857
{
5958
if (!class_exists(CacheClassMetadataFactory::class) || !method_exists(XmlFileLoader::class, 'getMappedClasses') || !method_exists(YamlFileLoader::class, 'getMappedClasses')) {
60-
return;
59+
return false;
6160
}
6261

63-
$adapter = new PhpArrayAdapter($this->phpArrayFile, $this->fallbackPool);
64-
$arrayPool = new ArrayAdapter(0, false);
62+
$metadataFactory = new CacheClassMetadataFactory(new ClassMetadataFactory(new LoaderChain($this->loaders)), $arrayAdapter);
6563

66-
$metadataFactory = new CacheClassMetadataFactory(new ClassMetadataFactory(new LoaderChain($this->loaders)), $arrayPool);
67-
68-
spl_autoload_register(array($adapter, 'throwOnRequiredClass'));
69-
try {
70-
foreach ($this->extractSupportedLoaders($this->loaders) as $loader) {
71-
foreach ($loader->getMappedClasses() as $mappedClass) {
72-
try {
73-
$metadataFactory->getMetadataFor($mappedClass);
74-
} catch (\ReflectionException $e) {
75-
// ignore failing reflection
76-
} catch (AnnotationException $e) {
77-
// ignore failing annotations
78-
}
64+
foreach ($this->extractSupportedLoaders($this->loaders) as $loader) {
65+
foreach ($loader->getMappedClasses() as $mappedClass) {
66+
try {
67+
$metadataFactory->getMetadataFor($mappedClass);
68+
} catch (\ReflectionException $e) {
69+
// ignore failing reflection
70+
} catch (AnnotationException $e) {
71+
// ignore failing annotations
7972
}
8073
}
81-
} finally {
82-
spl_autoload_unregister(array($adapter, 'throwOnRequiredClass'));
8374
}
84-
85-
$values = $arrayPool->getValues();
86-
$adapter->warmUp($values);
87-
88-
foreach ($values as $k => $v) {
89-
$item = $this->fallbackPool->getItem($k);
90-
$this->fallbackPool->saveDeferred($item->set($v));
91-
}
92-
$this->fallbackPool->commit();
93-
}
94-
95-
/**
96-
* {@inheritdoc}
97-
*/
98-
public function isOptional()
99-
{
100-
return true;
10175
}
10276

10377
/**

0 commit comments

Comments
 (0)
0