From a12d4740065a36a1b14afebf762e5489f1fe1c5d Mon Sep 17 00:00:00 2001 From: n0nag0n Date: Mon, 3 Jun 2024 20:03:02 -0600 Subject: [PATCH] beautify --- flight/Engine.php | 91 ++++++++++---------- flight/Flight.php | 2 +- tests/FlightTest.php | 3 +- tests/groupcompactsyntax/UsersController.php | 6 +- 4 files changed, 52 insertions(+), 50 deletions(-) diff --git a/flight/Engine.php b/flight/Engine.php index 2c06adc..8572b69 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -42,7 +42,7 @@ use flight\net\Route; * Routes a PATCH URL to a callback function. * @method Route delete(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '') * Routes a DELETE URL to a callback function. - * @method void resource(string $pattern, string $controllerClass, array $methods = []) + * @method void resource(string $pattern, string $controllerClass, array $methods = []) * Adds standardized RESTful routes for a controller. * @method Router router() Gets router * @method string getUrl(string $alias) Gets a url from an alias @@ -729,7 +729,7 @@ class Engine return $this->router()->map('DELETE ' . $pattern, $callback, $pass_route, $route_alias); } - /** + /** * Create a resource controller customizing the methods names mapping. * * @param class-string $controllerClass @@ -748,56 +748,57 @@ class Engine // 'GET /@id/edit' => 'edit', // 'PUT /@id' => 'update', // 'DELETE /@id' => 'destroy' - // ]; + // ]; - $defaultMapping = [ + $defaultMapping = [ 'index' => 'GET ', 'create' => 'GET /create', 'store' => 'POST ', 'show' => 'GET /@id', 'edit' => 'GET /@id/edit', 'update' => 'PUT /@id', - 'destroy' =>'DELETE /@id' - ]; - - // Create a custom alias base - $aliasBase = trim(basename($pattern), '/'); - if(isset($options['alias_base']) === true) { - $aliasBase = $options['alias_base']; - } - - // Only use these controller methods - if(isset($options['only']) === true) { - $only = $options['only']; - $defaultMapping = array_filter($defaultMapping, function($key) use ($only) { - return in_array($key, $only, true) === true; - }, ARRAY_FILTER_USE_KEY); - - // Exclude these controller methods - } else if(isset($options['except']) === true) { - $except = $options['except']; - $defaultMapping = array_filter($defaultMapping, function($key) use ($except) { - return in_array($key, $except, true) === false; - }, ARRAY_FILTER_USE_KEY); - } - - // Add group middleware - $middleware = []; - if(isset($options['middleware']) === true) { - $middleware = $options['middleware']; - } - - $this->group( - $pattern, - function (Router $router) use ($controllerClass, $defaultMapping, $aliasBase): void { - foreach ($defaultMapping as $controllerMethod => $methodPattern) { - $router->map( - $methodPattern, - $controllerClass.'->'.$controllerMethod - )->setAlias($aliasBase.'.'.$controllerMethod); - } - } - , $middleware); + 'destroy' => 'DELETE /@id' + ]; + + // Create a custom alias base + $aliasBase = trim(basename($pattern), '/'); + if (isset($options['alias_base']) === true) { + $aliasBase = $options['alias_base']; + } + + // Only use these controller methods + if (isset($options['only']) === true) { + $only = $options['only']; + $defaultMapping = array_filter($defaultMapping, function ($key) use ($only) { + return in_array($key, $only, true) === true; + }, ARRAY_FILTER_USE_KEY); + + // Exclude these controller methods + } elseif (isset($options['except']) === true) { + $except = $options['except']; + $defaultMapping = array_filter($defaultMapping, function ($key) use ($except) { + return in_array($key, $except, true) === false; + }, ARRAY_FILTER_USE_KEY); + } + + // Add group middleware + $middleware = []; + if (isset($options['middleware']) === true) { + $middleware = $options['middleware']; + } + + $this->group( + $pattern, + function (Router $router) use ($controllerClass, $defaultMapping, $aliasBase): void { + foreach ($defaultMapping as $controllerMethod => $methodPattern) { + $router->map( + $methodPattern, + $controllerClass . '->' . $controllerMethod + )->setAlias($aliasBase . '.' . $controllerMethod); + } + }, + $middleware + ); } /** diff --git a/flight/Flight.php b/flight/Flight.php index 70e8bdd..4942b25 100644 --- a/flight/Flight.php +++ b/flight/Flight.php @@ -42,7 +42,7 @@ require_once __DIR__ . '/autoload.php'; * Routes a PATCH URL to a callback function. * @method static Route delete(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '') * Routes a DELETE URL to a callback function. - * @method void resource(string $pattern, string $controllerClass, array $methods = []) + * @method void resource(string $pattern, string $controllerClass, array $methods = []) * Adds standardized RESTful routes for a controller. * @method static Router router() Returns Router instance. * @method static string getUrl(string $alias, array $params = []) Gets a url from an alias diff --git a/tests/FlightTest.php b/tests/FlightTest.php index 5d196d6..cf0c9f0 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -361,8 +361,7 @@ class FlightTest extends TestCase string $output, array $renderParams, string $regexp - ): void - { + ): void { Flight::view()->preserveVars = false; $this->expectOutputString($output); diff --git a/tests/groupcompactsyntax/UsersController.php b/tests/groupcompactsyntax/UsersController.php index 184c102..90e1b68 100644 --- a/tests/groupcompactsyntax/UsersController.php +++ b/tests/groupcompactsyntax/UsersController.php @@ -2,13 +2,15 @@ namespace tests\groupcompactsyntax; -final class UsersController { +final class UsersController +{ public function list(): void { echo __METHOD__; } - public function handleRegister(): void { + public function handleRegister(): void + { echo __METHOD__; } }