Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/Cache/FixedTaggingCachePool.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Psr\Cache\CacheItemPoolInterface;

/**
* This class is a decorator for a TaggablePoolInterface. It tags everything with 'doctrine'.
* This class is a decorator for a TaggablePoolInterface. It tags everything with predefined tags.
* Use this class with the DoctrineBridge.
*
* @author Tobias Nyholm <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion src/CacheBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new Compiler\CacheTaggingPass());
$container->addCompilerPass(new Compiler\LoggingCompilerPass());
$container->addCompilerPass(new Compiler\SessionSupportCompilerPass());
$container->addCompilerPass(new Compiler\DoctrineSupportCompilerPass());
$container->addCompilerPass(new Compiler\DoctrineCompilerPass());

if ($container->getParameter('kernel.debug')) {
$container->addCompilerPass(new Compiler\DataCollectorCompilerPass());
Expand Down
28 changes: 17 additions & 11 deletions src/DependencyInjection/CacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
use Cache\Bridge\DoctrineCacheBridge;
use Cache\CacheBundle\Bridge\SessionHandlerBridge;
use Cache\CacheBundle\Bridge\SymfonyValidatorBridge;
use Cache\CacheBundle\Factory\AnnotationFactory;
use Cache\CacheBundle\Factory\SerializerFactory;
use Cache\CacheBundle\Factory\DoctrineBridgeFactory;
use Cache\CacheBundle\Factory\ValidationFactory;
use Cache\CacheBundle\Routing\CachingRouter;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
Expand Down Expand Up @@ -48,20 +48,26 @@ public function load(array $configs, ContainerBuilder $container)
}
}

if ($config['doctrine']['enabled']) {
$this->verifyDoctrineBridgeExists('doctrine');
}

if ($config['annotation']['enabled']) {
$this->verifyDoctrineBridgeExists('annotation');
$container->register('cache.service.annotation', DoctrineCacheBridge::class)
->setFactory([AnnotationFactory::class, 'get'])
->setFactory([DoctrineBridgeFactory::class, 'get'])
->addArgument(new Reference($config['annotation']['service_id']))
->addArgument($config['annotation']);
->addArgument($config['annotation'])
->addArgument('annotation');
}

if ($config['serializer']['enabled']) {
$this->verifyDoctrineBridgeExists('serializer');
$container->register('cache.service.serializer', DoctrineCacheBridge::class)
->setFactory([SerializerFactory::class, 'get'])
->setFactory([DoctrineBridgeFactory::class, 'get'])
->addArgument(new Reference($config['serializer']['service_id']))
->addArgument($config['serializer']);
->addArgument($config['serializer'])
->addArgument('serializer');
}

if ($config['validation']['enabled']) {
Expand All @@ -79,11 +85,11 @@ public function load(array $configs, ContainerBuilder $container)
}

if ($config['router']['enabled']) {
$loader->load('router.yml');
$container->getDefinition('cache.router')
$container->register('cache.service.router', CachingRouter::class)
->setDecoratedService('router', null, 10)
->replaceArgument(0, new Reference($config['router']['service_id']))
->replaceArgument(2, $config['router']['ttl']);
->addArgument(new Reference($config['router']['service_id']))
->addArgument(new Reference('cache.service.router.inner'))
->addArgument($config['router']['ttl']);
}

if ($container->getParameter('kernel.debug')) {
Expand All @@ -92,7 +98,7 @@ public function load(array $configs, ContainerBuilder $container)

$serviceIds = [];
$this->findServiceIds($config, $serviceIds);
$container->setParameter('cache.provider.serviceIds', $serviceIds);
$container->setParameter('cache.provider_service_ids', $serviceIds);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Compiler/CacheTaggingPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CacheTaggingPass implements CompilerPassInterface
public function process(ContainerBuilder $container)
{
// get service ids form parameters
$serviceIds = $container->getParameter('cache.provider.serviceIds');
$serviceIds = $container->getParameter('cache.provider_service_ids');

foreach ($serviceIds as $id) {
$def = $container->findDefinition($id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Cache\CacheBundle\DependencyInjection\Compiler;

use Cache\Bridge\DoctrineCacheBridge;
use Cache\CacheBundle\Cache\FixedTaggingCachePool;
use Cache\CacheBundle\Factory\DoctrineBridgeFactory;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -24,7 +24,7 @@
* @author Aaron Scherer <[email protected]>
* @author Tobias Nyholm <[email protected]>
*/
class DoctrineSupportCompilerPass implements CompilerPassInterface
class DoctrineCompilerPass implements CompilerPassInterface
{
/**
* @type ContainerBuilder
Expand All @@ -51,12 +51,6 @@ public function process(ContainerBuilder $container)
);
}

if (!class_exists('Cache\Bridge\DoctrineCacheBridge')) {
throw new \Exception(
'You need the DoctrineBridge to be able to cache queries, results and metadata. Please run "composer require cache/psr-6-doctrine-bridge" to install the missing dependency.'
);
}

$this->enableDoctrineSupport($this->container->getParameter('cache.doctrine'));
}

Expand All @@ -70,19 +64,23 @@ public function process(ContainerBuilder $container)
protected function enableDoctrineSupport(array $config)
{
$types = ['entity_managers', 'document_managers'];
foreach ($config as $cacheType => $cacheData) {
// For each ['metadata' => [], 'result' => [], 'query' => []]
foreach ($config as $cacheType => $typeConfig) {
foreach ($types as $type) {
if (!isset($cacheData[$type])) {
if (!isset($typeConfig[$type])) {
continue;
}

// Doctrine can't talk to a PSR-6 cache, so we need a bridge
$bridgeServiceId = sprintf('cache.provider.doctrine.%s.bridge', $cacheType);
$bridgeDef = $this->container->register($bridgeServiceId, DoctrineCacheBridge::class);
$bridgeDef->addArgument(new Reference($this->getPoolReferenceForBridge($bridgeServiceId, $cacheData, $config['use_tagging'])))
->setPublic(false);

foreach ($cacheData[$type] as $manager) {
$bridgeServiceId = sprintf('cache.service.doctrine.%s.%s.bridge', $cacheType, $type);
$this->container->register($bridgeServiceId, FixedTaggingCachePool::class)
->setPublic(false)
->setFactory([DoctrineBridgeFactory::class, 'get'])
->addArgument(new Reference($typeConfig['service_id']))
->addArgument($typeConfig)
->addArgument(['doctrine', $cacheType]);

foreach ($typeConfig[$type] as $manager) {
$doctrineDefinitionId =
sprintf(
'doctrine.%s.%s_%s_cache',
Expand All @@ -98,31 +96,6 @@ protected function enableDoctrineSupport(array $config)
}
}

/**
* Get a reference string for the PSR-6 cache implementation service to use with doctrine.
* If we support tagging we use the DoctrineTaggingCachePool.
*
* @param string $bridgeServiceId
* @param array $cacheData
* @param bool $tagging
*
* @return string
*/
public function getPoolReferenceForBridge($bridgeServiceId, $cacheData, $tagging)
{
if (!$tagging) {
return $cacheData['service_id'];
}

$taggingServiceId = $bridgeServiceId.'.tagging';
$taggingDef = $this->container->register($taggingServiceId, FixedTaggingCachePool::class);
$taggingDef->addArgument(new Reference($cacheData['service_id']))
->addArgument(['doctrine'])
->setPublic(false);

return $taggingServiceId;
}

/**
* Checks to see if there are ORM's or ODM's.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
/**
* @author Tobias Nyholm <[email protected]>
*/
class AnnotationFactory
class DoctrineBridgeFactory
{
/**
* @param CacheItemPoolInterface $pool
* @param array $config
* @param array $tags
*
* @return DoctrineCacheBridge
*/
public static function get(CacheItemPoolInterface $pool, $config)
public static function get(CacheItemPoolInterface $pool, $config, array $tags)
{
if ($config['use_tagging']) {
$pool = new FixedTaggingCachePool($pool, ['annotation']);
$pool = new FixedTaggingCachePool($pool, $tags);
}

return new DoctrineCacheBridge($pool);
Expand Down
37 changes: 0 additions & 37 deletions src/Factory/SerializerFactory.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/Resources/config/router.yml

This file was deleted.