-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] integrate the Cache component #17868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e9f5e71
[FrameworkBundle] integrate the Cache component
xabbuh 0bfd3f5
[FrameworkBundle] Add wiring for cache-pool and cache-adapter service…
nicolas-grekas 6bf21a4
[FrameworkBundle] Add PSR-6 cache pool proxying
nicolas-grekas fba8f64
fix cache pool wiring
xabbuh aecb007
fix FrameworkExtension tests
xabbuh 5e927b4
add PSR-6 type tests
xabbuh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\DefinitionDecorator; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class CachePoolPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
foreach ($container->findTaggedServiceIds('cache.pool') as $id => $tags) { | ||
$pool = $container->getDefinition($id); | ||
|
||
if (!$pool instanceof DefinitionDecorator) { | ||
throw new \InvalidArgumentException(sprintf('Services tagged with "cache.pool" must have a parent service but "%s" has none.', $id)); | ||
} | ||
|
||
$adapter = $pool; | ||
|
||
do { | ||
$adapterId = $adapter->getParent(); | ||
$adapter = $container->getDefinition($adapterId); | ||
} while ($adapter instanceof DefinitionDecorator && !$adapter->hasTag('cache.adapter')); | ||
|
||
if (!$adapter->hasTag('cache.adapter')) { | ||
throw new \InvalidArgumentException(sprintf('Services tagged with "cache.pool" must have a parent service tagged with "cache.adapter" but "%s" has none.', $id)); | ||
} | ||
|
||
$tags = $adapter->getTag('cache.adapter'); | ||
|
||
if (!isset($tags[0]['namespace_arg_index'])) { | ||
throw new \InvalidArgumentException(sprintf('Invalid "cache.adapter" tag for service "%s": attribute "namespace_arg_index" is missing.', $adapterId)); | ||
} | ||
|
||
if (!$adapter->isAbstract()) { | ||
throw new \InvalidArgumentException(sprintf('Services tagged as "cache.adapter" must be abstract: "%s" is not.', $adapterId)); | ||
} | ||
|
||
if (0 <= $namespaceArgIndex = $tags[0]['namespace_arg_index']) { | ||
$pool->replaceArgument($namespaceArgIndex, $this->getNamespace($id)); | ||
} | ||
} | ||
} | ||
|
||
private function getNamespace($id) | ||
{ | ||
return substr(str_replace('/', '-', base64_encode(md5('symfony.'.$id, true))), 0, 10); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Symfony/Bundle/FrameworkBundle/Resources/config/cache_adapters.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
|
||
<service id="cache.adapter.apcu" class="Symfony\Component\Cache\Adapter\ApcuAdapter" abstract="true"> | ||
<tag name="cache.adapter" namespace-arg-index="0" /> | ||
<argument /> <!-- namespace --> | ||
<argument /> <!-- default lifetime --> | ||
</service> | ||
|
||
<service id="cache.adapter.doctrine" class="Symfony\Component\Cache\Adapter\DoctrineAdapter" abstract="true"> | ||
<tag name="cache.adapter" namespace-arg-index="2" /> | ||
<argument /> <!-- doctrine provider service --> | ||
<argument /> <!-- default lifetime --> | ||
<argument /> <!-- namespace --> | ||
</service> | ||
|
||
<service id="cache.adapter.psr6" class="Symfony\Component\Cache\Adapter\ProxyAdapter" abstract="true"> | ||
<tag name="cache.adapter" namespace-arg-index="2" /> | ||
<argument /> <!-- PSR-6 provider service --> | ||
<argument /> <!-- default lifetime --> | ||
<argument /> <!-- namespace --> | ||
</service> | ||
|
||
<service id="cache.adapter.filesystem" class="Symfony\Component\Cache\Adapter\FilesystemAdapter" abstract="true"> | ||
<tag name="cache.adapter" namespace-arg-index="2" /> | ||
<argument>%kernel.cache_dir%</argument> | ||
<argument /> <!-- default lifetime --> | ||
<argument /> <!-- namespace --> | ||
</service> | ||
|
||
</services> | ||
</container> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CachePoolPassTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\DefinitionDecorator; | ||
|
||
class CachePoolPassTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $cachePoolPass; | ||
|
||
protected function setUp() | ||
{ | ||
$this->cachePoolPass = new CachePoolPass(); | ||
} | ||
|
||
public function testNamespaceArgumentIsReplaced() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$adapter = new Definition(); | ||
$adapter->setAbstract(true); | ||
$adapter->addTag('cache.adapter', array('namespace_arg_index' => 0)); | ||
$container->setDefinition('app.cache_adapter', $adapter); | ||
$cachePool = new DefinitionDecorator('app.cache_adapter'); | ||
$cachePool->addArgument(null); | ||
$cachePool->addTag('cache.pool'); | ||
$container->setDefinition('app.cache_pool', $cachePool); | ||
|
||
$this->cachePoolPass->process($container); | ||
|
||
$this->assertSame('yRnzIIVLvL', $cachePool->getArgument(0)); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage Services tagged with "cache.pool" must have a parent service but "app.cache_pool" has none. | ||
*/ | ||
public function testThrowsExceptionWhenCachePoolHasNoParentDefinition() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$cachePool = new Definition(); | ||
$cachePool->addTag('cache.pool'); | ||
$container->setDefinition('app.cache_pool', $cachePool); | ||
|
||
$this->cachePoolPass->process($container); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage Services tagged with "cache.pool" must have a parent service tagged with "cache.adapter" but "app.cache_pool" has none. | ||
*/ | ||
public function testThrowsExceptionWhenCachePoolIsNotBasedOnAdapter() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->register('app.cache_adapter'); | ||
$cachePool = new DefinitionDecorator('app.cache_adapter'); | ||
$cachePool->addTag('cache.pool'); | ||
$container->setDefinition('app.cache_pool', $cachePool); | ||
|
||
$this->cachePoolPass->process($container); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage Invalid "cache.adapter" tag for service "app.cache_adapter": attribute "namespace_arg_index" is missing. | ||
*/ | ||
public function testThrowsExceptionWhenCacheAdapterDefinesNoNamespaceArgument() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$adapter = new Definition(); | ||
$adapter->setAbstract(true); | ||
$adapter->addTag('cache.adapter'); | ||
$container->setDefinition('app.cache_adapter', $adapter); | ||
$cachePool = new DefinitionDecorator('app.cache_adapter'); | ||
$cachePool->addTag('cache.pool'); | ||
$container->setDefinition('app.cache_pool', $cachePool); | ||
|
||
$this->cachePoolPass->process($container); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage Services tagged as "cache.adapter" must be abstract: "app.cache_adapter" is not. | ||
*/ | ||
public function testThrowsExceptionWhenCacheAdapterIsNotAbstract() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$adapter = new Definition(); | ||
$adapter->addTag('cache.adapter', array('namespace_arg_index' => 0)); | ||
$container->setDefinition('app.cache_adapter', $adapter); | ||
$cachePool = new DefinitionDecorator('app.cache_adapter'); | ||
$cachePool->addTag('cache.pool'); | ||
$container->setDefinition('app.cache_pool', $cachePool); | ||
|
||
$this->cachePoolPass->process($container); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/cache.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
$container->loadFromExtension('framework', array( | ||
'cache' => array( | ||
'pools' => array( | ||
'foo' => array( | ||
'type' => 'apcu', | ||
'default_lifetime' => 30, | ||
), | ||
'bar' => array( | ||
'type' => 'doctrine', | ||
'default_lifetime' => 5, | ||
'cache_provider_service' => 'app.doctrine_cache_provider', | ||
), | ||
'baz' => array( | ||
'type' => 'filesystem', | ||
'default_lifetime' => 7, | ||
'directory' => 'app/cache/psr', | ||
), | ||
'foobar' => array( | ||
'type' => 'psr6', | ||
'default_lifetime' => 10, | ||
'cache_provider_service' => 'app.cache_pool', | ||
), | ||
), | ||
), | ||
)); |
16 changes: 16 additions & 0 deletions
16
src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/cache.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:framework="http://symfony.com/schema/dic/symfony" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd | ||
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> | ||
|
||
<framework:config> | ||
<framework:cache> | ||
<framework:pool name="foo" type="apcu" default-lifetime="30" /> | ||
<framework:pool name="bar" type="doctrine" default-lifetime="5" cache-provider-service="app.doctrine_cache_provider" /> | ||
<framework:pool name="baz" type="filesystem" default-lifetime="7" directory="app/cache/psr" /> | ||
<framework:pool name="foobar" type="psr6" default-lifetime="10" cache-provider-service="app.cache_pool" /> | ||
</framework:cache> | ||
</framework:config> | ||
</container> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pools
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixXmlConfig()
takes the singular form as the first argument