1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace allejo\stakx\Templating; |
4
|
|
|
|
5
|
|
|
use allejo\stakx\Compiler; |
6
|
|
|
use allejo\stakx\Twig\StakxTwigTextProfiler; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
|
9
|
|
|
class TwigStakxBridge implements TemplateBridgeInterface |
10
|
|
|
{ |
11
|
|
|
private $twig; |
12
|
|
|
private $logger; |
13
|
|
|
|
14
|
|
|
/** @var \Twig_Profiler_Profile */ |
15
|
|
|
private $profiler; |
16
|
|
|
|
17
|
40 |
|
public function __construct(\Twig_Environment $twig) |
18
|
|
|
{ |
19
|
40 |
|
$this->twig = $twig; |
20
|
40 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function setLogger(LoggerInterface $logger) |
26
|
|
|
{ |
27
|
|
|
$this->logger = $logger; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
12 |
|
public function setGlobalVariable($key, $value) |
34
|
|
|
{ |
35
|
12 |
|
$this->twig->addGlobal($key, $value); |
36
|
12 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
12 |
|
public function createTemplate($templateContent) |
42
|
|
|
{ |
43
|
|
|
try |
44
|
|
|
{ |
45
|
12 |
|
$template = $this->twig->createTemplate($templateContent); |
46
|
|
|
} |
47
|
12 |
|
catch (\Twig_Error $e) |
48
|
|
|
{ |
49
|
|
|
throw new TwigError($e); |
50
|
|
|
} |
51
|
|
|
|
52
|
12 |
|
return (new TwigTemplate($template)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
21 |
View Code Duplication |
public function getAssortmentDependencies($namespace, $bodyContent) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
// To see what this regex should match and what shouldn't be see: |
61
|
|
|
// tests/allejo/stakx/Test/FrontMatter/FrontMatterDocumentTest.php |
62
|
|
|
|
63
|
21 |
|
$regex = "/{[{%].*?(?:$namespace)(?:\.|\[['\"])?([^_][^\W]+)?(?:\.|['\"]\])?[^_=]*?[%}]}/"; |
64
|
21 |
|
$results = array(); |
65
|
|
|
|
66
|
21 |
|
preg_match_all($regex, $bodyContent, $results); |
67
|
|
|
|
68
|
21 |
|
return array_unique($results[1]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
7 |
View Code Duplication |
public function getTemplateImportDependencies($bodyContent) |
|
|
|
|
75
|
|
|
{ |
76
|
7 |
|
$regex = "/{%\s?(?:import|from|include)\s?['\"](.+)['\"].+/"; |
77
|
7 |
|
$results = array(); |
78
|
|
|
|
79
|
7 |
|
preg_match_all($regex, $bodyContent, $results); |
80
|
|
|
|
81
|
7 |
|
if (empty($results[1])) |
82
|
7 |
|
{ |
83
|
|
|
return []; |
84
|
|
|
} |
85
|
|
|
|
86
|
7 |
|
return array_unique($results[1]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function hasProfiler() |
93
|
|
|
{ |
94
|
|
|
return ($this->profiler !== null); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
12 |
|
public function setProfiler($profiler) |
101
|
|
|
{ |
102
|
12 |
|
$this->profiler = $profiler; |
103
|
12 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function getProfilerOutput(Compiler $compiler) |
109
|
|
|
{ |
110
|
|
|
$dumper = new StakxTwigTextProfiler(); |
111
|
|
|
$dumper->setTemplateMappings($compiler->getTemplateMappings()); |
112
|
|
|
|
113
|
|
|
return $dumper->dump($this->profiler); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.