1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of php-cache\cache-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cache\CacheBundle\DependencyInjection\Compiler; |
13
|
|
|
|
14
|
|
|
use Cache\Bridge\Doctrine\DoctrineCacheBridge; |
15
|
|
|
use Cache\CacheBundle\Factory\DoctrineBridgeFactory; |
16
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
17
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Add the doctrine bridge around the PSR-6 cache services. |
23
|
|
|
* |
24
|
|
|
* @author Aaron Scherer <[email protected]> |
25
|
|
|
* @author Tobias Nyholm <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class DoctrineCompilerPass implements CompilerPassInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @type ContainerBuilder |
31
|
|
|
*/ |
32
|
|
|
private $container; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @throws \Exception |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
1 |
|
public function process(ContainerBuilder $container) |
40
|
|
|
{ |
41
|
1 |
|
$this->container = $container; |
42
|
|
|
|
43
|
|
|
// If disabled, continue |
|
|
|
|
44
|
1 |
|
if (!$this->container->hasParameter('cache.doctrine')) { |
45
|
1 |
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (!$this->hasDoctrine()) { |
49
|
|
|
throw new \Exception( |
50
|
|
|
'Not able to find any doctrine caches to implement. Ensure you have Doctrine ORM or ODM' |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->enableDoctrineSupport($this->container->getParameter('cache.doctrine')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Loads the Doctrine configuration. |
59
|
|
|
* |
60
|
|
|
* @param array $config A configuration array |
61
|
|
|
* |
62
|
|
|
* @throws InvalidConfigurationException |
63
|
|
|
*/ |
64
|
|
|
protected function enableDoctrineSupport(array $config) |
65
|
|
|
{ |
66
|
|
|
$types = ['entity_managers', 'document_managers']; |
67
|
|
|
// For each ['metadata' => [], 'result' => [], 'query' => []] |
|
|
|
|
68
|
|
|
foreach ($config as $cacheType => $typeConfig) { |
69
|
|
|
foreach ($types as $type) { |
70
|
|
|
if (!isset($typeConfig[$type])) { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Copy the tagging setting to the $typeConfig |
75
|
|
|
$typeConfig['use_tagging'] = $config['use_tagging']; |
76
|
|
|
|
77
|
|
|
// Doctrine can't talk to a PSR-6 cache, so we need a bridge |
78
|
|
|
$bridgeServiceId = sprintf('cache.service.doctrine.%s.%s.bridge', $cacheType, $type); |
79
|
|
|
$this->container->register($bridgeServiceId, DoctrineCacheBridge::class) |
80
|
|
|
->setPublic(false) |
81
|
|
|
->setFactory([DoctrineBridgeFactory::class, 'get']) |
82
|
|
|
->addArgument(new Reference($typeConfig['service_id'])) |
83
|
|
|
->addArgument($typeConfig) |
84
|
|
|
->addArgument(['doctrine', $cacheType]); |
85
|
|
|
|
86
|
|
|
foreach ($typeConfig[$type] as $manager) { |
87
|
|
|
$doctrineDefinitionId = |
88
|
|
|
sprintf( |
89
|
|
|
'doctrine.%s.%s_%s_cache', |
90
|
|
|
($type === 'entity_managers' ? 'orm' : 'odm'), |
91
|
|
|
$manager, |
92
|
|
|
$cacheType |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
// Replace the doctrine entity manager cache with our bridge |
96
|
|
|
$this->container->setAlias($doctrineDefinitionId, $bridgeServiceId); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Checks to see if there are ORM's or ODM's. |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
private function hasDoctrine() |
108
|
|
|
{ |
109
|
|
|
return |
110
|
|
|
$this->container->hasAlias('doctrine.orm.entity_manager') || |
111
|
|
|
$this->container->hasAlias('doctrine_mongodb.document_manager'); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.