Skip to content

Commit 3fdb646

Browse files
committed
Merge pull request #37 from Nyholm/features
Features
2 parents 2bd99f1 + 3f31236 commit 3fdb646

File tree

8 files changed

+38
-102
lines changed

8 files changed

+38
-102
lines changed

src/Cache/FixedTaggingCachePool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Psr\Cache\CacheItemPoolInterface;
1717

1818
/**
19-
* This class is a decorator for a TaggablePoolInterface. It tags everything with 'doctrine'.
19+
* This class is a decorator for a TaggablePoolInterface. It tags everything with predefined tags.
2020
* Use this class with the DoctrineBridge.
2121
*
2222
* @author Tobias Nyholm <[email protected]>

src/CacheBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function build(ContainerBuilder $container)
3232
$container->addCompilerPass(new Compiler\CacheTaggingPass());
3333
$container->addCompilerPass(new Compiler\LoggingCompilerPass());
3434
$container->addCompilerPass(new Compiler\SessionSupportCompilerPass());
35-
$container->addCompilerPass(new Compiler\DoctrineSupportCompilerPass());
35+
$container->addCompilerPass(new Compiler\DoctrineCompilerPass());
3636

3737
if ($container->getParameter('kernel.debug')) {
3838
$container->addCompilerPass(new Compiler\DataCollectorCompilerPass());

src/DependencyInjection/CacheExtension.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
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;
19+
use Cache\CacheBundle\Routing\CachingRouter;
2020
use Symfony\Component\Config\FileLocator;
2121
use Symfony\Component\DependencyInjection\ContainerBuilder;
2222
use Symfony\Component\DependencyInjection\Loader;
@@ -48,20 +48,26 @@ public function load(array $configs, ContainerBuilder $container)
4848
}
4949
}
5050

51+
if ($config['doctrine']['enabled']) {
52+
$this->verifyDoctrineBridgeExists('doctrine');
53+
}
54+
5155
if ($config['annotation']['enabled']) {
5256
$this->verifyDoctrineBridgeExists('annotation');
5357
$container->register('cache.service.annotation', DoctrineCacheBridge::class)
54-
->setFactory([AnnotationFactory::class, 'get'])
58+
->setFactory([DoctrineBridgeFactory::class, 'get'])
5559
->addArgument(new Reference($config['annotation']['service_id']))
56-
->addArgument($config['annotation']);
60+
->addArgument($config['annotation'])
61+
->addArgument('annotation');
5762
}
5863

5964
if ($config['serializer']['enabled']) {
6065
$this->verifyDoctrineBridgeExists('serializer');
6166
$container->register('cache.service.serializer', DoctrineCacheBridge::class)
62-
->setFactory([SerializerFactory::class, 'get'])
67+
->setFactory([DoctrineBridgeFactory::class, 'get'])
6368
->addArgument(new Reference($config['serializer']['service_id']))
64-
->addArgument($config['serializer']);
69+
->addArgument($config['serializer'])
70+
->addArgument('serializer');
6571
}
6672

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

8187
if ($config['router']['enabled']) {
82-
$loader->load('router.yml');
83-
$container->getDefinition('cache.router')
88+
$container->register('cache.service.router', CachingRouter::class)
8489
->setDecoratedService('router', null, 10)
85-
->replaceArgument(0, new Reference($config['router']['service_id']))
86-
->replaceArgument(2, $config['router']['ttl']);
90+
->addArgument(new Reference($config['router']['service_id']))
91+
->addArgument(new Reference('cache.service.router.inner'))
92+
->addArgument($config['router']['ttl']);
8793
}
8894

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

9399
$serviceIds = [];
94100
$this->findServiceIds($config, $serviceIds);
95-
$container->setParameter('cache.provider.serviceIds', $serviceIds);
101+
$container->setParameter('cache.provider_service_ids', $serviceIds);
96102
}
97103

98104
/**

src/DependencyInjection/Compiler/CacheTaggingPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CacheTaggingPass implements CompilerPassInterface
2727
public function process(ContainerBuilder $container)
2828
{
2929
// get service ids form parameters
30-
$serviceIds = $container->getParameter('cache.provider.serviceIds');
30+
$serviceIds = $container->getParameter('cache.provider_service_ids');
3131

3232
foreach ($serviceIds as $id) {
3333
$def = $container->findDefinition($id);

src/DependencyInjection/Compiler/DoctrineSupportCompilerPass.php renamed to src/DependencyInjection/Compiler/DoctrineCompilerPass.php

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace Cache\CacheBundle\DependencyInjection\Compiler;
1313

14-
use Cache\Bridge\DoctrineCacheBridge;
1514
use Cache\CacheBundle\Cache\FixedTaggingCachePool;
15+
use Cache\CacheBundle\Factory\DoctrineBridgeFactory;
1616
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1717
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1818
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -24,7 +24,7 @@
2424
* @author Aaron Scherer <[email protected]>
2525
* @author Tobias Nyholm <[email protected]>
2626
*/
27-
class DoctrineSupportCompilerPass implements CompilerPassInterface
27+
class DoctrineCompilerPass implements CompilerPassInterface
2828
{
2929
/**
3030
* @type ContainerBuilder
@@ -51,12 +51,6 @@ public function process(ContainerBuilder $container)
5151
);
5252
}
5353

54-
if (!class_exists('Cache\Bridge\DoctrineCacheBridge')) {
55-
throw new \Exception(
56-
'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.'
57-
);
58-
}
59-
6054
$this->enableDoctrineSupport($this->container->getParameter('cache.doctrine'));
6155
}
6256

@@ -70,19 +64,23 @@ public function process(ContainerBuilder $container)
7064
protected function enableDoctrineSupport(array $config)
7165
{
7266
$types = ['entity_managers', 'document_managers'];
73-
foreach ($config as $cacheType => $cacheData) {
67+
// For each ['metadata' => [], 'result' => [], 'query' => []]
68+
foreach ($config as $cacheType => $typeConfig) {
7469
foreach ($types as $type) {
75-
if (!isset($cacheData[$type])) {
70+
if (!isset($typeConfig[$type])) {
7671
continue;
7772
}
7873

7974
// 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) {
75+
$bridgeServiceId = sprintf('cache.service.doctrine.%s.%s.bridge', $cacheType, $type);
76+
$this->container->register($bridgeServiceId, FixedTaggingCachePool::class)
77+
->setPublic(false)
78+
->setFactory([DoctrineBridgeFactory::class, 'get'])
79+
->addArgument(new Reference($typeConfig['service_id']))
80+
->addArgument($typeConfig)
81+
->addArgument(['doctrine', $cacheType]);
82+
83+
foreach ($typeConfig[$type] as $manager) {
8684
$doctrineDefinitionId =
8785
sprintf(
8886
'doctrine.%s.%s_%s_cache',
@@ -98,31 +96,6 @@ protected function enableDoctrineSupport(array $config)
9896
}
9997
}
10098

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-
12699
/**
127100
* Checks to see if there are ORM's or ODM's.
128101
*

src/Factory/AnnotationFactory.php renamed to src/Factory/DoctrineBridgeFactory.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@
1818
/**
1919
* @author Tobias Nyholm <[email protected]>
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.

src/Resources/config/router.yml

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

0 commit comments

Comments
 (0)