Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e4177a7

Browse files
committed
feature #18544 [FrameworkBundle] Fallback to default cache system in production for validation (tgalopin)
This PR was merged into the 3.1-dev branch. Discussion ---------- [FrameworkBundle] Fallback to default cache system in production for validation | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | WIP | Fixed tickets | - | License | MIT | Doc PR | - This PR proposes a default fallback to filesystem cache for some services if the APC cache is not enabled in production. In other words, if the following part of `config_prod.yml` file is not uncommented, the filesystem will be used: ``` yaml #framework: # validation: # cache: validator.mapping.cache.doctrine.apc # serializer: # cache: serializer.mapping.cache.doctrine.apc # # ... other services ``` Commits ------- 1a65595 [FrameworkBundle] Fallback to default cache system in production for validation
2 parents 8d6670b + 1a65595 commit e4177a7

File tree

9 files changed

+43
-19
lines changed

9 files changed

+43
-19
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ class CachePoolPass implements CompilerPassInterface
2626
*/
2727
public function process(ContainerBuilder $container)
2828
{
29+
$namespaceSuffix = '';
30+
31+
foreach (array('name', 'root_dir', 'environment', 'debug') as $key) {
32+
if ($container->hasParameter('kernel.'.$key)) {
33+
$namespaceSuffix .= '.'.$container->getParameter('kernel.'.$key);
34+
}
35+
}
36+
2937
$attributes = array(
3038
'provider',
3139
'namespace',
@@ -36,9 +44,7 @@ public function process(ContainerBuilder $container)
3644
if ($pool->isAbstract()) {
3745
continue;
3846
}
39-
if (!isset($tags[0]['namespace'])) {
40-
$tags[0]['namespace'] = $this->getNamespace($id);
41-
}
47+
$tags[0]['namespace'] = $this->getNamespace($namespaceSuffix, isset($tags[0]['namespace']) ? $tags[0]['namespace'] : $id);
4248
while ($adapter instanceof DefinitionDecorator) {
4349
$adapter = $container->findDefinition($adapter->getParent());
4450
if ($t = $adapter->getTag('cache.pool')) {
@@ -72,8 +78,8 @@ public function process(ContainerBuilder $container)
7278
}
7379
}
7480

75-
private function getNamespace($id)
81+
private function getNamespace($namespaceSuffix, $id)
7682
{
77-
return substr(str_replace('/', '-', base64_encode(md5('symfony.'.$id, true))), 0, 10);
83+
return substr(str_replace('/', '-', base64_encode(md5($id.$namespaceSuffix, true))), 0, 10);
7884
}
7985
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ private function addValidationSection(ArrayNodeDefinition $rootNode)
468468
->info('validation configuration')
469469
->canBeEnabled()
470470
->children()
471-
->scalarNode('cache')->end()
471+
->scalarNode('cache')->defaultValue('validator.mapping.cache.symfony')->end()
472472
->booleanNode('enable_annotations')->defaultFalse()->end()
473473
->arrayNode('static_method')
474474
->defaultValue(array('loadValidatorMetadata'))

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public function load(array $configs, ContainerBuilder $container)
7171
// Property access is used by both the Form and the Validator component
7272
$loader->load('property_access.xml');
7373

74+
// Load Cache configuration first as it is used by other components
75+
$loader->load('cache_pools.xml');
76+
7477
$configuration = $this->getConfiguration($configs, $container);
7578
$config = $this->processConfiguration($configuration, $configs);
7679

@@ -781,7 +784,7 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
781784
}
782785
}
783786

