1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EmanueleMinotto\TwigCacheBundle\Strategy; |
4
|
|
|
|
5
|
|
|
use Asm89\Twig\CacheExtension\CacheStrategyInterface; |
6
|
|
|
use EmanueleMinotto\TwigCacheBundle\DataCollector\TwigCacheCollector; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Wrapper used to profile cache usage. |
10
|
|
|
*/ |
11
|
|
|
class ProfilerStrategy implements CacheStrategyInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var CacheStrategyInterface |
15
|
|
|
*/ |
16
|
|
|
private $cacheStrategy; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var TwigCacheCollector |
20
|
|
|
*/ |
21
|
|
|
private $dataCollector; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param CacheStrategyInterface $cacheStrategy |
25
|
|
|
* @param TwigCacheCollector $dataCollector |
26
|
|
|
*/ |
27
|
|
|
public function __construct(CacheStrategyInterface $cacheStrategy, TwigCacheCollector $dataCollector) |
28
|
|
|
{ |
29
|
|
|
$this->cacheStrategy = $cacheStrategy; |
30
|
|
|
$this->dataCollector = $dataCollector; |
31
|
|
|
|
32
|
|
|
$dataCollector->setStrategyClass(get_class($cacheStrategy)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Fetch the block for a given key. |
37
|
|
|
* |
38
|
|
|
* @param mixed $key |
39
|
|
|
* |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
|
public function fetchBlock($key) |
43
|
|
|
{ |
44
|
|
|
$output = $this->cacheStrategy->fetchBlock($key); |
45
|
|
|
$this->dataCollector->addFetchBlock($key, $output); |
46
|
|
|
|
47
|
|
|
return $output; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Generate a key for the value. |
52
|
|
|
* |
53
|
|
|
* @param string $annotation |
54
|
|
|
* @param mixed $value |
55
|
|
|
* |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
|
|
public function generateKey($annotation, $value) |
59
|
|
|
{ |
60
|
|
|
$this->dataCollector->addGenerateKey($annotation, $value); |
61
|
|
|
|
62
|
|
|
return $this->cacheStrategy->generateKey($annotation, $value); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Save the contents of a rendered block. |
67
|
|
|
* |
68
|
|
|
* @param mixed $key |
69
|
|
|
* @param string $block |
70
|
|
|
* |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
public function saveBlock($key, $block) |
74
|
|
|
{ |
75
|
|
|
return $this->cacheStrategy->saveBlock($key, $block); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|