1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace Nip\Controllers\Traits; |
||||
6 | |||||
7 | use Nip\Controllers\View\ControllerViewHydrator; |
||||
8 | use Nip\Http\Request; |
||||
9 | use Nip\View; |
||||
10 | |||||
11 | /** |
||||
12 | * Trait HasViewTrait |
||||
13 | * @package Nip\Controllers\Traits |
||||
14 | */ |
||||
15 | trait HasViewTrait |
||||
16 | { |
||||
17 | use AbstractControllerTrait; |
||||
18 | |||||
19 | /** |
||||
20 | * @var View |
||||
21 | */ |
||||
22 | protected $view; |
||||
23 | |||||
24 | /** |
||||
25 | * @var string |
||||
26 | */ |
||||
27 | protected $layout = 'default'; |
||||
28 | |||||
29 | /** |
||||
30 | * @param bool $return |
||||
31 | * @return bool|string|null |
||||
32 | */ |
||||
33 | public function loadView($return = false) |
||||
34 | { |
||||
35 | $view = $this->getView(); |
||||
36 | $this->populateView($view); |
||||
37 | $content = $view->load($this->getLayoutPath()); |
||||
38 | if ($return == true) { |
||||
0 ignored issues
–
show
|
|||||
39 | return $content; |
||||
40 | } |
||||
41 | echo $content; |
||||
42 | return null; |
||||
43 | } |
||||
44 | |||||
45 | /** |
||||
46 | * @return View |
||||
47 | */ |
||||
48 | public function getView() |
||||
49 | { |
||||
50 | if (!$this->view) { |
||||
51 | $this->view = $this->initView(); |
||||
52 | } |
||||
53 | |||||
54 | return $this->view; |
||||
55 | } |
||||
56 | |||||
57 | /** |
||||
58 | * @param View $view |
||||
59 | */ |
||||
60 | public function setView($view) |
||||
61 | { |
||||
62 | $this->view = $view; |
||||
63 | } |
||||
64 | |||||
65 | /** |
||||
66 | * @return View |
||||
67 | */ |
||||
68 | protected function initView() |
||||
69 | { |
||||
70 | $request = $this->getRequest(); |
||||
71 | if ($request instanceof Request) { |
||||
0 ignored issues
–
show
|
|||||
72 | if (isset($request->_view) && $request->_view instanceof View) { |
||||
73 | return $request->_view; |
||||
74 | } |
||||
75 | } |
||||
76 | |||||
77 | $view = $this->getViewObject(); |
||||
78 | |||||
79 | if ($request instanceof Request) { |
||||
0 ignored issues
–
show
|
|||||
80 | $request->_view = $view; |
||||
0 ignored issues
–
show
|
|||||
81 | } |
||||
82 | |||||
83 | return $view; |
||||
84 | } |
||||
85 | |||||
86 | /** |
||||
87 | * @return View |
||||
88 | */ |
||||
89 | protected function getViewObject() |
||||
90 | { |
||||
91 | return new View(); |
||||
92 | } |
||||
93 | |||||
94 | /** |
||||
95 | * @param View $view |
||||
96 | * |
||||
97 | * @return View |
||||
98 | */ |
||||
99 | public function populateView($view) |
||||
100 | { |
||||
101 | $this->populateViewPath($view); |
||||
0 ignored issues
–
show
The function
Nip\Controllers\Traits\H...ait::populateViewPath() has been deprecated: Rely on Response Payload Transformer to call this method. Use RegisterViewPaths if necessary
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||
102 | |||||
103 | $view = $this->initViewVars($view); |
||||
104 | |||||
105 | // @deprecated Rely on Response Payload Transformer to call this method |
||||
106 | // $view = $this->initViewContentBlocks($view); |
||||
107 | |||||
108 | return $view; |
||||
109 | } |
||||
110 | |||||
111 | public function registerViewPaths(View\View $view): void |
||||
0 ignored issues
–
show
The parameter
$view is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
112 | { |
||||
113 | } |
||||
114 | |||||
115 | /** |
||||
116 | * @deprecated Rely on Response Payload Transformer to call this method. Use RegisterViewPaths if necessary |
||||
117 | */ |
||||
118 | protected function populateViewPath($view) |
||||
119 | { |
||||
120 | return ControllerViewHydrator::populatePath($view, $this); |
||||
0 ignored issues
–
show
$this of type Nip\Controllers\Traits\HasViewTrait is incompatible with the type Nip\Controllers\Controller expected by parameter $controller of Nip\Controllers\View\Con...ydrator::populatePath() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
121 | } |
||||
122 | |||||
123 | /** |
||||
124 | * @param View $view |
||||
125 | * |
||||
126 | * @return View |
||||
127 | */ |
||||
128 | protected function initViewVars($view) |
||||
129 | { |
||||
130 | return ControllerViewHydrator::initVars($view, $this); |
||||
0 ignored issues
–
show
$this of type Nip\Controllers\Traits\HasViewTrait is incompatible with the type Nip\Controllers\Controller|null expected by parameter $controller of Nip\Controllers\View\Con...iewHydrator::initVars() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
131 | } |
||||
132 | |||||
133 | /** |
||||
134 | * @param View $view |
||||
135 | * |
||||
136 | * @return View |
||||
137 | */ |
||||
138 | protected function initViewContentBlocks($view) |
||||
139 | { |
||||
140 | return ControllerViewHydrator::initContentBlocks($view, $this); |
||||
141 | } |
||||
142 | |||||
143 | /** |
||||
144 | * @return string |
||||
145 | */ |
||||
146 | public function getLayoutPath() |
||||
147 | { |
||||
148 | return '/layouts/' . $this->getLayout(); |
||||
149 | } |
||||
150 | |||||
151 | /** |
||||
152 | * @return string |
||||
153 | */ |
||||
154 | public function getLayout() |
||||
155 | { |
||||
156 | return $this->layout; |
||||
157 | } |
||||
158 | |||||
159 | /** |
||||
160 | * @param string $layout |
||||
161 | */ |
||||
162 | public function setLayout($layout) |
||||
163 | { |
||||
164 | $this->layout = $layout; |
||||
165 | } |
||||
166 | } |
||||
167 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.