|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Controllers\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\View; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait HasViewTrait |
|
9
|
|
|
* @package Nip\Controllers\Traits |
|
10
|
|
|
*/ |
|
11
|
|
|
trait HasViewTrait |
|
12
|
|
|
{ |
|
13
|
|
|
use AbstractControllerTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var View |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $view; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $layout = 'default'; |
|
24
|
|
|
|
|
25
|
|
|
public function loadView() |
|
26
|
|
|
{ |
|
27
|
|
|
echo $this->getView()->load($this->getLayoutPath()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return View |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getView() |
|
34
|
|
|
{ |
|
35
|
|
|
if ( ! $this->view) { |
|
36
|
|
|
$this->view = $this->initView(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $this->view; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param View $view |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setView($view) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->view = $view; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return View |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function initView() |
|
54
|
|
|
{ |
|
55
|
|
|
$view = $this->getViewObject(); |
|
56
|
|
|
$view = $this->populateView($view); |
|
57
|
|
|
|
|
58
|
|
|
return $view; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return View |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function getViewObject() |
|
65
|
|
|
{ |
|
66
|
|
|
return new View(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param View $view |
|
71
|
|
|
* |
|
72
|
|
|
* @return View |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function populateView($view) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->populateViewPath($view); |
|
77
|
|
|
|
|
78
|
|
|
$view = $this->initViewVars($view); |
|
79
|
|
|
$view = $this->initViewContentBlocks($view); |
|
80
|
|
|
|
|
81
|
|
|
return $view; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param View $view |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function populateViewPath($view) |
|
88
|
|
|
{ |
|
89
|
|
|
if ( ! defined('MODULES_PATH')) { |
|
90
|
|
|
return; |
|
91
|
|
|
} |
|
92
|
|
|
$path = MODULES_PATH . $this->getRequest()->getModuleName() . '/views/'; |
|
93
|
|
|
if ( ! is_dir($path)) { |
|
94
|
|
|
return; |
|
95
|
|
|
} |
|
96
|
|
|
$view->setBasePath(MODULES_PATH . $this->getRequest()->getModuleName() . '/views/'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param View $view |
|
101
|
|
|
* |
|
102
|
|
|
* @return View |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function initViewVars($view) |
|
105
|
|
|
{ |
|
106
|
|
|
$view->setRequest($this->getRequest()); |
|
107
|
|
|
$view->set('controller', $this->getName()); |
|
|
|
|
|
|
108
|
|
|
$view->set('action', $this->getRequest()->getActionName()); |
|
109
|
|
|
$view->set('options', (isset($this->options) ? $this->options : null)); |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
return $view; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param View $view |
|
116
|
|
|
* |
|
117
|
|
|
* @return View |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function initViewContentBlocks($view) |
|
120
|
|
|
{ |
|
121
|
|
|
$view->setBlock('content', |
|
122
|
|
|
$this->getRequest()->getControllerName() . '/' . $this->getRequest()->getActionName()); |
|
123
|
|
|
|
|
124
|
|
|
return $view; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getLayoutPath() |
|
131
|
|
|
{ |
|
132
|
|
|
return '/layouts/' . $this->getLayout(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getLayout() |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->layout; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param string $layout |
|
145
|
|
|
*/ |
|
146
|
|
|
public function setLayout($layout) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->layout = $layout; |
|
149
|
|
|
} |
|
150
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.