|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BCRM\BackendBundle\Content; |
|
4
|
|
|
|
|
5
|
|
|
use Twig_Error_Loader; |
|
6
|
|
|
|
|
7
|
|
|
class TwigLoader implements \Twig_LoaderInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var ContentReader |
|
11
|
|
|
*/ |
|
12
|
|
|
private $cr; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(ContentReader $cr) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->cr = $cr; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Gets the source code of a template, given its name. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $name The name of the template to load |
|
23
|
|
|
* |
|
24
|
|
|
* @return string The template source code |
|
25
|
|
|
* |
|
26
|
|
|
* @throws Twig_Error_Loader When $name is not found |
|
27
|
|
|
*/ |
|
28
|
|
|
public function getSource($name) |
|
29
|
|
|
{ |
|
30
|
|
|
$template = $this->cr->getContent($this->getFile($name)); |
|
31
|
|
|
return $template->getContent(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function getFile($name) |
|
35
|
|
|
{ |
|
36
|
|
|
if (!preg_match('/^bcrm_content:(.+)/', $name, $match)) throw new Twig_Error_Loader(sprintf('Unknown template: %s', $name)); |
|
37
|
|
|
return $match[1]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Gets the cache key to use for the cache for a given template name. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $name The name of the template to load |
|
44
|
|
|
* |
|
45
|
|
|
* @return string The cache key |
|
46
|
|
|
* |
|
47
|
|
|
* @throws Twig_Error_Loader When $name is not found |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getCacheKey($name) |
|
50
|
|
|
{ |
|
51
|
|
|
return md5($this->getFile($name)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns true if the template is still fresh. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $name The template name |
|
58
|
|
|
* @param int $time The last modification time of the cached template |
|
59
|
|
|
* |
|
60
|
|
|
* @return Boolean true if the template is fresh, false otherwise |
|
61
|
|
|
* |
|
62
|
|
|
* @throws Twig_Error_Loader When $name is not found |
|
63
|
|
|
*/ |
|
64
|
|
|
public function isFresh($name, $time) |
|
65
|
|
|
{ |
|
66
|
|
|
$info = $this->cr->getInfo($this->getFile($name)); |
|
67
|
|
|
return $info->getLastModified()->getTimestamp() > $time; |
|
68
|
|
|
} |
|
69
|
|
|
} |