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

Commit e44bfdc

Browse filesBrowse files
[FrameworkBundle] Add cache-pool tag and wiring
1 parent 281eafa commit e44bfdc
Copy full SHA for e44bfdc

File tree

8 files changed

+133
-160
lines changed
Filter options

8 files changed

+133
-160
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CacheAdapterPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CacheAdapterPass.php
-88Lines changed: 0 additions & 88 deletions
This file was deleted.
+64Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\DefinitionDecorator;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
19+
/**
20+
* @author Nicolas Grekas <p@tchwork.com>
21+
*/
22+
class CachePoolPass implements CompilerPassInterface
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function process(ContainerBuilder $container)
28+
{
29+
foreach ($container->findTaggedServiceIds('cache.pool') as $id => $tags) {
30+
$pool = $container->getDefinition($id);
31+
$namespaceArgIndex = isset($tags[0]['namespace_arg_index']) ? $tags[0]['namespace_arg_index'] : -1;
32+
33+
if (!$pool instanceof DefinitionDecorator) {
34+
throw new \InvalidArgumentException(sprintf('Services tagged with "cache.pool" must have a parent service but "%s" has none.', $id));
35+
}
36+
37+
$adapter = $pool;
38+
39+
do {
40+
$adapterId = $adapter->getParent();
41+
$adapter = $container->getDefinition($adapterId);
42+
} while ($adapter instanceof DefinitionDecorator && !$adapter->getTag('cache.adapter'));
43+
44+
$tags = $adapter->getTag('cache.adapter');
45+
46+
if (!isset($tags[0]['namespace_arg_index'])) {
47+
throw new \InvalidArgumentException(sprintf('Invalid "cache.adapter" tag for service "%s": attribute "namespace_arg_index" is missing.', $adapterId));
48+
}
49+
50+
if (!$adapter->isAbstract()) {
51+
throw new \InvalidArgumentException(sprintf('Services tagged as "cache.adapter" must be abstract: "%s" is not.', $adapterId));
52+
}
53+
54+
if (0 <= $namespaceArgIndex) {
55+
$pool->replaceArgument($namespaceArgIndex, $this->getNamespace($id));
56+
}
57+
}
58+
}
59+
60+
private function getNamespace($id)
61+
{
62+
return substr(str_replace('/', '-', base64_encode(md5('symfony.'.$id, true)), 0, 10));
63+
}
64+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
+7-25Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -555,40 +555,22 @@ private function addCacheSection(ArrayNodeDefinition $rootNode)
555555
->children()
556556
->arrayNode('cache')
557557
->info('Cache configuration')
558-
->fixXmlConfig('adapter')
558+
->fixXmlConfig('pool')
559559
->children()
560-
->arrayNode('adapters')
560+
->arrayNode('pool')
561561
->useAttributeAsKey('name')
562562
->prototype('array')
563563
->beforeNormalization()
564-
->always(function ($v) {
565-
if (!isset($v['options'])) {
566-
$v['options'] = array();
567-
}
568-
569-
foreach ($v as $key => $value) {
570-
if (!in_array($key, array('type', 'name', 'options'))) {
571-
$v['options'][$key] = $value;
572-
unset($v[$key]);
573-
}
574-
}
575-
576-
return $v;
577-
})
578564
->end()
579565
->children()
580566
->enumNode('type')
581-
->info('The cache adapter type (one of "apcu", "doctrine", "filesystem")')
567+
->info('The cache pool type (one of "apcu", "doctrine", "psr6" or "filesystem")')
582568
->isRequired()
583-
->values(array('apcu', 'doctrine', 'filesystem'))
584-
->end()
585-
->arrayNode('options')
586-
->children()
587-
->integerNode('default_lifetime')->end()
588-
->scalarNode('cache_provider_service')->end()
589-
->scalarNode('directory')->end()
590-
->end()
569+
->values(array('apcu', 'doctrine', 'psr6', 'filesystem'))
591570
->end()
571+
->integerNode('default_lifetime')->default(0)->end()
572+
->scalarNode('cache_provider_service')->defaultNull()->end()
573+
->scalarNode('directory')->defaultNull()->end()
592574
->end()
593575
->end()
594576
->end()

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+13-35Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;
1313

