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
|
12 |
|
public function createTemplate($templateContent) |
47
|
|
|
{ |
48
|
|
|
try |
49
|
|
|
{ |
50
|
12 |
|
$template = $this->twig->createTemplate($templateContent); |
51
|
|
|
} |
52
|
12 |
|
catch (\Twig_Error $e) |
53
|
|
|
{ |
54
|
|
|
throw new TwigError($e); |
55
|
|
|
} |
56
|
|
|
|
57
|
12 |
|
return new TwigTemplate($template); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
21 |
View Code Duplication |
public function getAssortmentDependencies($namespace, $bodyContent) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
// To see what this regex should match and what shouldn't be see: |
66
|
|
|
// tests/allejo/stakx/Test/FrontMatter/FrontMatterDocumentTest.php |
67
|
|
|
|
68
|
21 |
|
$regex = "/{[{%].*?(?:$namespace)(?:\.|\[['\"])?([^_][^\W]+)?(?:\.|['\"]\])?[^_=]*?[%}]}/"; |
69
|
21 |
|
$results = []; |
70
|
|
|
|
71
|
21 |
|
preg_match_all($regex, $bodyContent, $results); |
72
|
|
|
|
73
|
21 |
|
return array_unique($results[1]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
7 |
View Code Duplication |
public function getTemplateImportDependencies($bodyContent) |
|
|
|
|
80
|
|
|
{ |
81
|
7 |
|
$regex = "/{%\s?(?:import|from|include)\s?['\"](.+)['\"].+/"; |
82
|
7 |
|
$results = []; |
83
|
|
|
|
84
|
7 |
|
preg_match_all($regex, $bodyContent, $results); |
85
|
|
|
|
86
|
7 |
|
if (empty($results[1])) |
87
|
7 |
|
{ |
88
|
|
|
return []; |
89
|
|
|
} |
90
|
|
|
|
91
|
7 |
|
return array_unique($results[1]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function hasProfiler() |
98
|
|
|
{ |
99
|
|
|
return $this->profiler !== null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
48 |
|
public function setProfiler($profiler) |
106
|
|
|
{ |
107
|
48 |
|
$this->profiler = $profiler; |
108
|
48 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
|
|
public function getProfilerOutput(Compiler $compiler) |
114
|
|
|
{ |
115
|
|
|
$dumper = new TwigTextProfiler(); |
116
|
|
|
$dumper->setTemplateMappings($compiler->getTemplateMappings()); |
117
|
|
|
|
118
|
|
|
return $dumper->dump($this->profiler); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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.