1
|
|
|
<?php |
2
|
|
|
namespace Mouf\Html\Renderer\Twig; |
3
|
|
|
|
4
|
|
|
use Interop\Container\ContainerInterface; |
5
|
|
|
use Twig_Extension; |
6
|
|
|
use Mouf\Html\HtmlElement\HtmlElementInterface; |
7
|
|
|
use Mouf\Utils\Value\ValueInterface; |
8
|
|
|
use Mouf\MoufException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The Mouf Twig extension provides a number of functions (toHtml, val, ...) to the Twig template |
12
|
|
|
* |
13
|
|
|
* @author David Negrier <[email protected]> |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class MoufTwigExtension extends Twig_Extension |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
private $container; |
20
|
|
|
|
21
|
|
|
public function __construct(ContainerInterface $container) |
22
|
|
|
{ |
23
|
|
|
$this->container = $container; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* (non-PHPdoc) |
28
|
|
|
* @see Twig_ExtensionInterface::getName() |
29
|
|
|
*/ |
30
|
|
|
public function getName() |
31
|
|
|
{ |
32
|
|
|
return 'mouf'; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getFunctions() |
36
|
|
|
{ |
37
|
|
|
return array( |
38
|
|
|
/** |
39
|
|
|
* The toHtml Twig function takes an HtmlElementInterface |
40
|
|
|
* and calls the toHtml() method on it. |
41
|
|
|
* You can also call it with a string as a parameter. It will fetch the |
42
|
|
|
* instance with that name and call toHtml() method on it. |
43
|
|
|
*/ |
44
|
|
|
new \Twig_SimpleFunction('toHtml', [$this, 'toHtml'], array('is_safe' => array('html'))), |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The mouf Twig function takes an instance name and returns the instance object. |
48
|
|
|
* You would usually use it in conjunction with the toHtml function. |
49
|
|
|
*/ |
50
|
|
|
new \Twig_SimpleFunction('mouf', [$this, 'getInstance']), |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The val function will call the val() method of the object passed in parameter |
54
|
|
|
* (if the object extends the ValueInterface interface). |
55
|
|
|
*/ |
56
|
|
|
new \Twig_SimpleFunction('val', [$this, 'getValue']), |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The t function will call the iMsgNoEdit() method of the string passed in parameter |
60
|
|
|
*/ |
61
|
|
|
new \Twig_SimpleFunction('t', [$this, 'translate'], array('is_variadic' => true, 'deprecated' => true, 'alternative' => '"t" filter')), |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The l function will create a relative URL : in fact, it simply preprends the ROOT_URL |
65
|
|
|
*/ |
66
|
|
|
new \Twig_SimpleFunction('l', [$this, 'createRelativeLink'], array('deprecated' => true)), |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The tourl function will create a link instead of a string |
70
|
|
|
*/ |
71
|
|
|
new \Twig_SimpleFunction('tourl', [$this, 'toUrl'], array('deprecated' => true)), |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The Cookies function will return the $_COOKIE list |
75
|
|
|
*/ |
76
|
|
|
new \Twig_SimpleFunction('cookies', [$this, 'getCookie'], array('deprecated' => true)), |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns a list of filters to add to the existing list. |
82
|
|
|
* |
83
|
|
|
* @return array An array of filters |
84
|
|
|
*/ |
85
|
|
|
public function getFilters() |
86
|
|
|
{ |
87
|
|
|
return array( |
88
|
|
|
new \Twig_SimpleFilter('t', [$this, 'translate'], array('is_variadic' => true)), |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function toHtml($param) |
93
|
|
|
{ |
94
|
|
|
if ($param == null) { |
95
|
|
|
throw new MoufException("Empty parameter passed to the toHtml() function in a Twig template."); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (is_string($param)) { |
99
|
|
|
$param = $this->container->get($param); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (!$param instanceof HtmlElementInterface) { |
103
|
|
|
throw new MoufException("Parameter passed to the toHtml() function in a Twig template must be an object implementing HtmlElementInterface or the name of a Mouf instance implementing HtmlElementInterface."); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
ob_start(); |
107
|
|
|
$param->toHtml(); |
108
|
|
|
|
109
|
|
|
return ob_get_clean(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getInstance($instanceName) |
113
|
|
|
{ |
114
|
|
|
return $this->container->get($instanceName); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getValue($param) |
118
|
|
|
{ |
119
|
|
|
if ($param instanceof ValueInterface) { |
120
|
|
|
return $param->val(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->container->get($param)->val(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function translate($text, array $args = array()) |
127
|
|
|
{ |
128
|
|
|
array_unshift($args, $text); |
129
|
|
|
return call_user_func_array('iMsgNoEdit', $args); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function createRelativeLink($param) |
133
|
|
|
{ |
134
|
|
|
return ROOT_URL.$param; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function toUrl($param) |
138
|
|
|
{ |
139
|
|
|
return preg_replace('/http(s)*:\/\/[0-9a-zA-Z\.\:\/\?\&\#\%-=_]+/', '<a href="${0}" target="_blank">${0}</a>', $param); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function getCookie($key) |
143
|
|
|
{ |
144
|
|
|
if (isset($_COOKIE[$key])) { |
145
|
|
|
return $_COOKIE[$key]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|