|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jasny\Controller\View; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* View using Twig |
|
10
|
|
|
*/ |
|
11
|
|
|
trait Twig |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Twig environment |
|
15
|
|
|
* @var \Twig_Environment |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $twig = null; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Get server request |
|
21
|
|
|
* @return ServerRequestInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract public function getRequest(); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Get server response |
|
27
|
|
|
* @return ResponseInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract public function getResponse(); |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Add a global variable to the view. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $name Variable name |
|
35
|
|
|
* @param mixed $value |
|
36
|
|
|
* @return $this |
|
37
|
|
|
*/ |
|
38
|
|
|
public function setViewVariable($name, $value) |
|
39
|
|
|
{ |
|
40
|
|
|
if (!$name) throw new \InvalidArgumentException("Name should not be empty"); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
$this->getTwig()->addGlobal($name, $value); |
|
43
|
|
|
|
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Expose a function to the view. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $function Variable name |
|
51
|
|
|
* @param mixed $callback |
|
|
|
|
|
|
52
|
|
|
* @param string $as 'function' or 'filter' |
|
53
|
|
|
* @return $this |
|
54
|
|
|
*/ |
|
55
|
|
|
public function setViewFunction($name, $function = null, $as = 'function') |
|
56
|
|
|
{ |
|
57
|
|
|
if ($as === 'function') { |
|
58
|
|
|
$this->getTwig()->addFunction($this->createTwigFunction($name, $function)); |
|
|
|
|
|
|
59
|
|
|
} elseif ($as === 'filter') { |
|
60
|
|
|
$this->getTwig()->addFilter($this->createTwigFilter($name, $function)); |
|
|
|
|
|
|
61
|
|
|
} else { |
|
62
|
|
|
throw new \InvalidArgumentException("You should create either function or filter, not '$as'"); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Add extension to view |
|
70
|
|
|
* |
|
71
|
|
|
* @param object $extension |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setViewExtension($extension) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->getTwig()->addExtension($extension); |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* View rendered template |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $name Template name |
|
85
|
|
|
* @param array $context Template context |
|
86
|
|
|
* @return ResponseInterface |
|
87
|
|
|
*/ |
|
88
|
|
|
public function view($name, array $context = []) |
|
89
|
|
|
{ |
|
90
|
|
|
if (!pathinfo($name, PATHINFO_EXTENSION)) $name .= '.html.twig'; |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
$twig = $this->getTwig(); |
|
93
|
|
|
$tmpl = $twig->loadTemplate($name); |
|
94
|
|
|
|
|
95
|
|
|
$response = $this->getResponse(); |
|
96
|
|
|
$response = $response->withHeader('Content-Type', 'text/html; charset=' . $twig->getCharset()); |
|
97
|
|
|
$response->getBody()->write($tmpl->render($context)); |
|
98
|
|
|
|
|
99
|
|
|
return $response; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get twig environment |
|
104
|
|
|
* |
|
105
|
|
|
* @return \Twig_Environment |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getTwig() |
|
108
|
|
|
{ |
|
109
|
|
|
if ($this->twig) return $this->twig; |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
$loader = $this->getTwigLoader(); |
|
112
|
|
|
$this->twig = $this->getTwigEnvironment($loader); |
|
113
|
|
|
|
|
114
|
|
|
$extensions = ['DateExtension', 'PcreExtension', 'TextExtension', 'ArrayExtension']; |
|
115
|
|
|
foreach ($extensions as $name) { |
|
116
|
|
|
$class = "Jasny\Twig\\$name"; |
|
117
|
|
|
|
|
118
|
|
|
if (class_exists($class)) $this->setViewExtension(new $class()); |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$uri = $this->getRequest()->getUri()->getPath(); |
|
122
|
|
|
$this->setViewVariable('current_url', $uri); |
|
123
|
|
|
|
|
124
|
|
|
return $this->twig; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Get twig loasder for current working directory |
|
129
|
|
|
* |
|
130
|
|
|
* @return \Twig_Loader_Filesystem |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getTwigLoader() |
|
133
|
|
|
{ |
|
134
|
|
|
return new \Twig_Loader_Filesystem(getcwd()); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Get twig environment instance |
|
139
|
|
|
* |
|
140
|
|
|
* @param \Twig_Loader_Filesystem $loader |
|
141
|
|
|
* @return \Twig_Environment |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getTwigEnvironment(\Twig_Loader_Filesystem $loader) |
|
144
|
|
|
{ |
|
145
|
|
|
return new \Twig_Environment($loader); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Create twig function |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $name Name of function in view |
|
152
|
|
|
* @param callable $function |
|
153
|
|
|
* @return \Twig_SimpleFunction |
|
154
|
|
|
*/ |
|
155
|
|
|
public function createTwigFunction($name, $function) |
|
156
|
|
|
{ |
|
157
|
|
|
if (!$name) throw new \InvalidArgumentException("Function name should not be empty"); |
|
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
return new \Twig_SimpleFunction($name, $function ?: $name); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Create twig filter |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $name Name of filter in view |
|
166
|
|
|
* @param callable $function |
|
167
|
|
|
* @return \Twig_SimpleFilter |
|
168
|
|
|
*/ |
|
169
|
|
|
public function createTwigFilter($name, $function) |
|
170
|
|
|
{ |
|
171
|
|
|
if (!$name) throw new \InvalidArgumentException("Filter name should not be empty"); |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
return new \Twig_SimpleFilter($name, $function ?: $name); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
Adding braces to control structures avoids accidental mistakes as your code changes: