Skip to content

Commit f528ea6

Browse files
committed
more Engine simplifications
1 parent 3fe2ca1 commit f528ea6

1 file changed

Lines changed: 35 additions & 54 deletions

File tree

src/Engine.php

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -225,23 +225,17 @@ public function handleException(Throwable $e): void
225225

226226
/**
227227
* Registers the container handler
228-
*
229-
* @param ContainerInterface|callable(class-string<T> $id, array<int|string, mixed> $params): ?T $containerHandler
230-
* Callback function or PSR-11 Container object that sets the container and how it will inject classes
231-
*
232228
* @template T of object
229+
* @param ContainerInterface|callable(class-string<T>, array<int|string, mixed>): ?T $containerHandler
230+
* Callback function or PSR-11 Container object that sets the container and how it will inject classes
233231
*/
234232
public function registerContainerHandler($containerHandler): void
235233
{
236234
$this->dispatcher->setContainerHandler($containerHandler);
237235
}
238236

239237
/**
240-
* Maps a callback to a framework method.
241-
*
242-
* @param string $name Method name
243-
* @param callable $callback Callback function
244-
*
238+
* Maps a callback to a framework method
245239
* @throws Exception If trying to map over a framework method
246240
*/
247241
public function map(string $name, callable $callback): void
@@ -255,21 +249,18 @@ public function map(string $name, callable $callback): void
255249

256250
/**
257251
* Registers a class to a framework method.
258-
*
259252
* # Usage example:
260253
* ```
261254
* $app = new Engine;
262255
* $app->register('user', User::class);
263256
*
264257
* $app->user(); # <- Return a User instance
265258
* ```
266-
*
259+
* @template T of object
267260
* @param string $name Method name
268261
* @param class-string<T> $class Class name
269262
* @param array<int, mixed> $params Class initialization parameters
270-
* @param ?Closure(T $instance): void $callback Function to call after object instantiation
271-
*
272-
* @template T of object
263+
* @param ?callable(T): void $callback Function to call after object instantiation
273264
* @throws Exception If trying to map over a framework method
274265
*/
275266
public function register(string $name, string $class, array $params = [], ?callable $callback = null): void
@@ -281,39 +272,35 @@ public function register(string $name, string $class, array $params = [], ?calla
281272
$this->loader->register($name, $class, $params, $callback);
282273
}
283274

284-
/** Unregisters a class to a framework method. */
275+
/** Unregisters a class to a framework method */
285276
public function unregister(string $methodName): void
286277
{
287278
$this->loader->unregister($methodName);
288279
}
289280

290281
/**
291-
* Adds a pre-filter to a method.
292-
*
282+
* Adds a pre-filter to a method
293283
* @param string $name Method name
294-
* @param Closure(array<int, mixed> &$params, string &$output): (void|false) $callback
284+
* @param callable(array<int, mixed> &$params, string &$output): (void|false) $callback
295285
*/
296286
public function before(string $name, callable $callback): void
297287
{
298-
$this->dispatcher->hook($name, 'before', $callback);
288+
$this->dispatcher->hook($name, Dispatcher::FILTER_BEFORE, $callback);
299289
}
300290

301291
/**
302-
* Adds a post-filter to a method.
303-
*
292+
* Adds a post-filter to a method
304293
* @param string $name Method name
305-
* @param Closure(array<int, mixed> &$params, string &$output): (void|false) $callback
294+
* @param callable(array<int, mixed> &$params, string &$output): (void|false) $callback
306295
*/
307296
public function after(string $name, callable $callback): void
308297
{
309-
$this->dispatcher->hook($name, 'after', $callback);
298+
$this->dispatcher->hook($name, Dispatcher::FILTER_AFTER, $callback);
310299
}
311300

312301
/**
313-
* Gets a variable.
314-
*
302+
* Gets a variable
315303
* @param ?string $key Variable name
316-
*
317304
* @return mixed Variable value or `null` if `$key` doesn't exists.
318305
*/
319306
public function get(?string $key = null)
@@ -326,15 +313,14 @@ public function get(?string $key = null)
326313
}
327314

328315
/**
329-
* Sets a variable.
330-
*
316+
* Sets a variable
331317
* @param string|iterable<string, mixed> $key
332318
* Variable name as `string` or an iterable of `'varName' => $varValue`
333-
* @param ?mixed $value Ignored if `$key` is an `iterable`
319+
* @param mixed $value Ignored if `$key` is an `iterable`
334320
*/
335321
public function set($key, $value = null): void
336322
{
337-
if (\is_iterable($key)) {
323+
if (is_iterable($key)) {
338324
foreach ($key as $k => $v) {
339325
$this->vars[$k] = $v;
340326
}
@@ -346,10 +332,8 @@ public function set($key, $value = null): void
346332
}
347333

348334
/**
349-
* Checks if a variable has been set.
350-
*
335+
* Checks if a variable has been set
351336
* @param string $key Variable name
352-
*
353337
* @return bool Variable status
354338
*/
355339
public function has(string $key): bool
@@ -358,8 +342,7 @@ public function has(string $key): bool
358342
}
359343

360344
/**
361-
* Unsets a variable. If no key is passed in, clear all variables.
362-
*
345+
* Unsets a variable. If no key is passed in, clear all variables
363346
* @param ?string $key Variable name, if `$key` isn't provided, it clear all variables.
364347
*/
365348
public function clear(?string $key = null): void
@@ -373,8 +356,7 @@ public function clear(?string $key = null): void
373356
}
374357

