From 41989794da02731c0c678575ee6d847b94c09351 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0zni=20Burak=20Demirta=C5=9F?= Date: Tue, 15 Feb 2022 00:37:28 +0300 Subject: [PATCH] Added Enum binding suppport for Laravel 9. Also added mixed type support for PHP 8. --- README.md | 2 +- composer.json | 5 +-- src/AutoRoute.php | 86 +++++++++++++++-------------------------------- 3 files changed, 32 insertions(+), 61 deletions(-) diff --git a/README.md b/README.md index 6656068..b1eea85 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ [![Latest Unstable Version](https://poser.pugx.org/izniburak/laravel-auto-routes/v/unstable.svg)](https://packagist.org/packages/izniburak/laravel-auto-routes) [![License](https://poser.pugx.org/izniburak/laravel-auto-routes/license.svg)](https://packagist.org/packages/izniburak/laravel-auto-routes) -Automatically Route Generator Package for Laravel. +Automatically Route Generator & Discovery Package for Laravel. ## Features - All HTTP Methods which supported by Laravel diff --git a/composer.json b/composer.json index bc46b08..1a3819b 100644 --- a/composer.json +++ b/composer.json @@ -1,12 +1,13 @@ { "name": "izniburak/laravel-auto-routes", - "description": "Auto Route Generating Package for Laravel.", + "description": "Auto Route Generating (Auto-Discovery) Package for Laravel", "keywords": [ "auto-route", "laravel", "route", "router", - "route-generator" + "route-generator", + "route-discovery" ], "type": "library", "license": "MIT", diff --git a/src/AutoRoute.php b/src/AutoRoute.php index b3f6f92..d933ef4 100644 --- a/src/AutoRoute.php +++ b/src/AutoRoute.php @@ -18,49 +18,31 @@ */ class AutoRoute { - /** - * @var Container - */ + /** @var Container */ protected $app; - /** - * @var Router - */ + /** @var Router */ protected $router; - /** - * @var string - */ + /** @var string */ protected $namespace; - /** - * @var string[] Laravel Routing Available Methods. - */ + /** @var string[] Laravel Routing Available Methods. */ protected $availableMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']; - /** - * @var string[] Custom methods for the package - */ + /** @var string[] Custom methods for the package */ protected $customMethods = ['XGET', 'XPOST', 'XPUT', 'XPATCH', 'XDELETE', 'XOPTIONS', 'XANY']; - /** - * @var string Main Method - */ + /** @var string Main Method */ protected $mainMethod; - /** - * @var string - */ + /** @var string */ protected $defaultHttpMethods; - /** - * @var string Ajax Middleware class for the Requests - */ + /** @var string Ajax Middleware class for the Requests */ protected $ajaxMiddleware; - /** - * @var string[] - */ + /** @var string[] */ protected $defaultPatterns = [ ':any' => '([^/]+)', ':int' => '(\d+)', @@ -70,9 +52,6 @@ class AutoRoute /** * AutoRoute constructor. - * - * @param Container $app - * * @throws */ public function __construct(Container $app) @@ -83,8 +62,6 @@ public function __construct(Container $app) /** * Initialize for the AutoRoute - * - * @param array $config */ public function setConfigurations(array $config): void { @@ -100,11 +77,6 @@ public function setConfigurations(array $config): void } /** - * @param string $prefix - * @param string $controller - * @param array $options - * - * @return void * @throws */ public function auto(string $prefix, string $controller, array $options = []): void @@ -162,11 +134,6 @@ function () use ($controller, $only, $except, $patterns) { ); } - /** - * @param string $methodName - * - * @return array - */ private function getHttpMethodAndName(string $methodName): array { $httpMethods = $this->defaultHttpMethods; @@ -191,12 +158,6 @@ private function getHttpMethodAndName(string $methodName): array return [$httpMethods, $methodName, $middleware]; } - /** - * @param ReflectionMethod $method - * @param array $patterns - * - * @return array - */ private function getRouteValues(ReflectionMethod $method, array $patterns = []): array { $routePatterns = $endpoints = []; @@ -205,11 +166,8 @@ private function getRouteValues(ReflectionMethod $method, array $patterns = []): $paramName = $param->getName(); $typeHint = $param->hasType() ? $param->getType()->getName() : null; - if ($typeHint !== null && class_exists($typeHint)) { - if (!in_array($typeHint, ['int', 'float', 'string', 'bool']) && !in_array($typeHint, $patterns) - && !is_subclass_of($typeHint, Model::class)) { - continue; - } + if (!$this->isValidRouteParam($typeHint)) { + continue; } $routePatterns[$paramName] = $patterns[$paramName] ?? @@ -220,11 +178,6 @@ private function getRouteValues(ReflectionMethod $method, array $patterns = []): return [$endpoints, $routePatterns]; } - /** - * @param string $controller - * - * @return array - */ private function resolveControllerName(string $controller): array { $controller = str_replace(['.', $this->namespace], ['\\', ''], $controller); @@ -233,4 +186,21 @@ private function resolveControllerName(string $controller): array $controller, ]; } + + private function isValidRouteParam(?string $type): bool + { + if (is_null($type) || in_array($type, ['int', 'float', 'string', 'bool', 'mixed'])) { + return true; + } + + if (class_exists($type) && is_subclass_of($type, Model::class)) { + return true; + } + + if (function_exists('enum_exists') && enum_exists($type)) { + return true; + } + + return false; + } }