1 | <?php |
||
11 | class DoctrineLoader implements \Twig_LoaderInterface |
||
12 | { |
||
13 | const KEY_SEPARATOR = '|'; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $prefix; |
||
19 | |||
20 | /** |
||
21 | * @var ObjectRepository |
||
22 | */ |
||
23 | protected $repository; |
||
24 | |||
25 | /** |
||
26 | * @var callable|null |
||
27 | */ |
||
28 | protected $localeCallable; |
||
29 | |||
30 | /** |
||
31 | * Constructor |
||
32 | * |
||
33 | * @param ObjectRepository $repository The template repository |
||
34 | * @param string $prefix The template prefix to identify database templates |
||
35 | * @param callable|null $localeCallable |
||
36 | */ |
||
37 | 19 | public function __construct(ObjectRepository $repository, $prefix = 'database::', $localeCallable = null) |
|
38 | { |
||
39 | 19 | if ($localeCallable !== null && !is_callable($localeCallable)) { |
|
40 | 1 | throw new \InvalidArgumentException('Given `localeCallable` must be a callable or NULL.'); |
|
41 | } |
||
42 | |||
43 | 18 | $this->repository = $repository; |
|
44 | 18 | $this->prefix = $prefix; |
|
45 | 18 | $this->localeCallable = $localeCallable; |
|
46 | 18 | } |
|
47 | |||
48 | /** |
||
49 | * @inheritdoc |
||
50 | */ |
||
51 | 4 | public function getSource($name) |
|
55 | |||
56 | /** |
||
57 | * @inheritdoc |
||
58 | */ |
||
59 | public function exists($name) |
||
60 | { |
||
61 | if ($this->isLoadableTemplate($name)) { |
||
62 | $templateIdentifier = substr($name, strlen($this->prefix)); |
||
63 | $template = $this->repository->find($templateIdentifier); |
||
64 | |||
65 | return $template !== null && $template instanceof TemplateInterface; |
||
66 | } |
||
67 | return false; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @inheritdoc |
||
72 | */ |
||
73 | 8 | public function getCacheKey($name) |
|
74 | { |
||
75 | 8 | $template = $this->findTemplate($name); |
|
76 | |||
77 | 3 | if ($template instanceof TranslatableTemplateInterface) { |
|
78 | /** @var TranslatableTemplateInterface $template */ |
||
79 | |||
80 | 2 | return $this->prefix . self::KEY_SEPARATOR . $template->getCurrentLocale() . self::KEY_SEPARATOR . $template->getIdentifier(); |
|
81 | } |
||
82 | |||
83 | 1 | return $this->prefix . self::KEY_SEPARATOR . $template->getIdentifier(); |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * @inheritdoc |
||
88 | */ |
||
89 | 6 | public function isFresh($name, $time) |
|
93 | |||
94 | /** |
||
95 | * Check if the given name has the correct prefix for loading. |
||
96 | * |
||
97 | * @param string $name The name of the template to check |
||
98 | * @return bool |
||
99 | */ |
||
100 | 18 | protected function isLoadableTemplate($name) |
|
104 | |||
105 | /** |
||
106 | * Find a template by it's name |
||
107 | * |
||
108 | * @param string $name The name of the template to find |
||
109 | * @return TemplateInterface |
||
110 | * @throws \Twig_Error_Loader |
||
111 | */ |
||
112 | 18 | protected function findTemplate($name) |
|
134 | |||
135 | 2 | protected function getCurrentLocale() |
|
146 | } |
||
147 |