|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Alpha\TwigBundle\Loader; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use Doctrine\ORM\NoResultException; |
|
9
|
|
|
use Twig\Error\LoaderError; |
|
10
|
|
|
use Twig\Loader\LoaderInterface; |
|
11
|
|
|
use Twig\Source; |
|
12
|
|
|
|
|
13
|
|
|
class DatabaseTwigLoader implements LoaderInterface |
|
14
|
|
|
{ |
|
15
|
|
|
protected $entityManager; |
|
16
|
|
|
protected $entity; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(EntityManagerInterface $entityManager, string $entity) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->entityManager = $entityManager; |
|
21
|
|
|
$this->entity = $entity; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function getCacheKey($name): string |
|
25
|
|
|
{ |
|
26
|
|
|
return $name; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function exists($templateName): bool |
|
30
|
|
|
{ |
|
31
|
|
|
try { |
|
32
|
|
|
$template = $this->getTemplate($templateName); |
|
33
|
|
|
|
|
34
|
|
|
return $template instanceof $this->entity; |
|
35
|
|
|
} catch (NoResultException $e) { |
|
36
|
|
|
return false; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
private function getTemplate(string $templateName): object |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->entityManager |
|
43
|
|
|
->getRepository($this->entity) |
|
44
|
|
|
->createQueryBuilder('t') |
|
|
|
|
|
|
45
|
|
|
->select('t') |
|
46
|
|
|
->where('t.name = :name') |
|
47
|
|
|
->setMaxResults(1) |
|
48
|
|
|
->getQuery() |
|
49
|
|
|
->setParameter('name', $templateName) |
|
50
|
|
|
->getSingleResult(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getSourceContext($templateName): Source |
|
54
|
|
|
{ |
|
55
|
|
|
$templateSource = $this->getValue('source', $templateName); |
|
56
|
|
|
if (!is_string($templateSource) || mb_strlen($templateSource) < 1) { |
|
57
|
|
|
throw new LoaderError(sprintf('Template "%s" does not exist.', $templateName)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return new Source($templateSource, $templateName); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function isFresh($templateName, $time): bool |
|
64
|
|
|
{ |
|
65
|
|
|
try { |
|
66
|
|
|
$lastModified = $this->getValue('lastModified', $templateName); |
|
67
|
|
|
if (null === $lastModified) { |
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return strtotime($lastModified) <= $time; |
|
72
|
|
|
} catch (NoResultException $e) { |
|
73
|
|
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
private function getValue(string $column, string $templateName) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->entityManager |
|
83
|
|
|
->getRepository($this->entity) |
|
84
|
|
|
->createQueryBuilder('t') |
|
85
|
|
|
->select(sprintf('t.%s', $column)) |
|
86
|
|
|
->where('t.name = :name') |
|
87
|
|
|
->setMaxResults(1) |
|
88
|
|
|
->getQuery() |
|
89
|
|
|
->setParameter('name', $templateName) |
|
90
|
|
|
->getSingleScalarResult(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|