|
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\Adapter\Renderer\Twig; |
|
15
|
|
|
|
|
16
|
|
|
use Twig_Extension; |
|
17
|
|
|
use Twig_SimpleFilter; |
|
18
|
|
|
use Twig_SimpleFunction; |
|
19
|
|
|
use WebHemi\Adapter\DependencyInjection\DependencyInjectionAdapterInterface; |
|
20
|
|
|
use WebHemi\Adapter\Renderer\RendererFilterInterface; |
|
21
|
|
|
use WebHemi\Adapter\Renderer\RendererHelperInterface; |
|
22
|
|
|
use WebHemi\Application\EnvironmentManager; |
|
23
|
|
|
use WebHemi\Config\ConfigInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class TwigExtension |
|
27
|
|
|
* |
|
28
|
|
|
* @codeCoverageIgnore - Test helpers and filters individually. It's only the shipped solution |
|
29
|
|
|
* to add them to the renderer. |
|
30
|
|
|
*/ |
|
31
|
|
|
class TwigExtension extends Twig_Extension |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var DependencyInjectionAdapterInterface */ |
|
34
|
|
|
private $dependencyInjectionAdapter; |
|
35
|
|
|
/** @var ConfigInterface */ |
|
36
|
|
|
private $configuration; |
|
37
|
|
|
/** @var EnvironmentManager */ |
|
38
|
|
|
private $environmentManager; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* WebHemiTwigExtension constructor. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct() |
|
44
|
|
|
{ |
|
45
|
|
|
// Oh, this is a disgusting ugly hack... |
|
46
|
|
|
global $diAdapter; |
|
|
|
|
|
|
47
|
|
|
$this->dependencyInjectionAdapter = $diAdapter; |
|
48
|
|
|
$this->configuration = $diAdapter->get(ConfigInterface::class); |
|
49
|
|
|
$this->environmentManager = $diAdapter->get(EnvironmentManager::class); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Returns extension filters. |
|
54
|
|
|
* |
|
55
|
|
|
* @return array<Twig_SimpleFilter> |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getFilters() |
|
58
|
|
|
{ |
|
59
|
|
|
$filters = []; |
|
60
|
|
|
$filterConfig = $this->getConfig('filter'); |
|
61
|
|
|
|
|
62
|
|
|
foreach ($filterConfig as $className) { |
|
63
|
|
|
/** @var RendererFilterInterface $callable */ |
|
64
|
|
|
$callable = $this->dependencyInjectionAdapter->get($className); |
|
65
|
|
|
$filters[] = new Twig_SimpleFilter($callable::getName(), $callable); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $filters; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns extension functions. |
|
73
|
|
|
* |
|
74
|
|
|
* @return array<Twig_SimpleFunction> |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getFunctions() : array |
|
77
|
|
|
{ |
|
78
|
|
|
$functions = []; |
|
79
|
|
|
$helperConfig = $this->getConfig('helper'); |
|
80
|
|
|
|
|
81
|
|
|
foreach ($helperConfig as $className) { |
|
82
|
|
|
/** @var RendererHelperInterface $callable */ |
|
83
|
|
|
$callable = $this->dependencyInjectionAdapter->get($className); |
|
84
|
|
|
$functions[] = new Twig_SimpleFunction($callable::getName(), $callable); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $functions; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns the renderer config by type. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $type |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
private function getConfig(string $type) : array |
|
97
|
|
|
{ |
|
98
|
|
|
$module = $this->environmentManager->getSelectedModule(); |
|
99
|
|
|
$config = []; |
|
100
|
|
|
|
|
101
|
|
|
if ($this->configuration->has('renderer/Global/'.$type)) { |
|
102
|
|
|
$config = $this->configuration->getData('renderer/Global/' . $type); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if ($this->configuration->has('renderer/'.$module.'/'.$type)) { |
|
106
|
|
|
$config = merge_array_overwrite( |
|
107
|
|
|
$config, |
|
108
|
|
|
$this->configuration->getData('renderer/'.$module.'/'.$type) |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $config; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state