|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EmanueleMinotto\TwigCacheBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use EmanueleMinotto\TwigCacheBundle\DataCollector\TwigCacheCollector; |
|
6
|
|
|
use EmanueleMinotto\TwigCacheBundle\Strategy\ProfilerStrategy; |
|
7
|
|
|
use Symfony\Component\Config\FileLocator; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
12
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This is the class that loads and manages your bundle configuration. |
|
16
|
|
|
* |
|
17
|
|
|
* @see http://symfony.com/doc/current/cookbook/bundles/extension.html |
|
18
|
|
|
*/ |
|
19
|
|
|
class TwigCacheExtension extends Extension |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Loads a specific configuration. |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $config an array of configuration values |
|
25
|
|
|
* @param ContainerBuilder $container a ContainerBuilder instance |
|
26
|
|
|
* |
|
27
|
|
|
* @throws \InvalidArgumentException if tag is not defined |
|
28
|
|
|
*/ |
|
29
|
|
|
public function load(array $config, ContainerBuilder $container) |
|
30
|
|
|
{ |
|
31
|
|
|
$configuration = new Configuration(); |
|
32
|
|
|
$config = $this->processConfiguration($configuration, $config); |
|
33
|
|
|
|
|
34
|
|
|
$container->setAlias('twig_cache.service', $config['service']); |
|
35
|
|
|
$container->setAlias('twig_cache.strategy.key_generator', $config['key_generator']); |
|
36
|
|
|
|
|
37
|
|
|
$loader = new Loader\XmlFileLoader( |
|
38
|
|
|
$container, |
|
39
|
|
|
new FileLocator(__DIR__.'/../Resources/config') |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
$loader->load('services.xml'); |
|
43
|
|
|
|
|
44
|
|
|
$strategy = new Reference($config['strategy']); |
|
45
|
|
|
|
|
46
|
|
|
if ($config['profiler']) { |
|
47
|
|
|
$dataCollectorDefinition = new Definition(TwigCacheCollector::class); |
|
48
|
|
|
$dataCollectorDefinition->addTag('data_collector', [ |
|
49
|
|
|
'id' => 'asm89_cache', |
|
50
|
|
|
'template' => 'TwigCacheBundle:Collector:asm89_cache', |
|
51
|
|
|
]); |
|
52
|
|
|
$container->setDefinition(TwigCacheCollector::class, $dataCollectorDefinition); |
|
53
|
|
|
|
|
54
|
|
|
$strategy = new Definition(ProfilerStrategy::class, [ |
|
55
|
|
|
new Reference($config['strategy']), |
|
56
|
|
|
new Reference(TwigCacheCollector::class), |
|
57
|
|
|
]); |
|
58
|
|
|
|
|
59
|
|
|
$container->addDefinitions([$strategy]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$container->getDefinition('twig_cache.extension')->replaceArgument(0, $strategy); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|