784-
if (isset($config['cache'])) {
787+
if (!$container->getParameter('kernel.debug')) {
785788
$container->setParameter(
786789
'validator.mapping.cache.prefix',
787790
'validator_'.$this->getKernelRootHash($container)
@@ -1019,8 +1022,6 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild
10191022

10201023
private function registerCacheConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
10211024
{
1022-
$loader->load('cache_pools.xml');
1023-
10241025
foreach ($config['pools'] as $name => $poolConfig) {
10251026
$poolDefinition = new DefinitionDecorator($poolConfig['adapter']);
10261027
$poolDefinition->setPublic($poolConfig['public']);

src/Symfony/Bundle/FrameworkBundle/Resources/config/cache_pools.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
<tag name="cache.pool" clearer="cache.default_pools_clearer" />
2222
</service>
2323

24+
<service id="cache.pool.validator" parent="cache.adapter.local" public="false">
25+
<tag name="cache.pool" clearer="cache.default_pools_clearer" />
26+
</service>
27+
2428
<service id="cache.adapter.apcu" class="Symfony\Component\Cache\Adapter\ApcuAdapter" abstract="true">
2529
<argument /> <!-- namespace -->
2630
<argument /> <!-- default lifetime -->

src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828

2929
<service id="validator.mapping.class_metadata_factory" alias="validator" public="false" />
3030

31+
<service id="validator.mapping.cache.symfony" class="Symfony\Component\Validator\Mapping\Cache\Psr6Cache" public="false">
32+
<argument type="service" id="cache.pool.validator" />
33+
</service>
34+
3135
<service id="validator.mapping.cache.doctrine.apc" class="Symfony\Component\Validator\Mapping\Cache\DoctrineCache" public="false">
3236
<argument type="service">
3337
<service class="Doctrine\Common\Cache\ApcCache">

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CachePoolPassTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function testNamespaceArgumentIsReplaced()
4141

4242
$this->cachePoolPass->process($container);
4343

44-
$this->assertSame('yRnzIIVLvL', $cachePool->getArgument(0));
44+
$this->assertSame('VcRIZlUhEv', $cachePool->getArgument(0));
4545
}
4646

4747
public function testArgsAreReplaced()
@@ -61,7 +61,7 @@ public function testArgsAreReplaced()
6161

6262
$this->assertInstanceOf(Reference::class, $cachePool->getArgument(0));
6363
$this->assertSame('foobar', (string) $cachePool->getArgument(0));
64-
$this->assertSame('yRnzIIVLvL', $cachePool->getArgument(1));
64+
$this->assertSame('VcRIZlUhEv', $cachePool->getArgument(1));
6565
$this->assertSame(3, $cachePool->getArgument(2));
6666
}
6767

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ protected static function getBundleDefaultConfig()
211211
'static_method' => array('loadValidatorMetadata'),
212212
'translation_domain' => 'validators',
213213
'strict_email' => false,
214+
'cache' => 'validator.mapping.cache.symfony',
214215
),
215216
'annotations' => array(
216217
'cache' => 'file',

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public function testValidation()
323323

324324
public function testValidationService()
325325
{
326-
$container = $this->createContainerFromFile('validation_annotations');
326+
$container = $this->createContainerFromFile('validation_annotations', array('kernel.charset' => 'UTF-8'), false);
327327

328328
$this->assertInstanceOf('Symfony\Component\Validator\Validator\ValidatorInterface', $container->get('validator'));
329329
}
@@ -350,11 +350,13 @@ public function testValidationAnnotations()
350350

351351
$calls = $container->getDefinition('validator.builder')->getMethodCalls();
352352

353-
$this->assertCount(6, $calls);
353+
$this->assertCount(7, $calls);
354354
$this->assertSame('enableAnnotationMapping', $calls[4][0]);
355355
$this->assertEquals(array(new Reference('annotation_reader')), $calls[4][1]);
356356
$this->assertSame('addMethodMapping', $calls[5][0]);
357357
$this->assertSame(array('loadValidatorMetadata'), $calls[5][1]);
358+
$this->assertSame('setMetadataCache', $calls[6][0]);
359+
$this->assertEquals(array(new Reference('validator.mapping.cache.symfony')), $calls[6][1]);
358360
// no cache this time
359361
}
360362

@@ -368,12 +370,14 @@ public function testValidationPaths()
368370

369371
$calls = $container->getDefinition('validator.builder')->getMethodCalls();
370372

371-
$this->assertCount(7, $calls);
373+
$this->assertCount(8, $calls);
372374
$this->assertSame('addXmlMappings', $calls[3][0]);
373375
$this->assertSame('addYamlMappings', $calls[4][0]);
374376
$this->assertSame('enableAnnotationMapping', $calls[5][0]);
375377
$this->assertSame('addMethodMapping', $calls[6][0]);
376378
$this->assertSame(array('loadValidatorMetadata'), $calls[6][1]);
379+
$this->assertSame('setMetadataCache', $calls[7][0]);
380+
$this->assertEquals(array(new Reference('validator.mapping.cache.symfony')), $calls[7][1]);
377381

378382
$xmlMappings = $calls[3][1][0];
379383
$this->assertCount(2, $xmlMappings);
@@ -397,8 +401,10 @@ public function testValidationNoStaticMethod()
397401

398402
$calls = $container->getDefinition('validator.builder')->getMethodCalls();
399403

400-
$this->assertCount(4, $calls);
404+
$this->assertCount(5, $calls);
401405
$this->assertSame('addXmlMappings', $calls[3][0]);
406+
$this->assertSame('setMetadataCache', $calls[4][0]);
407+
$this->assertEquals(array(new Reference('validator.mapping.cache.symfony')), $calls[4][1]);
402408
// no cache, no annotations, no static methods
403409
}
404410

@@ -594,7 +600,7 @@ protected function createContainer(array $data = array())
594600
), $data)));
595601
}
596602

597-
protected function createContainerFromFile($file, $data = array())
603+
protected function createContainerFromFile($file, $data = array(), $resetCompilerPasses = true)
598604
{
599605
$cacheKey = md5(get_class($this).$file.serialize($data));
600606
if (isset(self::$containerCache[$cacheKey])) {
@@ -604,8 +610,10 @@ protected function createContainerFromFile($file, $data = array())
604610
$container->registerExtension(new FrameworkExtension());
605611
$this->loadFromFile($container, $file);
606612

607-
$container->getCompilerPassConfig()->setOptimizationPasses(array());
608-
$container->getCompilerPassConfig()->setRemovingPasses(array());
613+
if ($resetCompilerPasses) {
614+
$container->getCompilerPassConfig()->setOptimizationPasses(array());
615+
$container->getCompilerPassConfig()->setRemovingPasses(array());
616+
}
609617
$container->compile();
610618

611619
return self::$containerCache[$cacheKey] = $container;

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"symfony/expression-language": "~2.8|~3.0",
4949
"symfony/process": "~2.8|~3.0",
5050
"symfony/serializer": "~2.8|^3.0",
51-
"symfony/validator": "~2.8|~3.0",
51+
"symfony/validator": "~3.1",
5252
"symfony/yaml": "~2.8|~3.0",
5353
"symfony/property-info": "~2.8|~3.0",
5454
"phpdocumentor/reflection-docblock": "^3.0",

0 commit comments

Comments
 (0)