-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathController.php
274 lines (242 loc) · 8.6 KB
/
Controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
declare(strict_types=1);
namespace MAKS\Velox\Backend;
use MAKS\Velox\App;
use MAKS\Velox\Backend\Exception;
use MAKS\Velox\Backend\Event;
use MAKS\Velox\Backend\Config;
use MAKS\Velox\Backend\Router;
use MAKS\Velox\Backend\Globals;
use MAKS\Velox\Backend\Session;
use MAKS\Velox\Backend\Database;
use MAKS\Velox\Backend\Auth;
use MAKS\Velox\Backend\Model;
use MAKS\Velox\Frontend\Data;
use MAKS\Velox\Frontend\View;
use MAKS\Velox\Frontend\HTML;
use MAKS\Velox\Frontend\Path;
use MAKS\Velox\Helper\Dumper;
use MAKS\Velox\Helper\Misc;
/**
* An abstract class that serves as a base Controller that can be extended to make handlers for application router.
*
* Example:
* ```
* // create a controller (alternatively, you can create it as a normal class in "/app/Controller/")
* $additionalVars = [1, 2, 3];
* $controller = new class($additionalVars) extends Controller {
* public function someAction(string $path, ?string $match, $previous) {
* $this->data->set('page.title', 'Some Page');
* $someVar = $this->config->get('filename.someVar');
* return $this->view->render('some-page', $this->vars);
* }
* };
*
* // use the created action as a handler for a route
* Router::handle('/some-route', [$controller, 'someAction'], ['GET', 'POST']);
* ```
*
* @package Velox\Backend
* @since 1.0.0
* @api
*
* @property Event $event Instance of the `Event` class.
* @property Config $config Instance of the `Config` class.
* @property Router $router Instance of the `Router` class.
* @property Globals $globals Instance of the `Globals` class.
* @property Session $session Instance of the `Session` class.
* @property Database $database Instance of the `Database` class.
* @property Auth $auth Instance of the `Auth` class.
* @property Data $data Instance of the `Data` class.
* @property View $view Instance of the `View` class.
* @property HTML $html Instance of the `HTML` class.
* @property Path $path Instance of the `Path` class.
* @property Dumper $dumper Instance of the `Dumper` class.
* @property Misc $misc Instance of the `Misc` class.
*/
abstract class Controller
{
/**
* This event will be dispatched when a controller (or a subclass) is constructed.
* This event will not be passed any arguments, but its listener callback will be bound to the object (the controller class).
*
* @var string
*/
public const ON_CONSTRUCT = 'controller.on.construct';
/**
* Preconfigured CRUD routes.
*
* @since 1.3.0
*/
private array $crudRoutes = [
'index' => [
'expression' => '/{controller}',
'method' => 'GET',
],
'create' => [
'expression' => '/{controller}/create',
'method' => 'GET',
],
'store' => [
'expression' => '/{controller}',
'method' => 'POST',
],
'show' => [
'expression' => '/{controller}/([1-9][0-9]*)',
'method' => 'GET',
],
'edit' => [
'expression' => '/{controller}/([1-9][0-9]*)/edit',
'method' => 'GET',
],
'update' => [
'expression' => '/{controller}/([1-9][0-9]*)',
'method' => ['PUT', 'PATCH'],
],
'destroy' => [
'expression' => '/{controller}/([1-9][0-9]*)',
'method' => 'DELETE',
],
];
/**
* The passed variables array to the Controller.
*/
protected array $vars;
protected ?Model $model;
/**
* Class constructor.
*
* @param array $vars [optional] Additional variables to pass to the controller.
*/
public function __construct(array $vars = [])
{
$this->vars = $vars;
$this->model = null;
if ($this->associateModel()) {
$this->doAssociateModel();
}
if ($this->registerRoutes()) {
$this->doRegisterRoutes();
}
Event::dispatch(self::ON_CONSTRUCT, null, $this);
}
public function __get(string $property)
{
if (isset(App::instance()->{$property})) {
return App::instance()->{$property};
}
Exception::throw(
'UndefinedPropertyException:OutOfBoundsException',
sprintf('Call to undefined property %s::$%s', static::class, $property),
);
}
public function __isset(string $property)
{
return isset(App::instance()->{$property});
}
/**
* Controls which model should be used by current controller.
*
* This method should return a concrete class FQN of a model that extends `Model::class`.
*
* This method returns `null` by default.
*
* NOTE: If the model class does not exist, the controller will ignore it silently.
*
* @return string
*
* @since 1.3.0
*/
protected function associateModel(): ?string
{
return null; // @codeCoverageIgnore
}
/**
* Whether or not to automatically register controller routes.
*
* NOTE: The controller class has to be instantiated at least once for this to work.
*
* Only public methods suffixed with the word `Action` or `Middleware` will be registered.
* The suffix will determine the route type (`*Action` => `handler`, `*Middleware` => `middleware`).
* The route will look like `/controller-name/method-name` (names will be converted to slugs).
* The method will be `GET` by default. See also `self::$crudRoutes`.
* You can use the `@route` annotation to overrides the default Route and Method.
* The `@route` annotation can be used in DocBlock on a class method with the following syntax:
* - Pattern: `@route("<path>", {<http-verb>, ...})`
* - Example: `@route("/some-route", {GET, POST})`
*
* This method returns `false` by default.
*
* @return bool
*
* @since 1.3.0
*/
protected function registerRoutes(): bool
{
return false; // @codeCoverageIgnore
}
/**
* Associates a model class to the controller.
*
* @return void
*
* @since 1.3.0
*/
private function doAssociateModel(): void
{
$model = $this->associateModel();
// to prevent \ReflectionClass from throwing an exception
$model = class_exists($model) ? $model : Model::class;
$reflection = new \ReflectionClass($model);
if ($reflection->isSubclassOf(Model::class) && !$reflection->isAbstract()) {
$this->model = $reflection->newInstance();
}
}
/**
* Registers all public methods which are suffixed with `Action` or `Middleware` as `handler` or `middleware` respectively.
*
* @return void
*
* @since 1.3.0
*/
private function doRegisterRoutes(): void
{
$class = new \ReflectionClass($this);
$methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
foreach ($methods as $method) {
$className = $class->getShortName();
$methodName = $method->getName();
$docBlock = $method->getDocComment() ?: '';
if (
$method->isAbstract() ||
$method->isStatic() ||
preg_match('/(Action|Middleware)$/', $methodName) === 0
) {
continue;
}
$controller = Misc::transform(str_replace('Controller', '', $className), 'kebab', 'slug');
$handler = Misc::transform(str_replace(['Action', 'Middleware'], '', $methodName), 'kebab', 'slug');
$routes = $this->crudRoutes;
if (!in_array($handler, array_keys($routes))) {
Misc::setArrayValueByKey(
$routes,
$handler . '.expression',
sprintf('/%s/%s', $controller, $handler)
);
}
if (preg_match('/(@route[ ]*\(["\'](.+)["\']([ ,]*\{(.+)\})?\))/', $docBlock, $matches)) {
$routeExpression = $matches[2] ?? '';
$routeMethod = $matches[4] ?? '';
$routeMethod = array_filter(array_map('trim', explode(',', $routeMethod)));
$routes[$handler] = [
'expression' => $routeExpression,
'method' => $routeMethod,
];
}
$function = preg_match('/(Middleware)$/', $methodName) ? 'middleware' : 'handle';
$expression = Misc::interpolate($routes[$handler]['expression'], ['controller' => $controller]);
$method = $routes[$handler]['method'] ?? 'GET';
$this->router->{$function}($expression, [$this, $methodName], $method);
}
}
}