375358
/**
376-
* Processes each routes middleware.
377-
*
359+
* Processes each routes middleware
378360
* @param Route $route The route to process the middleware for.
379361
* @param string $eventName If this is the before or after method.
380362
*/
@@ -386,30 +368,31 @@ protected function processMiddleware(Route $route, string $eventName): bool
386368
$middlewares = $eventName === Dispatcher::FILTER_BEFORE
387369
? $route->middleware
388370
: array_reverse($route->middleware);
371+
389372
$params = $route->params;
390373

391374
foreach ($middlewares as $middleware) {
392375
// Assume that nothing is going to be executed for the middleware.
393376
$middlewareObject = false;
394377

395378
// Closure functions can only run on the before event
396-
if ($eventName === Dispatcher::FILTER_BEFORE && is_object($middleware) === true && ($middleware instanceof Closure)) {
379+
if ($eventName === Dispatcher::FILTER_BEFORE && is_object($middleware) && ($middleware instanceof Closure)) {
397380
$middlewareObject = $middleware;
398381

399382
// If the object has already been created, we can just use it if the event name exists.
400-
} elseif (is_object($middleware) === true) {
401-
$middlewareObject = method_exists($middleware, $eventName) === true ? [$middleware, $eventName] : false;
383+
} elseif (is_object($middleware)) {
384+
$middlewareObject = method_exists($middleware, $eventName) ? [$middleware, $eventName] : false;
402385

403386
// If the middleware is a string, we need to create the object and then call the event.
404-
} elseif (is_string($middleware) === true && method_exists($middleware, $eventName) === true) {
387+
} elseif (is_string($middleware) && method_exists($middleware, $eventName)) {
405388
$resolvedClass = null;
406389

407390
// if there's a container assigned, we should use it to create the object
408-
if ($this->dispatcher->mustUseContainer($middleware) === true) {
391+
if ($this->dispatcher->mustUseContainer($middleware)) {
409392
$resolvedClass = $this->dispatcher->resolveContainerClass($middleware, $params);
410393
// otherwise just assume it's a plain jane class, so inject the engine
411394
// just like in Dispatcher::invokeCallable()
412-
} elseif (class_exists($middleware) === true) {
395+
} elseif (class_exists($middleware)) {
413396
$resolvedClass = new $middleware($this);
414397
}
415398

@@ -420,14 +403,14 @@ protected function processMiddleware(Route $route, string $eventName): bool
420403
}
421404

422405
// If nothing was resolved, go to the next thing
423-
if ($middlewareObject === false) {
406+
if (!$middlewareObject) {
424407
continue;
425408
}
426409

427410
// This is the way that v3 handles output buffering (which captures output correctly)
428411
$useV3OutputBuffering = !$route->is_streamed;
429412

430-
if ($useV3OutputBuffering === true) {
413+
if ($useV3OutputBuffering) {
431414
ob_start();
432415
}
433416

@@ -445,7 +428,7 @@ protected function processMiddleware(Route $route, string $eventName): bool
445428
microtime(true) - $start
446429
);
447430

448-
if ($useV3OutputBuffering === true) {
431+
if ($useV3OutputBuffering) {
449432
$this->response()->write(ob_get_clean());
450433
}
451434

@@ -548,18 +531,16 @@ public function _start(): void
548531

549532
$useV3OutputBuffering = !$route->is_streamed;
550533

551-
if ($useV3OutputBuffering === true) {
534+
if ($useV3OutputBuffering) {
552535
ob_start();
553536
}
554537

555538
// Call route handler
556539
$routeStart = microtime(true);
557-
$continue = $this->dispatcher->execute(
558-
$route->callback,
559-
$params
560-
);
540+
$continue = $this->dispatcher->execute($route->callback, $params);
561541
$this->triggerEvent('flight.route.executed', $route, microtime(true) - $routeStart);
562-
if ($useV3OutputBuffering === true) {
542+
543+
if ($useV3OutputBuffering) {
563544
$response->write(ob_get_clean());
564545
}
565546

@@ -572,6 +553,7 @@ public function _start(): void
572553
$failedMiddlewareCheck = true;
573554
break;
574555
}
556+
575557
$this->triggerEvent('flight.middleware.after', $route);
576558
}
577559

@@ -582,7 +564,6 @@ public function _start(): void
582564
}
583565

584566
$router->next();
585-
586567
$dispatched = false;
587568
}
588569

@@ -596,7 +577,7 @@ public function _start(): void
596577
} elseif ($dispatched === false) {
597578
// Get the previous route and check if the method failed, but the URL was good.
598579
$lastRouteExecuted = $router->executedRoute;
599-
if ($lastRouteExecuted !== null && $lastRouteExecuted->matchUrl($request->url) === true && $lastRouteExecuted->matchMethod($request->method) === false) {
580+
if ($lastRouteExecuted !== null && $lastRouteExecuted->matchUrl($request->url) && !$lastRouteExecuted->matchMethod($request->method)) {
600581
$this->methodNotFound($lastRouteExecuted);
601582
} else {
602583
$this->notFound();

0 commit comments

Comments
 (0)