1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2018 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\Templating\Twig; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\Compiler; |
11
|
|
|
use allejo\stakx\Templating\TemplateBridgeInterface; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
|
14
|
|
|
class TwigStakxBridge implements TemplateBridgeInterface |
15
|
|
|
{ |
16
|
|
|
private $twig; |
17
|
|
|
private $logger; |
18
|
|
|
|
19
|
|
|
/** @var \Twig_Profiler_Profile */ |
20
|
|
|
private $profiler; |
21
|
|
|
|
22
|
76 |
|
public function __construct(\Twig_Environment $twig) |
23
|
|
|
{ |
24
|
76 |
|
$this->twig = $twig; |
25
|
76 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function setLogger(LoggerInterface $logger) |
31
|
|
|
{ |
32
|
|
|
$this->logger = $logger; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
12 |
|
public function setGlobalVariable($key, $value) |
39
|
|
|
{ |
40
|
12 |
|
$this->twig->addGlobal($key, $value); |
41
|
12 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function clearTemplateCache() |
47
|
|
|
{ |
48
|
|
|
$this->twig->clearTemplateCache(); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
12 |
|
public function createTemplate($templateContent) |
55
|
|
|
{ |
56
|
|
|
try |
57
|
|
|
{ |
58
|
12 |
|
$template = $this->twig->createTemplate($templateContent); |
59
|
|
|
} |
60
|
|
|
catch (\Twig_Error $e) |
61
|
|
|
{ |
62
|
|
|
throw new TwigError($e); |
63
|
|
|
} |
64
|
|
|
|
65
|
12 |
|
return new TwigTemplate($template); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
21 |
View Code Duplication |
public function getAssortmentDependencies($namespace, $bodyContent) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
// To see what this regex should match and what shouldn't be see: |
74
|
|
|
// tests/allejo/stakx/Test/FrontMatter/FrontMatterDocumentTest.php |
75
|
|
|
|
76
|
21 |
|
$regex = "/{[{%].*?(?:$namespace)(?:\.|\[['\"])?([^_][^\W]+)?(?:\.|['\"]\])?[^_=]*?[%}]}/"; |
77
|
21 |
|
$results = []; |
78
|
|
|
|
79
|
21 |
|
preg_match_all($regex, $bodyContent, $results); |
80
|
|
|
|
81
|
21 |
|
return array_unique($results[1]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
7 |
View Code Duplication |
public function getTemplateImportDependencies($bodyContent) |
|
|
|
|
88
|
|
|
{ |
89
|
7 |
|
$regex = "/{%\s?(?:import|from|include)\s?['\"](.+)['\"].+/"; |
90
|
7 |
|
$results = []; |
91
|
|
|
|
92
|
7 |
|
preg_match_all($regex, $bodyContent, $results); |
93
|
|
|
|
94
|
7 |
|
if (empty($results[1])) |
95
|
|
|
{ |
96
|
|
|
return []; |
97
|
|
|
} |
98
|
|
|
|
99
|
7 |
|
return array_unique($results[1]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function hasProfiler() |
106
|
|
|
{ |
107
|
|
|
return $this->profiler !== null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
48 |
|
public function setProfiler($profiler) |
114
|
|
|
{ |
115
|
48 |
|
$this->profiler = $profiler; |
116
|
48 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function getProfilerOutput(Compiler $compiler) |
122
|
|
|
{ |
123
|
|
|
$dumper = new TwigTextProfiler(); |
124
|
|
|
$dumper->setTemplateMappings($compiler->getTemplateMappings()); |
125
|
|
|
|
126
|
|
|
return $dumper->dump($this->profiler); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.