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 714b916

Browse filesBrowse files
[FrameworkBundle] Add & use Psr6CacheClearer
1 parent 4740c5c commit 714b916
Copy full SHA for 714b916

File tree

20 files changed

+228
-52
lines changed
Filter options

20 files changed

+228
-52
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php
+21-10Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,47 @@ class CachePoolPass implements CompilerPassInterface
2727
public function process(ContainerBuilder $container)
2828
{
2929
$attributes = array(
30-
'provider_service',
30+
'provider',
3131
'namespace',
3232
'default_lifetime',
33-
'directory',
3433
);
3534
foreach ($container->findTaggedServiceIds('cache.pool') as $id => $tags) {
3635
$adapter = $pool = $container->getDefinition($id);
37-
$tags[0] += array('namespace' => $this->getNamespace($id));
38-
36+
if ($pool->isAbstract()) {
37+
continue;
38+
}
39+
if (!isset($tags[0]['namespace'])) {
40+
$tags[0]['namespace'] = $this->getNamespace($id);
41+
}
3942
while ($adapter instanceof DefinitionDecorator) {
4043
$adapter = $container->findDefinition($adapter->getParent());
4144
if ($t = $adapter->getTag('cache.pool')) {
4245
$tags[0] += $t[0];
4346
}
4447
}
45-
if ($pool->isAbstract()) {
46-
continue;
48+
if (isset($tags[0]['clearer'])) {
49+
$clearer = $container->getDefinition($tags[0]['clearer']);
50+
} else {
51+
$clearer = null;
4752
}
48-
if (isset($tags[0]['provider_service']) && is_string($tags[0]['provider_service'])) {
49-
$tags[0]['provider_service'] = new Reference($tags[0]['provider_service']);
53+
unset($tags[0]['clearer']);
54+
55+
if (isset($tags[0]['provider']) && is_string($tags[0]['provider'])) {
56+
$tags[0]['provider'] = new Reference($tags[0]['provider']);
5057
}
5158
$i = 0;
5259
foreach ($attributes as $attr) {
5360
if (isset($tags[0][$attr])) {
5461
$pool->replaceArgument($i++, $tags[0][$attr]);
55-
unset($tags[0][$attr]);
5662
}
63+
unset($tags[0][$attr]);
5764
}
5865
if (!empty($tags[0])) {
59-
throw new \InvalidArgumentException(sprintf('Invalid "cache.pool" tag for service "%s": accepted attributes are "provider_service", "namespace", "default_lifetime" and "directory", found "%s".', $id, implode('", "', array_keys($tags[0]))));
66+
throw new \InvalidArgumentException(sprintf('Invalid "cache.pool" tag for service "%s": accepted attributes are "provider", "namespace" and "default_lifetime", found "%s".', $id, implode('", "', array_keys($tags[0]))));
67+
}
68+
69+
if (null !== $clearer) {
70+
$clearer->addMethodCall('addPool', array(new Reference($id)));
6071
}
6172
}
6273
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
+10-5Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,14 +561,19 @@ private function addCacheSection(ArrayNodeDefinition $rootNode)
561561
->useAttributeAsKey('name')
562562
->prototype('array')
563563
->children()
564-
->scalarNode('adapter_service')
565-
->info('The cache pool service to use as template definition.')
564+
->scalarNode('adapter')
565+
->info('The cache pool adapter service to use as template definition.')
566566
->defaultValue('cache.adapter.default')
567567
->end()
568568
->booleanNode('public')->defaultFalse()->end()
569-
->integerNode('default_lifetime')->defaultNull()->end()
570-
->scalarNode('provider_service')->defaultNull()->end()
571-
->scalarNode('directory')->defaultNull()->end()
569+
->integerNode('default_lifetime')->end()
570+
->scalarNode('provider')
571+
->info('The service name to use as provider when the specified adapter needs one.')
572+
->end()
573+
->scalarNode('namespace')
574+
->info('The namespace where cached items are stored. Auto-generated by default. Set to false to disable namespacing.')
575+
->end()
576+
->scalarNode('clearer')->defaultValue('cache.default_pools_clearer')->end()
572577
->end()
573578
->end()
574579
->end()

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,9 +1027,9 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
10271027
}
10281028

10291029
foreach ($config['pools'] as $name => $poolConfig) {
1030-
$poolDefinition = new DefinitionDecorator($poolConfig['adapter_service']);
1030+
$poolDefinition = new DefinitionDecorator($poolConfig['adapter']);
10311031
$poolDefinition->setPublic($poolConfig['public']);
1032-
unset($poolConfig['adapter_service'], $poolConfig['public']);
1032+
unset($poolConfig['adapter'], $poolConfig['public']);
10331033

10341034
$poolDefinition->addTag('cache.pool', $poolConfig);
10351035
$container->setDefinition('cache.pool.'.$name, $poolDefinition);

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/cache_pools.xml
+12-6Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,39 @@
66

77
<services>
88

9+
<service id="cache.default_pools_clearer" class="Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer" public="false">
10+
<tag name="kernel.cache_clearer" />
11+
</service>
12+
913
<service id="cache.adapter.default" alias="cache.adapter.filesystem" />
1014

1115
<service id="cache.adapter.apcu" class="Symfony\Component\Cache\Adapter\ApcuAdapter" abstract="true">
12-
<tag name="cache.pool" />
1316
<argument /> <!-- namespace -->
1417
<argument /> <!-- default lifetime -->
1518
</service>
1619

1720
<service id="cache.adapter.doctrine" class="Symfony\Component\Cache\Adapter\DoctrineAdapter" abstract="true">
18-
<tag name="cache.pool" />
1921
<argument /> <!-- Doctrine provider service -->
2022
<argument /> <!-- namespace -->
2123
<argument /> <!-- default lifetime -->
2224
</service>
2325

26+
<service id="cache.adapter.filesystem" class="Symfony\Component\Cache\Adapter\FilesystemAdapter" abstract="true">
27+
<argument /> <!-- namespace -->
28+
<argument /> <!-- default lifetime -->
29+
<argument>%kernel.cache_dir%/pools</argument>
30+
</service>
31+
2432
<service id="cache.adapter.psr6" class="Symfony\Component\Cache\Adapter\ProxyAdapter" abstract="true">
25-
<tag name="cache.pool" />
2633
<argument /> <!-- PSR-6 provider service -->
2734
<argument /> <!-- namespace -->
2835
<argument /> <!-- default lifetime -->
2936
</service>
3037

31-
<service id="cache.adapter.filesystem" class="Symfony\Component\Cache\Adapter\FilesystemAdapter" abstract="true">
32-
<tag name="cache.pool" />
38+
<service id="cache.adapter.redis" class="Symfony\Component\Cache\Adapter\RedisAdapter" abstract="true">
39+
<argument /> <!-- Redis connection object -->
3340
<argument /> <!-- namespace -->
3441
<argument /> <!-- default lifetime -->
35-
<argument>%kernel.cache_dir%</argument>
3642
</service>
3743

3844
</services>

‎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
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,11 @@
212212

213213
<xsd:complexType name="cache_pool">
214214
<xsd:attribute name="name" type="xsd:string" use="required" />
215-
<xsd:attribute name="adapter-service" type="xsd:string" />
215+
<xsd:attribute name="adapter" type="xsd:string" />
216216
<xsd:attribute name="public" type="xsd:boolean" />
217217
<xsd:attribute name="default-lifetime" type="xsd:integer" />
218-
<xsd:attribute name="provider-service" type="xsd:string" />
218+
<xsd:attribute name="provider" type="xsd:string" />
219219
<xsd:attribute name="directory" type="xsd:string" />
220+
<xsd:attribute name="clearer" type="xsd:string" />
220221
</xsd:complexType>
221222
</xsd:schema>

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CachePoolPassTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function testArgsAreReplaced()
4949
$container = new ContainerBuilder();
5050
$cachePool = new Definition();
5151
$cachePool->addTag('cache.pool', array(
52-
'provider_service' => 'foobar',
52+
'provider' => 'foobar',
5353
'default_lifetime' => 3,
5454
));
5555
$cachePool->addArgument(null);

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/cache.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/cache.php
+6-7Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@
44
'cache' => array(
55
'pools' => array(
66
'foo' => array(
7-
'adapter_service' => 'cache.adapter.apcu',
7+
'adapter' => 'cache.adapter.apcu',
88
'default_lifetime' => 30,
99
),
1010
'bar' => array(
11-
'adapter_service' => 'cache.adapter.doctrine',
11+
'adapter' => 'cache.adapter.doctrine',
1212
'default_lifetime' => 5,
13-
'provider_service' => 'app.doctrine_cache_provider',
13+
'provider' => 'app.doctrine_cache_provider',
1414
),
1515
'baz' => array(
16-
'adapter_service' => 'cache.adapter.filesystem',
16+
'adapter' => 'cache.adapter.filesystem',
1717
'default_lifetime' => 7,
18-
'directory' => 'app/cache/psr',
1918
),
2019
'foobar' => array(
21-
'adapter_service' => 'cache.adapter.psr6',
20+
'adapter' => 'cache.adapter.psr6',
2221
'default_lifetime' => 10,
23-
'provider_service' => 'app.cache_pool',
22+
'provider' => 'app.cache_pool',
2423
),
2524
'def' => array(
2625
'default_lifetime' => 11,

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/cache.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/cache.xml
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
<framework:config>
99
<framework:cache>
10-
<framework:pool name="foo" adapter-service="cache.adapter.apcu" default-lifetime="30" />
11-
<framework:pool name="bar" adapter-service="cache.adapter.doctrine" default-lifetime="5" provider-service="app.doctrine_cache_provider" />
12-
<framework:pool name="baz" adapter-service="cache.adapter.filesystem" default-lifetime="7" directory="app/cache/psr" />
13-
<framework:pool name="foobar" adapter-service="cache.adapter.psr6" default-lifetime="10" provider-service="app.cache_pool" />
10+
<framework:pool name="foo" adapter="cache.adapter.apcu" default-lifetime="30" />
11+
<framework:pool name="bar" adapter="cache.adapter.doctrine" default-lifetime="5" provider="app.doctrine_cache_provider" />
12+
<framework:pool name="baz" adapter="cache.adapter.filesystem" default-lifetime="7" />
13+
<framework:pool name="foobar" adapter="cache.adapter.psr6" default-lifetime="10" provider="app.cache_pool" />
1414
<framework:pool name="def" default-lifetime="11" />
1515
</framework:cache>
1616
</framework:config>

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/cache.yml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/cache.yml
+6-7Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@ framework:
22
cache:
33
pools:
44
foo:
5-
adapter_service: cache.adapter.apcu
5+
adapter: cache.adapter.apcu
66
default_lifetime: 30
77
bar:
8-
adapter_service: cache.adapter.doctrine
8+
adapter: cache.adapter.doctrine
99
default_lifetime: 5
10-
provider_service: app.doctrine_cache_provider
10+
provider: app.doctrine_cache_provider
1111
baz:
12-
adapter_service: cache.adapter.filesystem
12+
adapter: cache.adapter.filesystem
1313
default_lifetime: 7
14-
directory: app/cache/psr
1514
foobar:
16-
adapter_service: cache.adapter.psr6
15+
adapter: cache.adapter.psr6
1716
default_lifetime: 10
18-
provider_service: app.cache_pool
17+
provider: app.cache_pool
1918
def:
2019
default_lifetime: 11

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,6 @@ private function assertCachePoolServiceDefinitionIsCreated(ContainerBuilder $con
683683
break;
684684
}
685685

686-
$this->assertTrue($adapterDefinition->hasTag('cache.pool'), sprintf('Service definition "%s" is tagged with the "cache.pool" tag.', $adapterId));
687686
$this->assertTrue($adapterDefinition->isAbstract(), sprintf('Service definition "%s" is abstract.', $adapterId));
688687
}
689688
}
+66Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\Tests\Functional;
13+
14+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
15+
use Symfony\Component\Cache\Adapter\RedisAdapter;
16+
17+
class CachePoolsTest extends WebTestCase
18+
{
19+
public function testCachePools()
20+
{
21+
$this->doTestCachePools(array(), FilesystemAdapter::class);
22+
}
23+
24+
/**
25+
* @requires extension redis
26+
*/
27+
public function testRedisCachePools()
28+
{
29+
try {
30+
$this->doTestCachePools(array('root_config' => 'redis_config.yml', 'environment' => 'redis_cache'), RedisAdapter::class);
31+
} catch (\PHPUnit_Framework_Error_Warning $e) {
32+
if (0 !== strpos($e->getMessage(), 'unable to connect to 127.0.0.1')) {
33+
throw $e;
34+
}
35+
$this->markTestSkipped($e->getMessage());
36+
}
37+
}
38+
39+
public function doTestCachePools($options, $adapterClass)
40+
{
41+
static::bootKernel($options);
42+
$container = static::$kernel->getContainer();
43+
44+
$pool = $container->get('cache.pool.test');
45+
$this->assertInstanceOf($adapterClass, $pool);
46+
47+
$key = 'foobar';
48+
$pool->deleteItem($key);
49+
$item = $pool->getItem($key);
50+
$this->assertFalse($item->isHit());
51+
52+
$item->set('baz');
53+
$pool->save($item);
54+
$item = $pool->getItem($key);
55+
$this->assertTrue($item->isHit());
56+
57+
$container->get('cache_clearer')->clear($container->getParameter('kernel.cache_dir'));
58+
$item = $pool->getItem($key);
59+
$this->assertFalse($item->isHit());
60+
}
61+
62+
protected static function createKernel(array $options = array())
63+
{
64+
return parent::createKernel(array('test_case' => 'CachePools') + $options);
65+
}
66+
}
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
13+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
14+
15+
return array(
16+
new FrameworkBundle(),
17+
new TestBundle(),
18+
);
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
4+
framework:
5+
cache:
6+
pools:
7+
test:
8+
public: true
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
4+
services:
5+
cache.adapter.redis.connection:
6+
public: false
7+
class: Redis
8+
calls:
9+
- [connect, [127.0.0.1]]
10+
11+
cache.adapter.default:
12+
abstract: true
13+
parent: cache.adapter.redis
14+
tags:
15+
- name: cache.pool
16+
provider: cache.adapter.redis.connection
17+
18+
framework:
19+
cache:
20+
pools:
21+
test:
22+
public: true

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/composer.json
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": ">=5.5.9",
2020
"symfony/asset": "~2.8|~3.0",
2121
"symfony/class-loader": "~2.8|~3.0",
22-
"symfony/dependency-injection": "~2.8|~3.0",
22+
"symfony/dependency-injection": "~3.1",
2323
"symfony/config": "~2.8|~3.0",
2424
"symfony/event-dispatcher": "~2.8|~3.0",
2525
"symfony/http-foundation": "~3.1",
@@ -38,6 +38,7 @@
3838
},
3939
"require-dev": {
4040
"symfony/browser-kit": "~2.8|~3.0",
41+
"symfony/cache": "~3.1",
4142
"symfony/console": "~2.8|~3.0",
4243
"symfony/css-selector": "~2.8|~3.0",
4344
"symfony/dom-crawler": "~2.8|~3.0",

‎src/Symfony/Component/Cache/Adapter/FilesystemAdapter.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Adapter/FilesystemAdapter.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public function __construct($namespace = '', $defaultLifetime = 0, $directory =
2828
$directory = sys_get_temp_dir().'/symfony-cache';
2929
}
3030
if (isset($namespace[0])) {
31+
if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) {
32+
throw new InvalidArgumentException(sprintf('FilesystemAdapter namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0]));
33+
}
3134
$directory .= '/'.$namespace;
3235
}
3336
if (!file_exists($dir = $directory.'/.')) {

‎src/Symfony/Component/Cache/Tests/Adapter/FilesystemTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Tests/Adapter/FilesystemTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ public function createCachePool()
2525
$this->skippedTests['testDeferredSaveWithoutCommit'] = 'Fails on HHVM';
2626
}
2727

28-
return new FilesystemAdapter(sys_get_temp_dir().DIRECTORY_SEPARATOR.'sf-cache');
28+
return new FilesystemAdapter('sf-cache');
2929
}
3030
}

0 commit comments

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