1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TwigOverride; |
4
|
|
|
|
5
|
|
|
use TwigOverride\Providers\ProviderInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The twig extension that provides the dynamic filtering functionality. |
9
|
|
|
*/ |
10
|
|
|
class TwigOverrideExtension extends \Twig_Extension { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The name of the function to provide for template overrides. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
const TEMPLATE_OVERRIDE_FUNCTION = '_twig_override'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The name of the function to provide for parameter overrides. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
const PARAMETER_OVERRIDE_FUNCTION = '_twig_override_parameters'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A list of override providers to apply. |
28
|
|
|
* |
29
|
|
|
* @var \TwigOverride\Providers\ProviderInterface[] |
30
|
|
|
*/ |
31
|
|
|
protected $providers; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Creates a twig override extension. |
35
|
|
|
* |
36
|
|
|
* @param \TwigOverride\Providers\ProviderInterface[] $providers |
37
|
|
|
* A list of override providers that template names / args will be filtered |
38
|
|
|
* through. |
39
|
|
|
*/ |
40
|
17 |
|
public function __construct(array $providers) { |
41
|
17 |
|
foreach ($providers as $provider) { |
42
|
17 |
|
if (!$provider instanceof ProviderInterface) { |
43
|
1 |
|
throw new \InvalidArgumentException( |
44
|
17 |
|
'One or more providers provided to \TwigOverride\TwigOverrideExtension does not implement \TwigOverride\Providers\ProviderInterface.' |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
} |
48
|
16 |
|
$this->providers = $providers; |
49
|
16 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
8 |
|
public function getFunctions() { |
55
|
|
|
return [ |
56
|
8 |
|
new \Twig_SimpleFunction(static::TEMPLATE_OVERRIDE_FUNCTION, [$this, 'twigOverride']), |
57
|
8 |
|
new \Twig_SimpleFunction(static::PARAMETER_OVERRIDE_FUNCTION, [$this, 'twigOverrideParameters']), |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
8 |
|
public function getNodeVisitors() { |
65
|
|
|
return [ |
66
|
8 |
|
new TwigOverrideNodeVisitor(), |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Overrides one twig template name with another. |
72
|
|
|
* |
73
|
|
|
* @param string $template_name |
74
|
|
|
* The initial template name to be potentially overridden. |
75
|
|
|
* @param bool $only |
76
|
|
|
* Whether or not the only flag is set. |
77
|
|
|
* @param array|null $with |
78
|
|
|
* The variables that were passed in the "with" statement. |
79
|
|
|
* @param array|null $_context |
80
|
|
|
* The current twig _context variable where the template is being requested. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
* The template name after being filtered through the overwrite providers. |
84
|
|
|
*/ |
85
|
15 |
|
public function twigOverride($template_name, $only, array $with = NULL, array $_context = NULL) { |
86
|
15 |
|
$with = isset($with) ? $with : []; |
87
|
15 |
|
$_context = isset($_context) ? $_context : []; |
88
|
15 |
|
foreach ($this->providers as $provider) { |
89
|
15 |
|
$template_name = $provider->rewriteTemplateName($template_name, $with, $_context, $only); |
90
|
|
|
} |
91
|
15 |
|
return $template_name; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Overrides the 'with' arguments passed to a twig template. |
96
|
|
|
* |
97
|
|
|
* @param string $template_name |
98
|
|
|
* The initial template name the arguments are being passed to. |
99
|
|
|
* @param bool $only |
100
|
|
|
* Whether or not the only flag is set. |
101
|
|
|
* @param array|null $with |
102
|
|
|
* The variables that were passed in the "with" statement. |
103
|
|
|
* @param array|null $_context |
104
|
|
|
* The current twig _context variable where the template is being requested. |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
* The 'with' arguments after being filtered through the overwrite |
108
|
|
|
* providers. |
109
|
|
|
*/ |
110
|
13 |
|
public function twigOverrideParameters($template_name, $only, array $with = NULL, array $_context = NULL) { |
111
|
13 |
|
$with = isset($with) ? $with : []; |
112
|
13 |
|
$_context = isset($_context) ? $_context : []; |
113
|
13 |
|
foreach ($this->providers as $provider) { |
114
|
13 |
|
$with = $provider->preprocessTemplateArgs($template_name, $with, $_context, $only); |
115
|
|
|
} |
116
|
13 |
|
return $with; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|