8000 [FrameworkBundle] integrate the Cache component by nicolas-grekas · Pull Request #18371 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] integrate the Cache component #18371

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

Merged
merged 7 commits into from
Apr 7, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[FrameworkBundle] Integrate the Cache component
  • Loading branch information
xabbuh authored and nicolas-grekas committed Apr 4, 2016
commit 281eafa5cbf6db25d2f48387f1a05530ae192fd7
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?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;
use Symfony\Component\DependencyInjection\Reference;

/**
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
class CacheAdapterPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$adapters = array();

foreach ($container->findTaggedServiceIds('cache.adapter') as $id => $tags) {
foreach ($tags as $attributes) {
$adapters[$attributes['id']] = array(
'definition_id' => $id,
'namespace_argument_index' => isset($attributes['namespace-arg-index']) ? $attributes['namespace-arg-index'] : null,
);
}
}

foreach ($container->getDefinitions() as $id => $definition) {
$definition->setArguments($this->resolveArguments($adapters, $id, $definition->getArguments()));

$calls = $definition->getMethodCalls();

foreach ($calls as $index => $call) {
$calls[$index] = array($call[0], $this->resolveArguments($adapters, $id, $call[1]));
}

$definition->setMethodCalls($calls);

$definition->setProperties($this->resolveArguments($adapters, $id, $definition->getProperties()));
}
}

private function resolveArguments(array $adapters, $id, array $arguments)
{
foreach ($arguments as $index => $argument) {
if ($argument instanceof Reference) {
$arguments[$index] = $this->createCacheAdapter($adapters, $id, $argument);
}
}

return $arguments;
}

private function createCacheAdapter(array $adapters, $serviceId, Reference $argument)
{
$adapterId = (string) $argument;

if (0 !== strpos($adapterId, 'cache.adapter.')) {
return $argument;
}

$name = substr($adapterId, 14);

if (!isset($adapters[$name])) {
throw new \InvalidArgumentException(sprintf('The cache adapter "%s" is not configured.', $name));
}

$adapter = new DefinitionDecorator($adapters[$name]['definition_id']);

if (null !== $adapters[$name]['namespace_argument_index']) {
$adapter->replaceArgument($adapters[$name]['namespace_argument_index'], sha1($serviceId));
}

return $adapter;
}
}
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,53 @@ private function addPropertyInfoSection(ArrayNodeDefinition $rootNode)
->end()
;
}

private function addCacheSection(ArrayNodeDefinition $rootNode)
{
$rootNode
->children()
->arrayNode('cache')
->info('Cache configuration')
->fixXmlConfig('adapter')
->children()
->arrayNode('adapters')
->useAttributeAsKey('name')
->prototype('array')
->beforeNormalization()
->always(function ($v) {
if (!isset($v['options'])) {
$v['options'] = array();
}

foreach ($v as $key => $value) {
if (!in_array($key, array('type', 'name', 'options'))) {
$v['options'][$key] = $value;
unset($v[$key]);
}
}

return $v;
})
->end()
->children()
->enumNode('type')
->info('The cache adapter type (one of "apcu", "doctrine", "filesystem")')
->isRequired()
->values(array('apcu', 'doctrine', 'filesystem'))
->end()
->arrayNode('options')
->children()
->integerNode('default_lifetime')->end()
->scalarNode('cache_provider_service')->end()
->scalarNode('directory')->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;

use Doctrine\Common\Annotations\Reader;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -138,6 +141,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->load('debug_prod.xml');
$definition = $container->findDefinition('debug.debug_handlers_listener');

Expand Down Expand Up @@ -1016,6 +1023,46 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild
}
}

private function registerCacheConfiguration(array $config, ContainerBuilder $container)
{
foreach ($config['adapters'] as $name => $adapter) {
$class = null;
$arguments = array();
$namespaceArgumentIndex = null;

switch ($adapter['type']) {
case 'apcu':
$class = ApcuAdapter::class;
$arguments[] = null;
$arguments[] = isset($adapter['options']['default_lifetime']) ? $adapter['options']['default_lifetime'] : 0;
$namespaceArgumentIndex = 0;
break;
case 'doctrine':
$class = DoctrineAdapter::class;
$arguments[] = isset($adapter['options']['cache_provider_service']) ? new Reference($adapter['options']['cache_provider_service']) : null;
$arguments[] = isset($adapter['options']['default_lifetime']) ? $adapter['options']['default_lifetime'] : null;
break;
case 'filesystem':
$class = FilesystemAdapter::class;
$arguments[] = isset($adapter['options']['directory']) ? $adapter['options']['directory'] : null;
$arguments[] = isset($adapter['options']['default_lifetime']) ? $adapter['options']['default_lifetime'] : null;
break;
}

$tagAttributes = array('id' => $name);

if (null !== $namespaceArgumentIndex) {
$tagAttributes['namespace-arg-index'] = $namespaceArgumentIndex;
}

$adapterDefinition = new Definition($class);
$adapterDefinition->setArguments($arguments);
$adapterDefinition->setAbstract(true);
$adapterDefinition->addTag('cache.adapter', $tagAttributes);
$container->setDefinition('cache.adapter.'.$name, $adapterDefinition);
}
}

/**
* 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\CacheAdapterPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ControllerArgumentValueResolverPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\PropertyInfoPass;
Expand Down Expand Up @@ -89,6 +90,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new SerializerPass());
$container->addCompilerPass(new PropertyInfoPass());
$container->addCompilerPass(new ControllerArgumentValueResolverPass());
$container->addCompilerPass(new CacheAdapterPass());

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
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="adapter" type="cache_adapter" />
</xsd:choice>
</xsd:complexType>

<xsd:complexType name="cache_adapter">
<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,109 @@
<?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\CacheAdapterPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class CacheAdapterPassTest extends \PHPUnit_Framework_TestCase
{
private $cacheAdapterPass;

protected function setUp()
{
$this->cacheAdapterPass = new CacheAdapterPass();
}

public function testAdapterIsInjectedIntoConstructorArguments()
{
$container = $this->initializeContainer();
$this->cacheAdapterPass->process($container);
$adapter = $container->getDefinition('foo')->getArgument(0);

$this->assertInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $adapter);
$this->assertFalse($adapter->isAbstract());
$this->assertSame('cache.adapter.apcu_adapter', $adapter->getParent());
$this->assertSame('0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', $adapter->getArgument(0));
}

public function testAdapterIsInjectedIntoMethodArguments()
{
$container = $this->initializeContainer();
$this->cacheAdapterPass->process($container);
$methodCalls = $container->getDefinition('bar')->getMethodCalls();
$arguments = $methodCalls[0][1];
$adapter = $arguments[0];

$this->assertInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $adapter);
$this->assertFalse($adapter->isAbstract());
$this->assertSame('cache.adapter.doctrine_adapter', $adapter->getParent());
}

public function testAdapterIsInjectIntoProperties()
{
$container = $this->initializeContainer();
$this->cacheAdapterPass->process($container);
$properties = $container->getDefinition('baz')->getProperties();
$adapter = $properties['cache'];

$this->assertInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $adapter);
$this->assertFalse($adapter->isAbstract());
$this->assertSame('cache.adapter.fs_adapter', $adapter->getParent());
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The cache adapter "bar" is not configured
*/
public function testThrowsExceptionWhenReferencedAdapterIsNotConfigured()
{
$container = new ContainerBuilder();
$container->setDefinition('foo', new Definition('Foo', array(new Reference('cache.adapter.bar'))));
$this->cacheAdapterPass->process($container);
}

