1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Copyright (C) 2024 Rafael San José <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU General Public License as published by |
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
8
|
|
|
* any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Alxarafe\Base\Controller\Trait; |
20
|
|
|
|
21
|
|
|
use Alxarafe\Lib\Trans; |
22
|
|
|
use Illuminate\Container\Container; |
23
|
|
|
use Illuminate\Events\Dispatcher; |
24
|
|
|
use Illuminate\Filesystem\Filesystem; |
25
|
|
|
use Illuminate\View\Engines\CompilerEngine; |
26
|
|
|
use Illuminate\View\Engines\EngineResolver; |
27
|
|
|
use Illuminate\View\FileViewFinder; |
28
|
|
|
use Illuminate\View\Factory; |
29
|
|
|
use Illuminate\View\Compilers\BladeCompiler; |
30
|
|
|
|
31
|
|
|
trait ViewTrait |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Contains messages, advices and errors pending to be shown. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
public static array $messages = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Theme name. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
public static string $theme = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Template routes added by modules. |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
public static array $templatesPath = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Contains the name of the blade template to print |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
public string $template; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Contains the title of the view. |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
public string $title; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Contains an array with the messages to be displayed, indicating the |
70
|
|
|
* type (message, advice or error) and the text to be displayed. |
71
|
|
|
* |
72
|
|
|
* @var array |
73
|
|
|
*/ |
74
|
|
|
public array $alerts; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Shows a translated text |
78
|
|
|
* |
79
|
|
|
* @param $message |
80
|
|
|
* @param array $parameters |
81
|
|
|
* @param $locale |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public static function _($message, array $parameters = [], $locale = null): string |
85
|
|
|
{ |
86
|
|
|
return Trans::_($message, $parameters, $locale); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Add a new message (success) to show the user |
91
|
|
|
* |
92
|
|
|
* @param $message |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
public static function addMessage($message): void |
96
|
|
|
{ |
97
|
|
|
self::$messages[]['success'] = $message; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Add a new advice (warning) to show the user |
102
|
|
|
* |
103
|
|
|
* @param $message |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
public static function addAdvice($message): void |
107
|
|
|
{ |
108
|
|
|
self::$messages[]['warning'] = $message; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Add a new error (danger) to show the user |
113
|
|
|
* |
114
|
|
|
* @param $message |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public static function addError($message): void |
118
|
|
|
{ |
119
|
|
|
self::$messages[]['danger'] = $message; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Upon completion of the controller execution, the template is displayed. |
124
|
|
|
*/ |
125
|
|
|
public function __destruct() |
126
|
|
|
{ |
127
|
|
|
if (!isset($this->template)) { |
128
|
|
|
return; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (!isset(self::$theme)) { |
132
|
|
|
self::$theme = 'alxarafe'; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (!isset($this->title)) { |
136
|
|
|
$this->title = 'Alxarafe'; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->alerts = self::getMessages(); |
140
|
|
|
|
141
|
|
|
$vars = ['me' => $this]; |
142
|
|
|
$viewPaths = self::getViewPaths(); |
143
|
|
|
|
144
|
|
|
$cachePaths = realpath(constant('BASE_PATH') . '/..') . '/tmp/blade'; |
145
|
|
|
if (!is_dir($cachePaths) && !mkdir($cachePaths, 0777, true) && !is_dir($cachePaths)) { |
146
|
|
|
die('Could not create cache directory for templates: ' . $cachePaths); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$container = new Container(); |
150
|
|
|
|
151
|
|
|
$container->singleton('files', function () { |
152
|
|
|
return new Filesystem(); |
153
|
|
|
}); |
154
|
|
|
|
155
|
|
|
$container->singleton('view.finder', function ($app) use ($viewPaths) { |
156
|
|
|
return new FileViewFinder($app['files'], $viewPaths); |
157
|
|
|
}); |
158
|
|
|
|
159
|
|
|
$container->singleton('blade.compiler', function ($app) use ($cachePaths) { |
160
|
|
|
return new BladeCompiler($app['files'], $cachePaths); |
161
|
|
|
}); |
162
|
|
|
|
163
|
|
|
$container->singleton('view.engine.resolver', function ($app) { |
164
|
|
|
$resolver = new EngineResolver(); |
165
|
|
|
|
166
|
|
|
// Register Blade engine |
167
|
|
|
$resolver->register('blade', function () use ($app) { |
168
|
|
|
return new CompilerEngine($app['blade.compiler']); |
169
|
|
|
}); |
170
|
|
|
|
171
|
|
|
return $resolver; |
172
|
|
|
}); |
173
|
|
|
|
174
|
|
|
$container->singleton('view', function ($app) { |
175
|
|
|
$resolver = $app['view.engine.resolver']; |
176
|
|
|
$finder = $app['view.finder']; |
177
|
|
|
$dispatcher = new Dispatcher($app); |
178
|
|
|
|
179
|
|
|
return new Factory($resolver, $finder, $dispatcher); |
180
|
|
|
}); |
181
|
|
|
|
182
|
|
|
$viewFactory = $container['view']; |
183
|
|
|
|
184
|
|
|
echo $viewFactory->make($this->template, $vars)->render(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Generates an array with the messages to be displayed, indicating the |
189
|
|
|
* type (message, advice or error) and the text to be displayed. |
190
|
|
|
* |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
|
|
private static function getMessages(): array |
194
|
|
|
{ |
195
|
|
|
$alerts = []; |
196
|
|
|
foreach (self::$messages as $message) { |
197
|
|
|
foreach ($message as $type => $text) { |
198
|
|
|
$alerts[] = [ |
199
|
|
|
'type' => $type, |
200
|
|
|
'text' => $text |
201
|
|
|
]; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
self::$messages = []; |
205
|
|
|
return $alerts; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Returns the routes to the application templates. |
210
|
|
|
* |
211
|
|
|
* @return string[] |
212
|
|
|
*/ |
213
|
|
|
private static function getViewPaths(): array |
214
|
|
|
{ |
215
|
|
|
$viewPaths = [ |
216
|
|
|
constant('APP_PATH') . '/Templates', |
217
|
|
|
constant('APP_PATH') . '/Templates/theme/' . self::$theme, |
218
|
|
|
constant('APP_PATH') . '/Templates/common', |
219
|
|
|
constant('ALX_PATH') . '/Templates', |
220
|
|
|
constant('ALX_PATH') . '/Templates/theme/' . self::$theme, |
221
|
|
|
constant('ALX_PATH') . '/Templates/common', |
222
|
|
|
]; |
223
|
|
|
|
224
|
|
|
if (!empty(self::$templatesPath)) { |
225
|
|
|
$viewPaths = array_merge(self::$templatesPath, $viewPaths); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return $viewPaths; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Sets a new path for the templates, prepending the current selection. |
233
|
|
|
* |
234
|
|
|
* @param array|string $path |
235
|
|
|
* @return void |
236
|
|
|
*/ |
237
|
|
|
public function setTemplatesPath(array|string $path): void |
238
|
|
|
{ |
239
|
|
|
if (is_array($path)) { |
|
|
|
|
240
|
|
|
self::$templatesPath = array_merge($path, self::$templatesPath); |
241
|
|
|
return; |
242
|
|
|
} |
243
|
|
|
array_unshift(self::$templatesPath, $path); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.