Skip to content

[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
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function getConfigTreeBuilder()
$this->addSerializerSection($rootNode);
$this->addPropertyAccessSection($rootNode);
$this->addPropertyInfoSection($rootNode);
$this->addCacheSection($rootNode);

return $treeBuilder;
}
Expand Down Expand Up @@ -547,4 +548,33 @@ private function addPropertyInfoSection(ArrayNodeDefinition $rootNode)
->end()
;
}

private function addCacheSection(ArrayNodeDefinition $rootNode)
{
$rootNode
->children()
->arrayNode('cache')
->info('Cache configuration')
->fixXmlConfig('pool')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pools

Copy link
Member Author

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

->children()
->arrayNode('pools')
->useAttributeAsKey('name')
->prototype('array')
->children()
->enumNode('type')
->info('The cache pool type (one of "apcu", "doctrine", "psr6" or "filesystem")')
->isRequired()
->values(array('apcu', 'doctrine', 'psr6', 'filesystem'))
->end()
->integerNode('default_lifetime')->defaultValue(0)->end()
->scalarNode('cache_provider_service')->defaultNull()->end()
->scalarNode('directory')->defaultNull()->end()
->end()
->end()
->end()
->end()
->end()
->end()
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerPropertyInfoConfiguration($config['property_info'], $container, $loader);
}

if (isset($config['cache'])) {
$this->registerCacheConfiguration($config['cache'], $container, $loader);
}

$loader->load('debug_prod.xml');
$definition = $container->findDefinition('debug.debug_handlers_listener');

Expand Down Expand Up @@ -1017,6 +1021,27 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild
}
}

private function registerCacheConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
{
if (!empty($config['pools'])) {
$loader->load('cache_adapters.xml');
}

foreach ($config['pools'] as $name => $poolConfig) {
$poolDefinition = new DefinitionDecorator('cache.adapter.'.$poolConfig['type']);
$poolDefinition->replaceArgument(1, $poolConfig['default_lifetime']);

if ('doctrine' === $poolConfig['type'] || 'psr6' === $poolConfig['type']) {
$poolDefinition->replaceArgument(0, new Reference($poolConfig['cache_provider_service']));
} elseif ('filesystem' === $poolConfig['type'] && isset($poolConfig['directory'][0])) {
$poolDefinition->replaceArgument(0, $poolConfig['directory']);
}

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

/**
* Gets a hash of the kernel root directory.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddValidatorInitializersPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\PropertyInfoPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass;
Expand Down Expand Up @@ -87,6 +88,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new FragmentRendererPass(), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new SerializerPass());
$container->addCompilerPass(new PropertyInfoPass());
$container->addCompilerPass(new CachePoolPass());

if ($container->getParameter('kernel.debug')) {
$container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
Expand Down
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>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<xsd:element name="property-access" type="property_access" minOccurs="0" maxOccurs="1" />
<xsd:element name="serializer" type="serializer" minOccurs="0" maxOccurs="1" />
<xsd:element name="property-info" type="property_info" minOccurs="0" maxOccurs="1" />
<xsd:element name="cache" type="cache" minOccurs="0" maxOccurs="1" />
</xsd:all>

<xsd:attribute name="http-method-override" type="xsd:boolean" />
Expand Down Expand Up @@ -202,4 +203,18 @@
<xsd:complexType name="property_info">
<xsd:attribute name="enabled" type="xsd:boolean" />
</xsd:complexType>

<xsd:complexType name="cache">
<xsd:choice minOccurs="1" maxOccurs="unbounded">
<xsd:element name="pool" type="cache_pool" />
</xsd:choice>
</xsd:complexType>

<xsd:complexType name="cache_pool">
<xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:attribute name="type" type="xsd:string" use="required" />
<xsd:attribute name="default-lifetime" type="xsd:integer" />
<xsd:attribute name="cache-provider-service" type="xsd:string" />
<xsd:attribute name="directory" type="xsd:string" />
</xsd:complexType>
</xsd:schema>
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);
}
}
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',
),
),
),
));
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>
Loading