Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit8753b06

Browse files
committed
also apply for other cache warmers + fix array_filter
1 parent9b84d72 commit8753b06

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+
namespaceSymfony\Bundle\FrameworkBundle\CacheWarmer;
13+
14+
usePsr\Cache\CacheItemPoolInterface;
15+
useSymfony\Component\Cache\Adapter\AdapterInterface;
16+
useSymfony\Component\Cache\Adapter\ArrayAdapter;
17+
useSymfony\Component\Cache\Adapter\PhpArrayAdapter;
18+
useSymfony\Component\Cache\Adapter\ProxyAdapter;
19+
useSymfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
20+
21+
abstractclass AbstractPhpFileCacheWarmerimplements 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+
publicfunction__construct($phpArrayFile,CacheItemPoolInterface$fallbackPool)
31+
{
32+
$this->phpArrayFile =$phpArrayFile;
33+
if (!$fallbackPoolinstanceof AdapterInterface) {
34+
$fallbackPool =newProxyAdapter($fallbackPool);
35+
}
36+
$this->fallbackPool =$fallbackPool;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
publicfunctionwarmUp($cacheDir)
43+
{
44+
$phpArrayAdapter =newPhpArrayAdapter($this->phpArrayFile,$this->fallbackPool);
45+
$arrayAdapter =newArrayAdapter();
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
57+
// to avoid mutation of the data after it was written to the cache
58+
// so here we un-serialize the values first
59+
$values =array_map(function ($val) {returnunserialize($val); },array_filter($arrayAdapter->getValues()));
60+
$phpArrayAdapter->warmUp($values);
61+
62+
foreach ($valuesas$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+
abstractprotectedfunctiondoWarmUp($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
useDoctrine\Common\Annotations\CachedReader;
1616
useDoctrine\Common\Annotations\Reader;
1717
usePsr\Cache\CacheItemPoolInterface;
18-
useSymfony\Component\Cache\Adapter\AdapterInterface;
1918
useSymfony\Component\Cache\Adapter\ArrayAdapter;
2019
useSymfony\Component\Cache\Adapter\PhpArrayAdapter;
21-
useSymfony\Component\Cache\Adapter\ProxyAdapter;
2220
useSymfony\Component\Cache\DoctrineProvider;
23-
useSymfony\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 AnnotationsCacheWarmerimplements CacheWarmerInterface
28+
class AnnotationsCacheWarmerextends 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
publicfunction__construct(Reader$annotationReader,$phpArrayFile,CacheItemPoolInterface$fallbackPool)
4338
{
39+
parent::__construct($phpArrayFile,$fallbackPool);
4440
$this->annotationReader =$annotationReader;
45-
$this->phpArrayFile =$phpArrayFile;
46-
if (!$fallbackPoolinstanceof AdapterInterface) {
47-
$fallbackPool =newProxyAdapter($fallbackPool);
48-
}
49-
$this->fallbackPool =$fallbackPool;
5041
}
5142

5243
/**
5344
* {@inheritdoc}
5445
*/
55-
publicfunctionwarmUp($cacheDir)
46+
publicfunctionisOptional()
47+
{
48+
returntrue;
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
protectedfunctiondoWarmUp($cacheDir,PhpArrayAdapter$phpArrayAdapter,ArrayAdapter$arrayAdapter)
5655
{
57-
$adapter =newPhpArrayAdapter($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+
returnfalse;
6462
}
6563

6664
$annotatedClasses =include$annotatedClassPatterns;
67-
68-
$arrayPool =newArrayAdapter(0,false);
69-
$reader =newCachedReader($this->annotationReader,newDoctrineProvider($arrayPool));
70-
71-
spl_autoload_register(array($adapter,'throwOnRequiredClass'));
72-
try {
73-
foreach ($annotatedClassesas$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 =newCachedReader($this->annotationReader,newDoctrineProvider($arrayAdapter));
66+
67+
foreach ($annotatedClassesas$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 ($valuesas$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-
publicfunctionisOptional()
107-
{
108-
returntrue;
10983
}
11084

11185
privatefunctionreadAllComponents(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
useDoctrine\Common\Annotations\AnnotationException;
1515
usePsr\Cache\CacheItemPoolInterface;
16-
useSymfony\Component\Cache\Adapter\AdapterInterface;
1716
useSymfony\Component\Cache\Adapter\ArrayAdapter;
1817
useSymfony\Component\Cache\Adapter\PhpArrayAdapter;
19-
useSymfony\Component\Cache\Adapter\ProxyAdapter;
20-
useSymfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
2118
useSymfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
2219
useSymfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
2320
useSymfony\Component\Serializer\Mapping\Loader\LoaderChain;
@@ -30,11 +27,9 @@
3027
*
3128
* @author Titouan Galopin <galopintitouan@gmail.com>
3229
*/
33-
class SerializerCacheWarmerimplements CacheWarmerInterface
30+
class SerializerCacheWarmerextends 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
publicfunction__construct(array$loaders,$phpArrayFile,CacheItemPoolInterface$fallbackPool)
4540
{
41+
parent::__construct($phpArrayFile,$fallbackPool);
4642
$this->loaders =$loaders;
47-
$this->phpArrayFile =$phpArrayFile;
48-
if (!$fallbackPoolinstanceof AdapterInterface) {
49-
$fallbackPool =newProxyAdapter($fallbackPool);
50-
}
51-
$this->fallbackPool =$fallbackPool;
5243
}
5344

5445
/**
5546
* {@inheritdoc}
5647
*/
57-
publicfunctionwarmUp($cacheDir)
48+
publicfunctionisOptional()
49+
{
50+
returntrue;
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
protectedfunctiondoWarmUp($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+
returnfalse;
6160
}
6261

63-
$adapter =newPhpArrayAdapter($this->phpArrayFile,$this->fallbackPool);
64-
$arrayPool =newArrayAdapter(0,false);
62+
$metadataFactory =newCacheClassMetadataFactory(newClassMetadataFactory(newLoaderChain($this->loaders)),$arrayAdapter);
6563

66-
$metadataFactory =newCacheClassMetadataFactory(newClassMetadataFactory(newLoaderChain($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 ($valuesas$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-
publicfunctionisOptional()
99-
{
100-
returntrue;
10175
}
10276

10377
/**

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp