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

Commitcb0f550

Browse files
committed
[FrameworkBundle] Introduce a cache warmer for Serializer based on PhpArrayAdapter
1 parent983b560 commitcb0f550

File tree

12 files changed

+333
-32
lines changed

12 files changed

+333
-32
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
useSymfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
21+
useSymfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
22+
useSymfony\Component\Serializer\Mapping\Loader\LoaderChain;
23+
useSymfony\Component\Serializer\Mapping\Loader\LoaderInterface;
24+
useSymfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
25+
useSymfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
26+
27+
/**
28+
* Warms up XML and YAML serializer metadata.
29+
*
30+
* @author Titouan Galopin <galopintitouan@gmail.com>
31+
*/
32+
class SerializerCacheWarmerimplements CacheWarmerInterface
33+
{
34+
private$loaders;
35+
private$phpArrayFile;
36+
private$fallbackPool;
37+
38+
/**
39+
* @param LoaderInterface[] $loaders The serializer metadata loaders.
40+
* @param string $phpArrayFile The PHP file where metadata are cached.
41+
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered metadata are cached.
42+
*/
43+
publicfunction__construct(array$loaders,$phpArrayFile,CacheItemPoolInterface$fallbackPool)
44+
{
45+
$this->loaders =$loaders;
46+
$this->phpArrayFile =$phpArrayFile;
47+
if (!$fallbackPoolinstanceof AdapterInterface) {
48+
$fallbackPool =newProxyAdapter($fallbackPool);
49+
}
50+
$this->fallbackPool =$fallbackPool;
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
publicfunctionwarmUp($cacheDir)
57+
{
58+
$adapter =newPhpArrayAdapter($this->phpArrayFile,$this->fallbackPool);
59+
$arrayPool =newArrayAdapter(0,false);
60+
61+
$metadataFactory =newCacheClassMetadataFactory(
62+
newClassMetadataFactory(newLoaderChain($this->loaders)),
63+
$arrayPool
64+
);
65+
66+
foreach ($this->extractSupportedLoaders($this->loaders)as$loader) {
67+
foreach ($loader->getMappedClasses()as$mappedClass) {
68+
$metadataFactory->getMetadataFor($mappedClass);
69+
}
70+
}
71+
72+
$values =$arrayPool->getValues();
73+
$adapter->warmUp($values);
74+
75+
foreach ($valuesas$k =>$v) {
76+
$item =$this->fallbackPool->getItem($k);
77+
$this->fallbackPool->saveDeferred($item->set($v));
78+
}
79+
$this->fallbackPool->commit();
80+
}
81+
82+
/**
83+
* {@inheritdoc}
84+
*/
85+
publicfunctionisOptional()
86+
{
87+
returntrue;
88+
}
89+
90+
/**
91+
* @param LoaderInterface[] $loaders
92+
*
93+
* @return XmlFileLoader[]|YamlFileLoader[]
94+
*/
95+
privatefunctionextractSupportedLoaders(array$loaders)
96+
{
97+
$supportedLoaders =array();
98+
99+
foreach ($loadersas$loader) {
100+
if ($loaderinstanceof XmlFileLoader ||$loaderinstanceof YamlFileLoader) {
101+
$supportedLoaders[] =$loader;
102+
}elseif ($loaderinstanceof LoaderChain) {
103+
$supportedLoaders =array_merge($supportedLoaders,$this->extractSupportedLoaders($loader->getDelegatedLoaders()));
104+
}
105+
}
106+
107+
return$supportedLoaders;
108+
}
109+
}

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
useDoctrine\Common\Annotations\Reader;
1515
useSymfony\Component\Cache\Adapter\AdapterInterface;
16+
useSymfony\Component\Cache\DoctrineProvider;
1617
useSymfony\Component\DependencyInjection\ContainerBuilder;
1718
useSymfony\Component\DependencyInjection\ContainerInterface;
1819
useSymfony\Component\DependencyInjection\Definition;
@@ -1067,24 +1068,38 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
10671068
}
10681069

10691070
$chainLoader->replaceArgument(0,$serializerLoaders);
1071+
$container->getDefinition('serializer.mapping.cache_warmer')->replaceArgument(0,$serializerLoaders);
1072+
1073+
$cache =null;
10701074

10711075
if (isset($config['cache']) &&$config['cache']) {
10721076
@trigger_error('The "framework.serializer.cache" option is deprecated since Symfony 3.1 and will be removed in 4.0. Configure the "cache.serializer" service under "framework.cache.pools" instead.',E_USER_DEPRECATED);
10731077

1078+
$cache =newDefinition(DoctrineProvider::class,array(newReference($config['cache'])));
1079+
$cache->setPublic(false);
1080+
1081+
$this->addClassesToCompile(array(
1082+
'Symfony\Component\Cache\DoctrineProvider',
1083+
));
1084+
}elseif (!$container->getParameter('kernel.debug')) {
1085+
$cache =newReference('serializer.mapping.cache.symfony');
1086+
1087+
$this->addClassesToCompile(array(
1088+
'Symfony\Component\Cache\Adapter\PhpArrayAdapter',
1089+
));
1090+
}
1091+
1092+
if ($cache) {
10741093
$container->setParameter(
10751094
'serializer.mapping.cache.prefix',
10761095
'serializer_'.$this->getKernelRootHash($container)
10771096
);
10781097

1079-
$container->getDefinition('serializer.mapping.class_metadata_factory')->replaceArgument(
1080-
1,newReference($config['cache'])
1081-
);
1082-
}elseif (!$container->getParameter('kernel.debug')) {
10831098
$cacheMetadataFactory =newDefinition(
10841099
CacheClassMetadataFactory::class,
10851100
array(
10861101
newReference('serializer.mapping.cache_class_metadata_factory.inner'),
1087-
newReference('cache.serializer'),
1102+
$cache,
10881103
)
10891104
);
10901105
$cacheMetadataFactory->setPublic(false);

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<parameters>
8+
<parameterkey="serializer.mapping.cache.file">%kernel.cache_dir%/serialization.php</parameter>
89
<parameterkey="serializer.mapping.cache.prefix" />
910
</parameters>
1011

@@ -39,6 +40,19 @@
3940
</service>
4041

4142
<!-- Cache-->
43+
<serviceid="serializer.mapping.cache_warmer"class="Symfony\Bundle\FrameworkBundle\CacheWarmer\SerializerCacheWarmer"public="false">
44+
<argumenttype="collection" /><!-- Loaders injected by the extension-->
45+
<argument>%serializer.mapping.cache.file%</argument>
46+
<argumenttype="service"id="cache.serializer" />
47+
<tagname="kernel.cache_warmer" />
48+
</service>
49+
50+
<serviceid="serializer.mapping.cache.symfony"class="Symfony\Component\Cache\Adapter\PhpArrayAdapter">
51+
<factoryclass="Symfony\Component\Cache\Adapter\PhpArrayAdapter"method="create" />
52+
<argument>%serializer.mapping.cache.file%</argument>
53+
<argumenttype="service"id="cache.serializer" />
54+
</service>
55+
4256
<serviceid="serializer.mapping.cache.doctrine.apc"class="Doctrine\Common\Cache\ApcCache"public="false">
4357
<callmethod="setNamespace">
4458
<argument>%serializer.mapping.cache.prefix%</argument>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\Tests\CacheWarmer;
13+
14+
useSymfony\Bundle\FrameworkBundle\CacheWarmer\SerializerCacheWarmer;
15+
useSymfony\Bundle\FrameworkBundle\Tests\TestCase;
16+
useSymfony\Component\Cache\Adapter\ArrayAdapter;
17+
useSymfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
18+
useSymfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
19+
20+
class SerializerCacheWarmerTestextends TestCase
21+
{
22+
publicfunctiontestWarmUp()
23+
{
24+
$loaders =array(
25+
newXmlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/person.xml'),
26+
newYamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/author.yml'),
27+
);
28+
29+
$file =sys_get_temp_dir().'/cache-serializer.php';
30+
@unlink($file);
31+
32+
$fallbackPool =newArrayAdapter();
33+
34+
$warmer =newSerializerCacheWarmer($loaders,$file,$fallbackPool);
35+
$warmer->warmUp(dirname($file));
36+
37+
$this->assertFileExists($file);
38+
39+
$values =require$file;
40+
41+
$this->assertInternalType('array',$values);
42+
$this->assertCount(2,$values);
43+
$this->assertArrayHasKey('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person',$values);
44+
$this->assertArrayHasKey('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author',$values);
45+
46+
$values =$fallbackPool->getValues();
47+
48+
$this->assertInternalType('array',$values);
49+
$this->assertCount(2,$values);
50+
$this->assertArrayHasKey('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person',$values);
51+
$this->assertArrayHasKey('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author',$values);
52+
}
53+
54+
publicfunctiontestWarmUpWithoutLoader()
55+
{
56+
$file =sys_get_temp_dir().'/cache-serializer-without-loader.php';
57+
@unlink($file);
58+
59+
$fallbackPool =newArrayAdapter();
60+
61+
$warmer =newSerializerCacheWarmer(array(),$file,$fallbackPool);
62+
$warmer->warmUp(dirname($file));
63+
64+
$this->assertFileExists($file);
65+
66+
$values =require$file;
67+
68+
$this->assertInternalType('array',$values);
69+
$this->assertCount(0,$values);
70+
71+
$values =$fallbackPool->getValues();
72+
73+
$this->assertInternalType('array',$values);
74+
$this->assertCount(0,$values);
75+
}
76+
}

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
useSymfony\Component\Cache\Adapter\ProxyAdapter;
2222
useSymfony\Component\Cache\Adapter\RedisAdapter;
2323
useSymfony\Component\DependencyInjection\ContainerBuilder;
24+
useSymfony\Component\DependencyInjection\Definition;
2425
useSymfony\Component\DependencyInjection\DefinitionDecorator;
2526
useSymfony\Component\DependencyInjection\Loader\ClosureLoader;
2627
useSymfony\Component\DependencyInjection\ParameterBag\ParameterBag;
@@ -543,7 +544,11 @@ public function testObjectNormalizerRegistered()
543544
publicfunctiontestSerializerCacheActivated()
544545
{
545546
$container =$this->createContainerFromFile('serializer_enabled');
547+
546548
$this->assertTrue($container->hasDefinition('serializer.mapping.cache_class_metadata_factory'));
549+
550+
$cache =$container->getDefinition('serializer.mapping.cache_class_metadata_factory')->getArgument(1);
551+
$this->assertEquals(newReference('serializer.mapping.cache.symfony'),$cache);
547552
}
548553

549554
publicfunctiontestSerializerCacheDisabled()
@@ -561,8 +566,10 @@ public function testDeprecatedSerializerCacheOption()
561566
ErrorAssert::assertDeprecationsAreTriggered('The "framework.serializer.cache" option is deprecated',function () {
562567
$container =$this->createContainerFromFile('serializer_legacy_cache',array('kernel.debug' =>true,'kernel.container_class' =>__CLASS__));
563568

564-
$this->assertFalse($container->hasDefinition('serializer.mapping.cache_class_metadata_factory'));
565-
$this->assertEquals(newReference('foo'),$container->getDefinition('serializer.mapping.class_metadata_factory')->getArgument(1));
569+
$this->assertTrue($container->hasDefinition('serializer.mapping.cache_class_metadata_factory'));
570+
571+
$cache =$container->getDefinition('serializer.mapping.cache_class_metadata_factory')->getArgument(1);
572+
$this->assertEquals(newReference('foo'),$cache->getArguments()[0]);
566573
});
567574
}
568575

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespaceSymfony\Bundle\FrameworkBundle\Tests\Fixtures\Serialization;
4+
5+
class Author
6+
{
7+
public$gender;
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespaceSymfony\Bundle\FrameworkBundle\Tests\Fixtures\Serialization;
4+
5+
class Person
6+
{
7+
public$gender;
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Serialization\Author:
2+
attributes:
3+
gender:
4+
groups:['group1', 'group2']
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" ?>
2+
<serializerxmlns="http://symfony.com/schema/dic/serializer-mapping"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping
5+
http://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
6+
>
7+
<classname="Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Serialization\Person">
8+
<attributename="gender">
9+
<group>group1</group>
10+
<group>group2</group>
11+
</attribute>
12+
</class>
13+
</serializer>

‎src/Symfony/Bundle/FrameworkBundle/composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"symfony/form":"~2.8|~3.0",
4848
"symfony/expression-language":"~2.8|~3.0",
4949
"symfony/process":"~2.8|~3.0",
50-
"symfony/serializer":"~2.8|^3.0",
50+
"symfony/serializer":"~3.2",
5151
"symfony/validator":"~3.1",
5252
"symfony/yaml":"~3.2",
5353
"symfony/property-info":"~2.8|~3.0",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp