|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jasny\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Jasny\ViewInterface; |
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* View using a template engine |
|
11
|
|
|
*/ |
|
12
|
|
|
trait View |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var ViewInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $viewer; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Get server request |
|
21
|
|
|
* |
|
22
|
|
|
* @return ServerRequestInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract public function getRequest(); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get server request |
|
28
|
|
|
* |
|
29
|
|
|
* @return ResponseInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract public function getResponse(); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get response. set for controller |
|
35
|
|
|
* |
|
36
|
|
|
* @param ResponseInterface $response |
|
37
|
|
|
*/ |
|
38
|
|
|
abstract public function setResponse(ResponseInterface $response); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the template engine abstraction |
|
43
|
|
|
* |
|
44
|
|
|
* @return ViewInterface |
|
45
|
|
|
*/ |
|
46
|
3 |
|
public function getViewer() |
|
47
|
|
|
{ |
|
48
|
3 |
|
if (!isset($this->viewer)) { |
|
49
|
1 |
|
throw new \LogicException("Viewer has not been set"); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
return $this->viewer; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get the template engine abstraction |
|
57
|
|
|
* |
|
58
|
|
|
* @param ViewInterface $viewer |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public function setViewer(ViewInterface $viewer) |
|
61
|
|
|
{ |
|
62
|
2 |
|
$this->viewer = $viewer; |
|
63
|
2 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get path of the view files |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function getViewPath() |
|
72
|
|
|
{ |
|
73
|
1 |
|
return getcwd(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* View rendered template |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $name Template name |
|
80
|
|
|
* @param array $context Template context |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function view($name, array $context = []) |
|
83
|
|
|
{ |
|
84
|
1 |
|
$context += ['current_url' => $this->getRequest()->getUri()]; |
|
85
|
|
|
|
|
86
|
1 |
|
if (method_exists($this, 'flash')) { |
|
87
|
1 |
|
$context += ['flash' => $this->flash()]; |
|
|
|
|
|
|
88
|
1 |
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
$response = $this->getViewer()->render($this->getResponse(), $name, $context); |
|
91
|
|
|
|
|
92
|
1 |
|
$this->setResponse($response); |
|
93
|
1 |
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@returndoc comment to communicate to implementors of these methods what they are expected to return.