|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright 2018 Vladimir Jimenez |
|
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace allejo\stakx\Templating\Twig; |
|
9
|
|
|
|
|
10
|
|
|
use allejo\stakx\Command\BuildableCommand; |
|
11
|
|
|
use allejo\stakx\MarkupEngine\MarkupEngineManager; |
|
12
|
|
|
use allejo\stakx\Service; |
|
13
|
|
|
use allejo\stakx\Templating\Twig\Extension\AbstractTwigExtension; |
|
14
|
|
|
use allejo\stakx\Templating\Twig\Extension\TwigFilterInterface; |
|
15
|
|
|
use allejo\stakx\Templating\Twig\Extension\TwigFunctionInterface; |
|
16
|
|
|
use allejo\stakx\Templating\Twig\MarkupBlock\TokenParser; |
|
17
|
|
|
|
|
18
|
|
|
class TwigExtension extends \Twig_Extension |
|
19
|
|
|
{ |
|
20
|
|
|
private $filters = []; |
|
21
|
18 |
|
private $functions = []; |
|
22
|
|
|
private $markupManager; |
|
23
|
|
|
|
|
24
|
18 |
|
public function __construct(MarkupEngineManager $manager) |
|
25
|
|
|
{ |
|
26
|
18 |
|
$this->markupManager = $manager; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function __call($name, $arguments) |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
18 |
|
$tag = lcfirst(str_replace('parseMarkup', '', $name)); |
|
32
|
|
|
|
|
33
|
18 |
|
if (!$tag) |
|
34
|
|
|
{ |
|
35
|
|
|
throw new \BadMethodCallException("No method $name found in this class."); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $this->parseMarkup($arguments[0], $tag); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function parseMarkup($content, $tag) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
return $this->markupManager->getEngineByTag($tag)->parse($content); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
View Code Duplication |
public function addFilters(/*iterable*/ $filters) |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
/** @var AbstractTwigExtension|TwigFilterInterface $filter */ |
|
49
|
6 |
|
foreach ($filters as $filter) |
|
50
|
|
|
{ |
|
51
|
6 |
|
if (Service::getParameter(BuildableCommand::SAFE_MODE) && $filter::disableInSafeMode()) |
|
52
|
|
|
{ |
|
53
|
|
|
continue; |
|
54
|
6 |
|
} |
|
55
|
|
|
|
|
56
|
6 |
|
$this->filters[] = $filter::get(); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
30 |
|
|
|
60
|
|
View Code Duplication |
public function addFunctions(/*iterable*/ $functions) |
|
|
|
|
|
|
61
|
30 |
|
{ |
|
62
|
|
|
/** @var AbstractTwigExtension|TwigFunctionInterface $fxn */ |
|
63
|
|
|
foreach ($functions as $fxn) |
|
64
|
|
|
{ |
|
65
|
|
|
if (Service::getParameter(BuildableCommand::SAFE_MODE) && $fxn::disableInSafeMode()) |
|
66
|
|
|
{ |
|
67
|
|
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->functions[] = $fxn::get(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function getFilters() |
|
75
|
|
|
{ |
|
76
|
|
|
$filters = $this->filters; |
|
77
|
|
|
|
|
78
|
|
|
foreach ($this->markupManager->getTemplateTags() as $tag) |
|
79
|
|
|
{ |
|
80
|
|
|
// Since we can't pass what tag/markup language we're using to the callable, let's make the callable to a |
|
81
|
|
|
// non-existent method that will be handled by __call() |
|
82
|
|
|
$filters[] = new \Twig_SimpleFilter( |
|
83
|
|
|
$tag, |
|
84
|
|
|
[$this, 'parseMarkup' . ucfirst($tag)], |
|
85
|
|
|
['is_safe' => ['html']] |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $filters; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getFunctions() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->functions; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getTokenParsers() |
|
98
|
|
|
{ |
|
99
|
|
|
$tokenParsers = []; |
|
100
|
|
|
|
|
101
|
|
|
foreach ($this->markupManager->getTemplateTags() as $tag) |
|
102
|
|
|
{ |
|
103
|
|
|
$tokenParsers[] = new TokenParser($tag); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $tokenParsers; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function getName() |
|
110
|
|
|
{ |
|
111
|
|
|
return 'stakx_twig_base_extension'; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.