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