Skip to content

Commit 1f423a0

Browse files
committed
Refactored the doctrine compiler pass
1 parent 2bd99f1 commit 1f423a0

File tree

4 files changed

+24
-79
lines changed

4 files changed

+24
-79
lines changed

src/DependencyInjection/CacheExtension.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
use Cache\Bridge\DoctrineCacheBridge;
1515
use Cache\CacheBundle\Bridge\SessionHandlerBridge;
1616
use Cache\CacheBundle\Bridge\SymfonyValidatorBridge;
17-
use Cache\CacheBundle\Factory\AnnotationFactory;
18-
use Cache\CacheBundle\Factory\SerializerFactory;
17+
use Cache\CacheBundle\Factory\DoctrineBridgeFactory;
1918
use Cache\CacheBundle\Factory\ValidationFactory;
2019
use Symfony\Component\Config\FileLocator;
2120
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -51,17 +50,19 @@ public function load(array $configs, ContainerBuilder $container)
5150
if ($config['annotation']['enabled']) {
5251
$this->verifyDoctrineBridgeExists('annotation');
5352
$container->register('cache.service.annotation', DoctrineCacheBridge::class)
54-
->setFactory([AnnotationFactory::class, 'get'])
53+
->setFactory([DoctrineBridgeFactory::class, 'get'])
5554
->addArgument(new Reference($config['annotation']['service_id']))
56-
->addArgument($config['annotation']);
55+
->addArgument($config['annotation'])
56+
->addArgument('annotation');
5757
}
5858

5959
if ($config['serializer']['enabled']) {
6060
$this->verifyDoctrineBridgeExists('serializer');
6161
$container->register('cache.service.serializer', DoctrineCacheBridge::class)
62-
->setFactory([SerializerFactory::class, 'get'])
62+
->setFactory([DoctrineBridgeFactory::class, 'get'])
6363
->addArgument(new Reference($config['serializer']['service_id']))
64-
->addArgument($config['serializer']);
64+
->addArgument($config['serializer'])
65+
->addArgument('serializer');
6566
}
6667

6768
if ($config['validation']['enabled']) {

src/DependencyInjection/Compiler/DoctrineSupportCompilerPass.php

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Cache\Bridge\DoctrineCacheBridge;
1515
use Cache\CacheBundle\Cache\FixedTaggingCachePool;
16+
use Cache\CacheBundle\Factory\DoctrineBridgeFactory;
1617
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1718
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1819
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -70,19 +71,23 @@ public function process(ContainerBuilder $container)
7071
protected function enableDoctrineSupport(array $config)
7172
{
7273
$types = ['entity_managers', 'document_managers'];
73-
foreach ($config as $cacheType => $cacheData) {
74+
// For each ['metadata' => [], 'result' => [], 'query' => []]
75+
foreach ($config as $cacheType => $typeConfig) {
7476
foreach ($types as $type) {
75-
if (!isset($cacheData[$type])) {
77+
if (!isset($typeConfig[$type])) {
7678
continue;
7779
}
7880

7981
// Doctrine can't talk to a PSR-6 cache, so we need a bridge
80-
$bridgeServiceId = sprintf('cache.provider.doctrine.%s.bridge', $cacheType);
81-
$bridgeDef = $this->container->register($bridgeServiceId, DoctrineCacheBridge::class);
82-
$bridgeDef->addArgument(new Reference($this->getPoolReferenceForBridge($bridgeServiceId, $cacheData, $config['use_tagging'])))
83-
->setPublic(false);
84-
85-
foreach ($cacheData[$type] as $manager) {
82+
$bridgeServiceId = sprintf('cache.service.doctrine.%s.%s.bridge', $cacheType, $type);
83+
$this->container->register($bridgeServiceId, FixedTaggingCachePool::class)
84+
->setPublic(false)
85+
->setFactory([DoctrineBridgeFactory::class, 'get'])
86+
->addArgument(new Reference($typeConfig['service_id']))
87+
->addArgument($typeConfig)
88+
->addArgument(['doctrine', $cacheType]);
89+
90+
foreach ($typeConfig[$type] as $manager) {
8691
$doctrineDefinitionId =
8792
sprintf(
8893
'doctrine.%s.%s_%s_cache',
@@ -98,31 +103,6 @@ protected function enableDoctrineSupport(array $config)
98103
}
99104
}
100105

101-
/**
102-
* Get a reference string for the PSR-6 cache implementation service to use with doctrine.
103-
* If we support tagging we use the DoctrineTaggingCachePool.
104-
*
105-
* @param string $bridgeServiceId
106-
* @param array $cacheData
107-
* @param bool $tagging
108-
*
109-
* @return string
110-
*/
111-
public function getPoolReferenceForBridge($bridgeServiceId, $cacheData, $tagging)
112-
{
113-
if (!$tagging) {
114-
return $cacheData['service_id'];
115-
}
116-
117-
$taggingServiceId = $bridgeServiceId.'.tagging';
118-
$taggingDef = $this->container->register($taggingServiceId, FixedTaggingCachePool::class);
119-
$taggingDef->addArgument(new Reference($cacheData['service_id']))
120-
->addArgument(['doctrine'])
121-
->setPublic(false);
122-
123-
return $taggingServiceId;
124-
}
125-
126106
/**
127107
* Checks to see if there are ORM's or ODM's.
128108
*
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@
1818
/**
1919
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2020
*/
21-
class AnnotationFactory
21+
class DoctrineBridgeFactory
2222
{
2323
/**
2424
* @param CacheItemPoolInterface $pool
2525
* @param array $config
26+
* @param array $tags
2627
*
2728
* @return DoctrineCacheBridge
2829
*/
29-
public static function get(CacheItemPoolInterface $pool, $config)
30+
public static function get(CacheItemPoolInterface $pool, $config, array $tags)
3031
{
3132
if ($config['use_tagging']) {
32-
$pool = new FixedTaggingCachePool($pool, ['annotation']);
33+
$pool = new FixedTaggingCachePool($pool, $tags);
3334
}
3435

3536
return new DoctrineCacheBridge($pool);

src/Factory/SerializerFactory.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)