1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Framework\Http; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Framework\Containers\Modules\DataStructures\Module\Theme; |
19
|
|
|
use O2System\Psr\Patterns\Structural\Repository\AbstractRepository; |
20
|
|
|
use O2System\Spl\DataStructures\SplArrayObject; |
21
|
|
|
use O2System\Spl\Traits\Collectors\ConfigCollectorTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Presenter |
25
|
|
|
* |
26
|
|
|
* @package O2System\Framework\Http |
27
|
|
|
*/ |
28
|
|
|
class Presenter extends AbstractRepository |
29
|
|
|
{ |
30
|
|
|
use ConfigCollectorTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Presenter::$meta |
34
|
|
|
* |
35
|
|
|
* @var Presenter\Meta |
36
|
|
|
*/ |
37
|
|
|
public $meta; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Presenter::$page |
41
|
|
|
* |
42
|
|
|
* @var Presenter\Page |
43
|
|
|
*/ |
44
|
|
|
public $page; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Presenter::$assets |
48
|
|
|
* |
49
|
|
|
* @var Presenter\Assets |
50
|
|
|
*/ |
51
|
|
|
public $assets; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Presenter::$partials |
55
|
|
|
* |
56
|
|
|
* @var Presenter\Repositories\Partials |
57
|
|
|
*/ |
58
|
|
|
public $partials; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Presenter::$widgets |
62
|
|
|
* |
63
|
|
|
* @var Presenter\Repositories\Widgets |
64
|
|
|
*/ |
65
|
|
|
public $widgets; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Presenter::$theme |
69
|
|
|
* |
70
|
|
|
* @var bool|Theme |
71
|
|
|
*/ |
72
|
|
|
public $theme = false; |
73
|
|
|
|
74
|
|
|
// ------------------------------------------------------------------------ |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Presenter::__construct |
78
|
|
|
*/ |
79
|
|
|
public function __construct() |
80
|
|
|
{ |
81
|
|
|
loader()->helper('Url'); |
82
|
|
|
|
83
|
|
|
$this->meta = new Presenter\Meta; |
84
|
|
|
$this->page = new Presenter\Page; |
85
|
|
|
$this->assets = new Presenter\Assets; |
86
|
|
|
$this->partials = new Presenter\Repositories\Partials(); |
87
|
|
|
$this->widgets = new Presenter\Repositories\Widgets(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// ------------------------------------------------------------------------ |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Presenter::initialize |
94
|
|
|
* |
95
|
|
|
* @param array $config |
96
|
|
|
* |
97
|
|
|
* @return static |
98
|
|
|
*/ |
99
|
|
|
public function initialize(array $config = []) |
100
|
|
|
{ |
101
|
|
|
if (count($config)) { |
102
|
|
|
$this->setConfig($config); |
103
|
|
|
} elseif (false !== ($config = config('view')->presenter)) { |
|
|
|
|
104
|
|
|
$this->setConfig($config); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// autoload presenter assets |
108
|
|
|
if (isset($config[ 'assets' ])) { |
109
|
|
|
$this->assets->autoload($config[ 'assets' ]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// autoload presenter theme |
113
|
|
|
if (isset($config[ 'theme' ])) { |
114
|
|
|
if (false !== ($theme = $config[ 'theme' ])) { |
115
|
|
|
$this->setTheme($theme); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// ------------------------------------------------------------------------ |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Presenter::setTheme |
126
|
|
|
* |
127
|
|
|
* @param string $theme |
128
|
|
|
* |
129
|
|
|
* @return static |
130
|
|
|
*/ |
131
|
|
|
public function setTheme($theme) |
132
|
|
|
{ |
133
|
|
|
if (is_bool($theme)) { |
|
|
|
|
134
|
|
|
$this->theme = false; |
135
|
|
|
} elseif (($this->theme = modules()->current()->getTheme($theme)) instanceof Theme) { |
|
|
|
|
136
|
|
|
$pathTheme = str_replace(PATH_RESOURCES, PATH_PUBLIC, $this->theme->getRealPath()); |
137
|
|
|
|
138
|
|
|
if ( ! defined('PATH_THEME')) { |
139
|
|
|
define('PATH_THEME', $pathTheme); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// add theme view directory |
143
|
|
|
view()->addFilePath($this->theme->getRealPath()); |
144
|
|
|
|
145
|
|
|
// add theme output directory |
146
|
|
|
output()->setFileDirName('views'); // replace views folder base on theme structure |
147
|
|
|
output()->addFilePath($this->theme->getRealPath(), 'theme'); |
148
|
|
|
output()->setFileDirName('Views'); // restore Views folder base on PSR-4 folder structure |
149
|
|
|
|
150
|
|
|
// add public theme directory |
151
|
|
|
loader()->addPublicDir($pathTheme, 'theme'); |
152
|
|
|
|
153
|
|
|
// add public theme assets directory |
154
|
|
|
loader()->addPublicDir($pathTheme . 'assets' . DIRECTORY_SEPARATOR, 'themeAssets'); |
155
|
|
|
|
156
|
|
|
// load theme and layout |
157
|
|
|
$this->theme->load(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// ------------------------------------------------------------------------ |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Presenter::store |
167
|
|
|
* |
168
|
|
|
* @param string $offset |
169
|
|
|
* @param mixed $value |
170
|
|
|
* @param bool $replace |
171
|
|
|
*/ |
172
|
|
|
public function store($offset, $value, $replace = false) |
173
|
|
|
{ |
174
|
|
|
if ($value instanceof \Closure) { |
175
|
|
|
parent::store($offset, call_user_func($value, $this)); |
176
|
|
|
} else { |
177
|
|
|
parent::store($offset, $value); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// ------------------------------------------------------------------------ |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Presenter::getArrayCopy |
185
|
|
|
* |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
|
|
public function getArrayCopy() |
189
|
|
|
{ |
190
|
|
|
$storage = $this->storage; |
191
|
|
|
|
192
|
|
|
// Add Properties |
193
|
|
|
$storage[ 'meta' ] = $this->meta; |
194
|
|
|
$storage[ 'page' ] = $this->page; |
195
|
|
|
$storage[ 'assets' ] = new SplArrayObject([ |
196
|
|
|
'head' => $this->assets->getHead(), |
197
|
|
|
'body' => $this->assets->getBody(), |
198
|
|
|
]); |
199
|
|
|
$storage[ 'partials' ] = $this->partials; |
200
|
|
|
$storage[ 'widgets' ] = $this->widgets; |
201
|
|
|
$storage[ 'theme' ] = $this->theme; |
202
|
|
|
|
203
|
|
|
// Add Services |
204
|
|
|
$storage[ 'config' ] = config(); |
205
|
|
|
$storage[ 'language' ] = language(); |
206
|
|
|
$storage[ 'session' ] = session(); |
207
|
|
|
$storage[ 'presenter' ] = presenter(); |
208
|
|
|
$storage[ 'input' ] = input(); |
209
|
|
|
|
210
|
|
|
if (services()->has('csrfProtection')) { |
211
|
|
|
$storage[ 'csrfToken' ] = services()->get('csrfProtection')->getToken(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $storage; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
// ------------------------------------------------------------------------ |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Presenter::get |
221
|
|
|
* |
222
|
|
|
* @param string $property |
223
|
|
|
* |
224
|
|
|
* @return mixed |
225
|
|
|
*/ |
226
|
|
|
public function get($property) |
227
|
|
|
{ |
228
|
|
|
// CodeIgniter property aliasing |
229
|
|
|
if ($property === 'load') { |
230
|
|
|
$property = 'loader'; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
if (services()->has($property)) { |
234
|
|
|
return services()->get($property); |
235
|
|
|
} elseif ($property === 'model') { |
236
|
|
|
return models('controller'); |
237
|
|
|
} elseif ($property === 'services' || $property === 'libraries') { |
238
|
|
|
return services(); |
239
|
|
|
} elseif (method_exists($this, $property)) { |
240
|
|
|
return call_user_func([&$this, $property]); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return parent::get($property); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
// ------------------------------------------------------------------------ |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Presenter::__call |
250
|
|
|
* |
251
|
|
|
* @param string $method |
252
|
|
|
* @param array $args |
253
|
|
|
* |
254
|
|
|
* @return mixed |
255
|
|
|
*/ |
256
|
|
|
public function __call($method, array $args = []) |
257
|
|
|
{ |
258
|
|
|
if (method_exists($this, $method)) { |
259
|
|
|
return call_user_func_array([$this, $method], $args); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|