private function initializeContainer()
{
$container = new ContainerBuilder();

$apcuAdapter = new Definition('Symfony\Component\Cache\Adapter\ApcuAdapter');
$apcuAdapter->setAbstract(true);
$apcuAdapter->addTag('cache.adapter', array('id' => 'adapter1', 'namespace-arg-index' => 0));
$container->setDefinition('cache.adapter.apcu_adapter', $apcuAdapter);

$doctrineAdapter = new Definition('Symfony\Component\Cache\Adapter\DoctrineAdapter');
$doctrineAdapter->setAbstract(true);
$doctrineAdapter->addTag('cache.adapter', array('id' => 'adapter2'));
$container->setDefinition('cache.adapter.doctrine_adapter', $doctrineAdapter);

$filesystemAdapter = new Definition('Symfony\Component\Cache\Adapter\FilesystemAdapter');
$filesystemAdapter->setAbstract(true);
$filesystemAdapter->addTag('cache.adapter', array('id' => 'adapter3'));
$container->setDefinition('cache.adapter.fs_adapter', $filesystemAdapter);

$foo = new Definition();
$foo->setArguments(array(new Reference('cache.adapter.adapter1')));
$container->setDefinition('foo', $foo);

$bar = new Definition();
$bar->addMethodCall('setCache', array(new Reference('cache.adapter.adapter2')));
$container->setDefinition('bar', $bar);

$baz = new Definition();
$baz->setProperty('cache', new Reference('cache.adapter.adapter3'));
$container->setDefinition('baz', $baz);

return $container;
}
}
Loading
0