-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathTedivmStashExtension.php
170 lines (146 loc) · 5.8 KB
/
TedivmStashExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/*
* This file is part of the StashBundle package.
*
* (c) Josh Hall-Bachner <[email protected]>
* (c) Robert Hafner <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tedivm\StashBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Definition\Processor;
/**
* Class TedivmStashExtension
*
* Bundle extension to handle configuration of the Stash bundle. Based on the specification provided
* in the configuration file, this extension instantiates and dynamically injects the selected caching provider into
* the Stash service, passing it any driver-specific settings from the configuration.
*
* @package Tedivm\StashBundle\DependencyInjection
* @author Josh Hall-Bachner <[email protected]>
* @author Robert Hafner <[email protected]>
*/
class TedivmStashExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
$processor = new Processor();
$config = $processor->processConfiguration(new Configuration(), $configs);
$container->setAlias('stash', sprintf('stash.%s_cache', $config['default_cache']));
$lq = isset($config['tracking'])
? $config['tracking']
: (in_array($container->getParameter('kernel.environment'), array('dev', 'test')));
$container->setParameter('stash.tracker', $lq);
$lqv = isset($config['tracking_values']) ? $config['tracking_values'] : true;
$container->setParameter('stash.tracking_values', $lqv);
$caches = array();
$options = array();
foreach ($config['caches'] as $name => $cache) {
$caches[$name] = sprintf('stash.%s_cache', $name);
$options[$name] = $cache;
$this->addCacheService($name, $cache, $container);
}
$container->setParameter('stash.caches', $caches);
$container->setParameter('stash.caches.options', $options);
$container->setParameter('stash.default_cache', $config['default_cache']);
}
protected function addCacheService($name, $cache, $container)
{
$logqueries = $container->getParameter('stash.tracker');
$logQueryValues = $container->getParameter('stash.tracking_values');
$drivers = isset($cache['drivers']) ? $cache['drivers'] : array();
unset($cache['drivers']);
if (isset($cache['inMemory']) && $cache['inMemory']) {
array_unshift($drivers, 'Ephemeral');
}
unset($cache['inMemory']);
$doctrine = $cache['registerDoctrineAdapter'];
unset($cache['registerDoctrineAdapter']);
$session = $cache['registerSessionHandler'];
unset($cache['registerSessionHandler']);
$container
->setDefinition(sprintf('stash.driver.%s_cache', $name), new ChildDefinition('stash.driver'))
->setArguments(
[
$drivers,
$cache,
]
)
->setAbstract(false);
$container
->setDefinition(sprintf('stash.tracker.%s_cache', $name), new ChildDefinition('stash.tracker'))
->setArguments(
[
$name,
]
)
->addMethodCall('enableQueryLogging', [$logqueries])
->addMethodCall('enableQueryValueLogging', [$logQueryValues])
->setAbstract(false);
$cacheDefinition = new ChildDefinition('stash.cache');
$container
->setDefinition(sprintf('stash.%s_cache', $name), $cacheDefinition)
->setArguments(
[
$name,
new Reference(sprintf('stash.driver.%s_cache', $name)),
new Reference(sprintf('stash.tracker.%s_cache', $name)),
]
)
->setAbstract(false);
if (isset($cache['logger']) && $cache['logger']) {
$cacheDefinition->addMethodCall('setLogger', array(new Reference($cache['logger'])));
}
if (interface_exists("\\Doctrine\\Common\\Cache\\Cache") && $doctrine) {
$container
->setDefinition(
sprintf('stash.adapter.doctrine.%s_cache', $name),
new ChildDefinition('stash.adapter.doctrine')
)
->setArguments(
[
new Reference(sprintf('stash.%s_cache', $name)),
]
)
->setAbstract(false);
}
if ($session) {
$container
->setDefinition(
sprintf('stash.adapter.session.%s_cache', $name),
new ChildDefinition('stash.adapter.session')
)
->setArguments(
[
new Reference(sprintf('stash.%s_cache', $name)),
]
)
->setAbstract(false);
}
$container
->getDefinition('data_collector.stash')
->addMethodCall('addTracker', array(
new Reference(sprintf('stash.tracker.%s_cache', $name))
))
;
}
/**
* {@inheritdoc}
*/
public function getAlias()
{
return 'stash';
}
}