1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Controllers\Traits; |
4
|
|
|
|
5
|
|
|
use Nip\Controllers\View\ControllerViewHydrator; |
6
|
|
|
use Nip\Http\Request; |
7
|
|
|
use Nip\View; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trait HasViewTrait |
11
|
|
|
* @package Nip\Controllers\Traits |
12
|
|
|
*/ |
13
|
|
|
trait HasViewTrait |
14
|
|
|
{ |
15
|
|
|
use AbstractControllerTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var View |
19
|
|
|
*/ |
20
|
|
|
protected $view; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $layout = 'default'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param bool $return |
29
|
|
|
* @return bool|string|null |
30
|
|
|
*/ |
31
|
|
|
public function loadView($return = false) |
32
|
|
|
{ |
33
|
|
|
$view = $this->getView(); |
34
|
|
|
$this->populateView($view); |
35
|
|
|
$content = $view->load($this->getLayoutPath()); |
36
|
|
|
if ($return == true) { |
|
|
|
|
37
|
|
|
return $content; |
38
|
|
|
} |
39
|
|
|
echo $content; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return View |
44
|
|
|
*/ |
45
|
|
|
public function getView() |
46
|
|
|
{ |
47
|
|
|
if (!$this->view) { |
48
|
|
|
$this->view = $this->initView(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->view; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param View $view |
56
|
|
|
*/ |
57
|
|
|
public function setView($view) |
58
|
|
|
{ |
59
|
|
|
$this->view = $view; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return View |
64
|
|
|
*/ |
65
|
|
|
protected function initView() |
66
|
|
|
{ |
67
|
|
|
$request = $this->getRequest(); |
68
|
|
|
if ($request instanceof Request) { |
|
|
|
|
69
|
|
|
if (isset($request->_view) && $request->_view instanceof View) { |
70
|
|
|
return $request->_view; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$view = $this->getViewObject(); |
75
|
|
|
|
76
|
|
|
if ($request instanceof Request) { |
|
|
|
|
77
|
|
|
$request->_view = $view; |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $view; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return View |
85
|
|
|
*/ |
86
|
|
|
protected function getViewObject() |
87
|
|
|
{ |
88
|
|
|
return new View(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param View $view |
93
|
|
|
* |
94
|
|
|
* @return View |
95
|
|
|
*/ |
96
|
|
|
public function populateView($view) |
97
|
|
|
{ |
98
|
|
|
$this->populateViewPath($view); |
99
|
|
|
|
100
|
|
|
$view = $this->initViewVars($view); |
101
|
|
|
|
102
|
|
|
// @deprecated Rely on Response Payload Transformer to call this method |
103
|
|
|
// $view = $this->initViewContentBlocks($view); |
104
|
|
|
|
105
|
|
|
return $view; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param View $view |
110
|
|
|
*/ |
111
|
|
|
protected function populateViewPath($view) |
112
|
|
|
{ |
113
|
|
|
return ControllerViewHydrator::populatePath($view, $this); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param View $view |
118
|
|
|
* |
119
|
|
|
* @return View |
120
|
|
|
*/ |
121
|
|
|
protected function initViewVars($view) |
122
|
|
|
{ |
123
|
|
|
return ControllerViewHydrator::initVars($view, $this); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param View $view |
128
|
|
|
* |
129
|
|
|
* @return View |
130
|
|
|
*/ |
131
|
|
|
protected function initViewContentBlocks($view) |
132
|
|
|
{ |
133
|
|
|
return ControllerViewHydrator::initContentBlocks($view, $this); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function getLayoutPath() |
140
|
|
|
{ |
141
|
|
|
return '/layouts/' . $this->getLayout(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function getLayout() |
148
|
|
|
{ |
149
|
|
|
return $this->layout; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $layout |
154
|
|
|
*/ |
155
|
|
|
public function setLayout($layout) |
156
|
|
|
{ |
157
|
|
|
$this->layout = $layout; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.