|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
declare(strict_types = 1); |
|
13
|
|
|
|
|
14
|
|
|
namespace WebHemi\Renderer\ServiceAdapter\Twig; |
|
15
|
|
|
|
|
16
|
|
|
use RuntimeException; |
|
17
|
|
|
use Twig_Extension; |
|
18
|
|
|
use Twig_SimpleFilter; |
|
19
|
|
|
use Twig_SimpleFunction; |
|
20
|
|
|
use WebHemi\Configuration\ServiceInterface as ConfigurationInterface; |
|
21
|
|
|
use WebHemi\DependencyInjection\ServiceInterface as DependencyInjectionInterface; |
|
22
|
|
|
use WebHemi\Environment\ServiceInterface as EnvironmentInterface; |
|
23
|
|
|
use WebHemi\Renderer\FilterInterface; |
|
24
|
|
|
use WebHemi\Renderer\HelperInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class TwigExtension |
|
28
|
|
|
* |
|
29
|
|
|
* @codeCoverageIgnore - Test helpers and filters individually. It's only the shipped solution |
|
30
|
|
|
* to add them to the renderer. |
|
31
|
|
|
*/ |
|
32
|
|
|
class TwigExtension extends Twig_Extension |
|
33
|
|
|
{ |
|
34
|
|
|
/** @var DependencyInjectionInterface */ |
|
35
|
|
|
private $dependencyInjectionAdapter; |
|
36
|
|
|
/** @var ConfigurationInterface */ |
|
37
|
|
|
private $configuration; |
|
38
|
|
|
/** @var EnvironmentInterface */ |
|
39
|
|
|
private $environmentManager; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* TwigExtension constructor. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct() |
|
45
|
|
|
{ |
|
46
|
|
|
// Oh, this is a disgusting ugly hack... |
|
47
|
|
|
global $dependencyInjection; |
|
48
|
|
|
$this->dependencyInjectionAdapter = $dependencyInjection; |
|
49
|
|
|
$this->configuration = $this->dependencyInjectionAdapter->get(ConfigurationInterface::class); |
|
50
|
|
|
$this->environmentManager = $this->dependencyInjectionAdapter->get(EnvironmentInterface::class); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns extension filters. |
|
55
|
|
|
* |
|
56
|
|
|
* @return array<Twig_SimpleFilter> |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getFilters() |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->getExtensions('filter'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Returns extension functions. |
|
65
|
|
|
* |
|
66
|
|
|
* @return array<Twig_SimpleFunction> |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getFunctions() : array |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->getExtensions('helper'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Gets the specific type of extension |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $type Must be `filter` or `helper` |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
|
|
private function getExtensions(string $type) : array |
|
80
|
|
|
{ |
|
81
|
|
|
$extensions = []; |
|
82
|
|
|
$extensionConfig = $this->getConfig($type); |
|
83
|
|
|
|
|
84
|
|
|
foreach ($extensionConfig as $className) { |
|
85
|
|
|
$callable = $this->dependencyInjectionAdapter->get($className); |
|
86
|
|
|
$this->checkExtensionType($type, $callable); |
|
87
|
|
|
|
|
88
|
|
|
if ($type == 'helper') { |
|
89
|
|
|
$extensions[] = new Twig_SimpleFunction($callable::getName(), $callable); |
|
90
|
|
|
continue; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$extensions[] = new Twig_SimpleFilter($callable::getName(), $callable); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $extensions; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Checks whether the extension has the valid type. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $type |
|
103
|
|
|
* @param object $callable |
|
104
|
|
|
* @throws RuntimeException |
|
105
|
|
|
*/ |
|
106
|
|
|
private function checkExtensionType($type, $callable) : void |
|
107
|
|
|
{ |
|
108
|
|
|
if (($type == 'helper' && $callable instanceof HelperInterface) |
|
109
|
|
|
|| ($type == 'filter' && $callable instanceof FilterInterface) |
|
110
|
|
|
) { |
|
111
|
|
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
throw new RuntimeException( |
|
115
|
|
|
sprintf( |
|
116
|
|
|
'The class %s cannot be registered as Renderer/'.ucfirst($type).'!', |
|
117
|
|
|
get_class($callable) |
|
118
|
|
|
), |
|
119
|
|
|
1000 |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns the renderer config by type. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $type |
|
127
|
|
|
* @return array |
|
128
|
|
|
*/ |
|
129
|
|
|
private function getConfig(string $type) : array |
|
130
|
|
|
{ |
|
131
|
|
|
$module = $this->environmentManager->getSelectedModule(); |
|
132
|
|
|
$config = []; |
|
133
|
|
|
|
|
134
|
|
|
if ($this->configuration->has('renderer/Global/'.$type)) { |
|
135
|
|
|
$config = $this->configuration->getData('renderer/Global/'.$type); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
if ($this->configuration->has('renderer/'.$module.'/'.$type)) { |
|
139
|
|
|
$config = merge_array_overwrite( |
|
140
|
|
|
$config, |
|
141
|
|
|
$this->configuration->getData('renderer/'.$module.'/'.$type) |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $config; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|