[FrameworkBundle] Add cache adapters in semantic configuration

This commit is contained in:
Titouan Galopin 2016-04-23 20:46:00 +02:00 committed by Nicolas Grekas
parent 8166094dc2
commit 80a55086ea
10 changed files with 124 additions and 31 deletions

View File

@ -44,13 +44,15 @@ class CachePoolPass implements CompilerPassInterface
if ($pool->isAbstract()) {
continue;
}
$tags[0]['namespace'] = $this->getNamespace($namespaceSuffix, isset($tags[0]['namespace']) ? $tags[0]['namespace'] : $id);
while ($adapter instanceof DefinitionDecorator) {
$adapter = $container->findDefinition($adapter->getParent());
if ($t = $adapter->getTag('cache.pool')) {
$tags[0] += $t[0];
}
}
if (!isset($tags[0]['namespace'])) {
$tags[0]['namespace'] = $this->getNamespace($namespaceSuffix, $id);
}
if (isset($tags[0]['clearer'])) {
$clearer = $container->getDefinition($tags[0]['clearer']);
} else {

View File

@ -557,24 +557,34 @@ class Configuration implements ConfigurationInterface
->info('Cache configuration')
->addDefaultsIfNotSet()
->fixXmlConfig('pool')
->fixXmlConfig('adapter')
->children()
->arrayNode('adapters')
->useAttributeAsKey('name')
->prototype('array')
->children()
->scalarNode('parent')
->isRequired()
->info('The parent cache adapter service.')
->end()
->integerNode('default_lifetime')->end()
->scalarNode('provider')
->info('The service name to use as provider when the specified parent adapter needs one.')
->end()
->scalarNode('clearer')->defaultValue('cache.default_pools_clearer')->end()
->end()
->end()
->end()
->arrayNode('pools')
->useAttributeAsKey('name')
->prototype('array')
->children()
->scalarNode('adapter')
->info('The cache pool adapter service to use as template definition.')
->info('The cache adapter service to use as template definition.')
->defaultValue('cache.adapter.shared')
->end()
->booleanNode('public')->defaultFalse()->end()
->booleanNode('public')->defaultTrue()->end()
->integerNode('default_lifetime')->end()
->scalarNode('provider')
->info('The service name to use as provider when the specified adapter needs one.')
->end()
->scalarNode('namespace')
->info('The namespace where cached items are stored. Auto-generated by default. Set to false to disable namespacing.')
->end()
->scalarNode('clearer')->defaultValue('cache.default_pools_clearer')->end()
->end()
->end()
->end()

View File

@ -1037,6 +1037,15 @@ class FrameworkExtension extends Extension
private function registerCacheConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
{
foreach ($config['adapters'] as $name => $adapterConfig) {
$adapterDefinition = new DefinitionDecorator($adapterConfig['parent']);
$adapterDefinition->setAbstract(true);
unset($adapterConfig['parent']);
$adapterDefinition->addTag('cache.pool', $adapterConfig);
$container->setDefinition('cache.adapter.'.$name, $adapterDefinition);
}
foreach ($config['pools'] as $name => $poolConfig) {
$poolDefinition = new DefinitionDecorator($poolConfig['adapter']);
$poolDefinition->setPublic($poolConfig['public']);

View File

@ -10,26 +10,23 @@
<tag name="kernel.cache_clearer" />
</service>
<service id="cache.adapter.shared" alias="cache.adapter.filesystem" />
<service id="cache.adapter.local" alias="cache.adapter.filesystem" />
<service id="cache.pool.shared" parent="cache.adapter.shared">
<tag name="cache.pool" clearer="cache.default_pools_clearer" />
</service>
<service id="cache.pool.local" parent="cache.adapter.local">
<tag name="cache.pool" clearer="cache.default_pools_clearer" />
<service id="cache.pool.app" parent="cache.adapter.shared">
<tag name="cache.pool" />
</service>
<service id="cache.pool.validator" parent="cache.adapter.local" public="false">
<tag name="cache.pool" clearer="cache.default_pools_clearer" />
<tag name="cache.pool" />
</service>
<service id="cache.pool.serializer" parent="cache.adapter.local" public="false">
<tag name="cache.pool" clearer="cache.default_pools_clearer" />
<tag name="cache.pool" />
</service>
<service id="cache.adapter.local" alias="cache.adapter.filesystem" />
<service id="cache.adapter.shared" alias="cache.adapter.filesystem" />
<service id="cache.adapter.apcu" class="Symfony\Component\Cache\Adapter\ApcuAdapter" abstract="true">
<tag name="cache.pool" clearer="cache.default_pools_clearer" />
<tag name="monolog.logger" channel="cache" />
<argument /> <!-- namespace -->
<argument /> <!-- default lifetime -->
@ -39,6 +36,7 @@
</service>
<service id="cache.adapter.doctrine" class="Symfony\Component\Cache\Adapter\DoctrineAdapter" abstract="true">
<tag name="cache.pool" clearer="cache.default_pools_clearer" />
<tag name="monolog.logger" channel="cache" />
<argument /> <!-- Doctrine provider service -->
<argument /> <!-- namespace -->
@ -49,6 +47,7 @@
</service>
<service id="cache.adapter.filesystem" class="Symfony\Component\Cache\Adapter\FilesystemAdapter" abstract="true">
<tag name="cache.pool" clearer="cache.default_pools_clearer" />
<tag name="monolog.logger" channel="cache" />
<argument /> <!-- namespace -->
<argument /> <!-- default lifetime -->
@ -59,12 +58,14 @@
</service>
<service id="cache.adapter.psr6" class="Symfony\Component\Cache\Adapter\ProxyAdapter" abstract="true">
<tag name="cache.pool" clearer="cache.default_pools_clearer" />
<argument /> <!-- PSR-6 provider service -->
<argument /> <!-- namespace -->
<argument /> <!-- default lifetime -->
</service>
<service id="cache.adapter.redis" class="Symfony\Component\Cache\Adapter\RedisAdapter" abstract="true">
<tag name="cache.pool" clearer="cache.default_pools_clearer" />
<tag name="monolog.logger" channel="cache" />
<argument /> <!-- Redis connection object -->
<argument /> <!-- namespace -->

View File

@ -206,16 +206,23 @@
<xsd:complexType name="cache">
<xsd:choice minOccurs="1" maxOccurs="unbounded">
<xsd:element name="adapter" type="cache_adapter" />
<xsd:element name="pool" type="cache_pool" />
</xsd:choice>
</xsd:complexType>
<xsd:complexType name="cache_adapter">
<xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:attribute name="parent" type="xsd:string" />
<xsd:attribute name="default-lifetime" type="xsd:integer" />
<xsd:attribute name="provider" type="xsd:string" />
<xsd:attribute name="clearer" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="cache_pool">
<xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:attribute name="adapter" type="xsd:string" />
<xsd:attribute name="public" type="xsd:boolean" />
<xsd:attribute name="default-lifetime" type="xsd:integer" />
<xsd:attribute name="provider" type="xsd:string" />
<xsd:attribute name="clearer" type="xsd:string" />
</xsd:complexType>
</xsd:schema>

View File

@ -267,6 +267,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
'packages' => array(),
),
'cache' => array(
'adapters' => array(),
'pools' => array(),
),
);

View File

@ -2,6 +2,17 @@
$container->loadFromExtension('framework', array(
'cache' => array(
'adapters' => array(
'foo' => array(
'parent' => 'cache.adapter.filesystem',
'default_lifetime' => 30,
),
'app_redis' => array(
'parent' => 'cache.adapter.redis',
'provider' => 'app.redis_connection',
'default_lifetime' => 30,
),
),
'pools' => array(
'foo' => array(
'adapter' => 'cache.adapter.apcu',
@ -10,7 +21,6 @@ $container->loadFromExtension('framework', array(
'bar' => array(
'adapter' => 'cache.adapter.doctrine',
'default_lifetime' => 5,
'provider' => 'app.doctrine_cache_provider',
),
'baz' => array(
'adapter' => 'cache.adapter.filesystem',
@ -19,7 +29,6 @@ $container->loadFromExtension('framework', array(
'foobar' => array(
'adapter' => 'cache.adapter.psr6',
'default_lifetime' => 10,
'provider' => 'app.cache_pool',
),
'def' => array(
'default_lifetime' => 11,

View File

@ -7,10 +7,12 @@
<framework:config>
<framework:cache>
<framework:adapter name="foo" parent="cache.adapter.filesystem" default-lifetime="30" />
<framework:adapter name="app_redis" parent="cache.adapter.redis" provider="app.redis_connection" default-lifetime="30" />
<framework:pool name="foo" adapter="cache.adapter.apcu" default-lifetime="30" />
<framework:pool name="bar" adapter="cache.adapter.doctrine" default-lifetime="5" provider="app.doctrine_cache_provider" />
<framework:pool name="bar" adapter="cache.adapter.doctrine" default-lifetime="5" />
<framework:pool name="baz" adapter="cache.adapter.filesystem" default-lifetime="7" />
<framework:pool name="foobar" adapter="cache.adapter.psr6" default-lifetime="10" provider="app.cache_pool" />
<framework:pool name="foobar" adapter="cache.adapter.psr6" default-lifetime="10" />
<framework:pool name="def" default-lifetime="11" />
</framework:cache>
</framework:config>

View File

@ -1,5 +1,13 @@
framework:
cache:
adapters:
foo:
parent: cache.adapter.filesystem
default_lifetime: 30
app_redis:
parent: cache.adapter.redis
provider: app.redis_connection
default_lifetime: 30
pools:
foo:
adapter: cache.adapter.apcu
@ -7,13 +15,11 @@ framework:
bar:
adapter: cache.adapter.doctrine
default_lifetime: 5
provider: app.doctrine_cache_provider
baz:
adapter: cache.adapter.filesystem
default_lifetime: 7
foobar:
adapter: cache.adapter.psr6
default_lifetime: 10
provider: app.cache_pool
def:
default_lifetime: 11

View File

@ -615,6 +615,14 @@ abstract class FrameworkExtensionTest extends TestCase
$this->assertTrue($container->has('property_info'));
}
public function testCacheAdapterServices()
{
$container = $this->createContainerFromFile('cache');
$this->assertCacheAdaptersServiceDefinitionIsCreated($container, 'foo', 'cache.adapter.foo', null, 30);
$this->assertCacheAdaptersServiceDefinitionIsCreated($container, 'app_redis', 'cache.adapter.redis', 'app.redis_connection', 30);
}
public function testCachePoolServices()
{
$container = $this->createContainerFromFile('cache');
@ -697,6 +705,46 @@ abstract class FrameworkExtensionTest extends TestCase
}
}
private function assertCacheAdaptersServiceDefinitionIsCreated(ContainerBuilder $container, $name, $parent, $provider, $defaultLifetime)
{
$id = 'cache.adapter.'.$name;
$this->assertTrue($container->has($id), sprintf('Service definition "%s" for cache adapter "%s" is registered', $id, $name));
$adapterDefinition = $container->getDefinition($id);
$this->assertTrue($adapterDefinition->hasTag('cache.pool'), sprintf('Service definition "%s" is tagged with the "cache.pool" tag.', $id));
$this->assertTrue($adapterDefinition->isAbstract(), sprintf('Service definition "%s" is abstract.', $id));
$tag = $adapterDefinition->getTag('cache.pool');
$this->assertTrue(isset($tag[0]['default_lifetime']), 'The default lifetime is stored as an attribute of the "cache.pool" tag.');
$this->assertSame($defaultLifetime, $tag[0]['default_lifetime'], 'The default lifetime is stored as an attribute of the "cache.pool" tag.');
if ($provider) {
$this->assertTrue(isset($tag[0]['provider']), 'The provider is stored as an attribute of the "cache.pool" tag.');
$this->assertSame($provider, $tag[0]['provider'], 'The provider is stored as an attribute of the "cache.pool" tag.');
} else {
$this->assertFalse(isset($tag[0]['provider']), 'No provider is stored as an attribute of the "cache.pool" tag.');
}
$this->assertInstanceOf(DefinitionDecorator::class, $adapterDefinition, sprintf('Cache adapter "%s" is based on a parent.', $name));
$adapterId = $adapterDefinition->getParent();
$adapterDefinition = $container->findDefinition($adapterId);
switch ($parent) {
case 'cache.adapter.apcu':
$this->assertSame(ApcuAdapter::class, $adapterDefinition->getClass());
break;
case 'cache.adapter.doctrine':
$this->assertSame(DoctrineAdapter::class, $adapterDefinition->getClass());
break;
case 'cache.adapter.filesystem':
$this->assertSame(FilesystemAdapter::class, $adapterDefinition->getClass());
break;
}
}
private function assertCachePoolServiceDefinitionIsCreated(ContainerBuilder $container, $name, $adapter, $defaultLifetime)
{
$id = 'cache.pool.'.$name;
@ -728,7 +776,5 @@ abstract class FrameworkExtensionTest extends TestCase
$this->assertSame(FilesystemAdapter::class, $adapterDefinition->getClass());
break;
}
$this->assertTrue($adapterDefinition->isAbstract(), sprintf('Service definition "%s" is abstract.', $adapterId));
}
}