1414
use Doctrine\Common\Annotations\Reader;
15-
use Symfony\Component\Cache\Adapter\ApcuAdapter;
16-
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
17-
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
1815
use Symfony\Component\DependencyInjection\ContainerBuilder;
1916
use Symfony\Component\DependencyInjection\ContainerInterface;
2017
use Symfony\Component\DependencyInjection\Definition;
@@ -1023,43 +1020,24 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild
10231020
}
10241021
}
10251022

1026-
private function registerCacheConfiguration(array $config, ContainerBuilder $container)
1023+
private function registerCacheConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
10271024
{
1028-
foreach ($config['adapters'] as $name => $adapter) {
1029-
$class = null;
1030-
$arguments = array();
1031-
$namespaceArgumentIndex = null;
1032-
1033-
switch ($adapter['type']) {
1034-
case 'apcu':
1035-
$class = ApcuAdapter::class;
1036-
$arguments[] = null;
1037-
$arguments[] = isset($adapter['options']['default_lifetime']) ? $adapter['options']['default_lifetime'] : 0;
1038-
$namespaceArgumentIndex = 0;
1039-
break;
1040-
case 'doctrine':
1041-
$class = DoctrineAdapter::class;
1042-
$arguments[] = isset($adapter['options']['cache_provider_service']) ? new Reference($adapter['options']['cache_provider_service']) : null;
1043-
$arguments[] = isset($adapter['options']['default_lifetime']) ? $adapter['options']['default_lifetime'] : null;
1044-
break;
1045-
case 'filesystem':
1046-
$class = FilesystemAdapter::class;
1047-
$arguments[] = isset($adapter['options']['directory']) ? $adapter['options']['directory'] : null;
1048-
$arguments[] = isset($adapter['options']['default_lifetime']) ? $adapter['options']['default_lifetime'] : null;
1049-
break;
1050-
}
1025+
if (!empty($config['pool'])) {
1026+
$loader->load('cache_adapters.xml');
1027+
}
10511028

1052-
$tagAttributes = array('id' => $name);
1029+
foreach ($config['pool'] as $name => $poolConfig) {
1030+
$poolDefinition = new DefinitionDecorator('cache.adapter.'.$poolConfig['type']);
1031+
$poolDefinition->replaceArgument(1, $poolConfig['default_lifetime']);
10531032

1054-
if (null !== $namespaceArgumentIndex) {
1055-
$tagAttributes['namespace-arg-index'] = $namespaceArgumentIndex;
1033+
if ('doctrine' === $poolConfig['type'] || 'psr6' === $poolConfig['type']) {
1034+
$poolDefinition->replaceArgument(0, new Reference($poolConfig['cache_provider_service']));
1035+
} elseif ('filesystem' === $poolConfig['type'] && isset($poolConfig['directory'][0])) {
1036+
$poolDefinition->replaceArgument(0, $poolConfig['directory']);
10561037
}
10571038

1058-
$adapterDefinition = new Definition($class);
1059-
$adapterDefinition->setArguments($arguments);
1060-
$adapterDefinition->setAbstract(true);
1061-
$adapterDefinition->addTag('cache.adapter', $tagAttributes);
1062-
$container->setDefinition('cache.adapter.'.$name, $adapterDefinition);
1039+
$poolDefinition->addTag('cache.pool');
1040+
$container->setDefinition('cache.pool.'.$name, $poolDefinition);
10631041
}
10641042
}
10651043

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
1515
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddValidatorInitializersPass;
1616
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass;
17-
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CacheAdapterPass;
17+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
1818
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ControllerArgumentValueResolverPass;
1919
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
2020
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\PropertyInfoPass;
@@ -90,7 +90,7 @@ public function build(ContainerBuilder $container)
9090
$container->addCompilerPass(new SerializerPass());
9191
$container->addCompilerPass(new PropertyInfoPass());
9292
$container->addCompilerPass(new ControllerArgumentValueResolverPass());
93-
$container->addCompilerPass(new CacheAdapterPass());
93+
$container->addCompilerPass(new CachePoolPass());
9494

9595
if ($container->getParameter('kernel.debug')) {
9696
$container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<services>
8+
9+
<service id="cache.adapter.apcu" class="Symfony\Component\Cache\Adapter\ApcuAdapter" abstract="true">
10+
<tag name="cache.adapter" namespace-arg-index="0"></tag>
11+
<argument /> <!-- namespace -->
12+
<argument /> <!-- default lifetime -->
13+
</service>
14+
15+
<service id="cache.adapter.doctrine" class="Symfony\Component\Cache\Adapter\DoctrineAdapter" abstract="true">
16+
<tag name="cache.adapter" namespace-arg-index="2"></tag>
17+
<argument /> <!-- doctrine provider service -->
18+
<argument /> <!-- default lifetime -->
19+
<argument /> <!-- namespace -->
20+
</service>
21+
22+
<service id="cache.adapter.psr6" class="Symfony\Component\Cache\Adapter\ProxyAdapter" abstract="true">
23+
<tag name="cache.adapter" namespace-arg-index="2"></tag>
24+
<argument /> <!-- PSR-6 provider service -->
25+
<argument /> <!-- default lifetime -->
26+
<argument /> <!-- namespace -->
27+
</service>
28+
29+
<service id="cache.adapter.filesystem" class="Symfony\Component\Cache\Adapter\FilesystemAdapter" abstract="true">
30+
<tag name="cache.adapter" namespace-arg-index="2"></tag>
31+
<argument>%kernel.cache_dir%</argument>
32+
<argument /> <!-- default lifetime -->
33+
<argument /> <!-- namespace -->
34+
</service>
35+
36+
</services>
37+
</container>

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@
206206

207207
<xsd:complexType name="cache">
208208
<xsd:choice minOccurs="1" maxOccurs="unbounded">
209-
<xsd:element name="adapter" type="cache_adapter" />
209+
<xsd:element name="pool" type="cache_pool" />
210210
</xsd:choice>
211211
</xsd:complexType>
212212

213-
<xsd:complexType name="cache_adapter">
213+
<xsd:complexType name="cache_pool">
214214
<xsd:attribute name="name" type="xsd:string" use="required" />
215215
<xsd:attribute name="type" type="xsd:string" use="required" />
216216
<xsd:attribute name="default-lifetime" type="xsd:integer" />
+8-8Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
1313

14-
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CacheAdapterPass;
14+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Definition;
1717
use Symfony\Component\DependencyInjection\Reference;
1818

19-
class CacheAdapterPassTest extends \PHPUnit_Framework_TestCase
19+
class CachePoolPassTest extends \PHPUnit_Framework_TestCase
2020
{
21-
private $cacheAdapterPass;
21+
private $cachePoolPass;
2222

2323
protected function setUp()
2424
{
25-
$this->cacheAdapterPass = new CacheAdapterPass();
25+
$this->cachePoolPass = new CachePoolPass();
2626
}
2727

2828
public function testAdapterIsInjectedIntoConstructorArguments()
2929
{
3030
$container = $this->initializeContainer();
31-
$this->cacheAdapterPass->process($container);
31+
$this->cachePoolPass->process($container);
3232
$adapter = $container->getDefinition('foo')->getArgument(0);
3333

3434
$this->assertInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $adapter);
@@ -40,7 +40,7 @@ public function testAdapterIsInjectedIntoConstructorArguments()
4040
public function testAdapterIsInjectedIntoMethodArguments()
4141
{
4242
$container = $this->initializeContainer();
43-
$this->cacheAdapterPass->process($container);
43+
$this->cachePoolPass->process($container);
4444
$methodCalls = $container->getDefinition('bar')->getMethodCalls();
4545
$arguments = $methodCalls[0][1];
4646
$adapter = $arguments[0];
@@ -53,7 +53,7 @@ public function testAdapterIsInjectedIntoMethodArguments()
5353
public function testAdapterIsInjectIntoProperties()
5454
{
5555
$container = $this->initializeContainer();
56-
$this->cacheAdapterPass->process($container);
56+
$this->cachePoolPass->process($container);
5757
$properties = $container->getDefinition('baz')->getProperties();
5858
$adapter = $properties['cache'];
5959

@@ -70,7 +70,7 @@ public function testThrowsExceptionWhenReferencedAdapterIsNotConfigured()
7070
{
7171
$container = new ContainerBuilder();
7272
$container->setDefinition('foo', new Definition('Foo', array(new Reference('cache.adapter.bar'))));
73-
$this->cacheAdapterPass->process($container);
73+
$this->cachePoolPass->process($container);
7474
}
7575

7676
private function initializeContainer()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.