Skip to content

Commit 4198979

Browse files
committed
Added Enum binding suppport for Laravel 9. Also added mixed type support for PHP 8.
1 parent b91d82e commit 4198979

File tree

3 files changed

+32
-61
lines changed

3 files changed

+32
-61
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
[![Latest Unstable Version](https://poser.pugx.org/izniburak/laravel-auto-routes/v/unstable.svg)](https://packagist.org/packages/izniburak/laravel-auto-routes)
1313
[![License](https://poser.pugx.org/izniburak/laravel-auto-routes/license.svg)](https://packagist.org/packages/izniburak/laravel-auto-routes)
1414

15-
Automatically Route Generator Package for Laravel.
15+
Automatically Route Generator & Discovery Package for Laravel.
1616

1717
## Features
1818
- All HTTP Methods which supported by Laravel

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "izniburak/laravel-auto-routes",
3-
"description": "Auto Route Generating Package for Laravel.",
3+
"description": "Auto Route Generating (Auto-Discovery) Package for Laravel",
44
"keywords": [
55
"auto-route",
66
"laravel",
77
"route",
88
"router",
9-
"route-generator"
9+
"route-generator",
10+
"route-discovery"
1011
],
1112
"type": "library",
1213
"license": "MIT",

src/AutoRoute.php

Lines changed: 28 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,31 @@
1818
*/
1919
class AutoRoute
2020
{
21-
/**
22-
* @var Container
23-
*/
21+
/** @var Container */
2422
protected $app;
2523

26-
/**
27-
* @var Router
28-
*/
24+
/** @var Router */
2925
protected $router;
3026

31-
/**
32-
* @var string
33-
*/
27+
/** @var string */
3428
protected $namespace;
3529

36-
/**
37-
* @var string[] Laravel Routing Available Methods.
38-
*/
30+
/** @var string[] Laravel Routing Available Methods. */
3931
protected $availableMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'];
4032

41-
/**
42-
* @var string[] Custom methods for the package
43-
*/
33+
/** @var string[] Custom methods for the package */
4434
protected $customMethods = ['XGET', 'XPOST', 'XPUT', 'XPATCH', 'XDELETE', 'XOPTIONS', 'XANY'];
4535

46-
/**
47-
* @var string Main Method
48-
*/
36+
/** @var string Main Method */
4937
protected $mainMethod;
5038

51-
/**
52-
* @var string
53-
*/
39+
/** @var string */
5440
protected $defaultHttpMethods;
5541

56-
/**
57-
* @var string Ajax Middleware class for the Requests
58-
*/
42+
/** @var string Ajax Middleware class for the Requests */
5943
protected $ajaxMiddleware;
6044

61-
/**
62-
* @var string[]
63-
*/
45+
/** @var string[] */
6446
protected $defaultPatterns = [
6547
':any' => '([^/]+)',
6648
':int' => '(\d+)',
@@ -70,9 +52,6 @@ class AutoRoute
7052

7153
/**
7254
* AutoRoute constructor.
73-
*
74-
* @param Container $app
75-
*
7655
* @throws
7756
*/
7857
public function __construct(Container $app)
@@ -83,8 +62,6 @@ public function __construct(Container $app)
8362

8463
/**
8564
* Initialize for the AutoRoute
86-
*
87-
* @param array $config
8865
*/
8966
public function setConfigurations(array $config): void
9067
{
@@ -100,11 +77,6 @@ public function setConfigurations(array $config): void
10077
}
10178

10279
/**
103-
* @param string $prefix
104-
* @param string $controller
105-
* @param array $options
106-
*
107-
* @return void
10880
* @throws
10981
*/
11082
public function auto(string $prefix, string $controller, array $options = []): void
@@ -162,11 +134,6 @@ function () use ($controller, $only, $except, $patterns) {
162134
);
163135
}
164136

165-
/**
166-
* @param string $methodName
167-
*
168-
* @return array
169-
*/
170137
private function getHttpMethodAndName(string $methodName): array
171138
{
172139
$httpMethods = $this->defaultHttpMethods;
@@ -191,12 +158,6 @@ private function getHttpMethodAndName(string $methodName): array
191158
return [$httpMethods, $methodName, $middleware];
192159
}
193160

194-
/**
195-
* @param ReflectionMethod $method
196-
* @param array $patterns
197-
*
198-
* @return array
199-
*/
200161
private function getRouteValues(ReflectionMethod $method, array $patterns = []): array
201162
{
202163
$routePatterns = $endpoints = [];
@@ -205,11 +166,8 @@ private function getRouteValues(ReflectionMethod $method, array $patterns = []):
205166
$paramName = $param->getName();
206167
$typeHint = $param->hasType() ? $param->getType()->getName() : null;
207168

208-
if ($typeHint !== null && class_exists($typeHint)) {
209-
if (!in_array($typeHint, ['int', 'float', 'string', 'bool']) && !in_array($typeHint, $patterns)
210-
&& !is_subclass_of($typeHint, Model::class)) {
211-
continue;
212-
}
169+
if (!$this->isValidRouteParam($typeHint)) {
170+
continue;
213171
}
214172

215173
$routePatterns[$paramName] = $patterns[$paramName] ??
@@ -220,11 +178,6 @@ private function getRouteValues(ReflectionMethod $method, array $patterns = []):
220178
return [$endpoints, $routePatterns];
221179
}
222180

223-
/**
224-
* @param string $controller
225-
*
226-
* @return array
227-
*/
228181
private function resolveControllerName(string $controller): array
229182
{
230183
$controller = str_replace(['.', $this->namespace], ['\\', ''], $controller);
@@ -233,4 +186,21 @@ private function resolveControllerName(string $controller): array
233186
$controller,
234187
];
235188
}
189+
190+
private function isValidRouteParam(?string $type): bool
191+
{
192+
if (is_null($type) || in_array($type, ['int', 'float', 'string', 'bool', 'mixed'])) {
193+
return true;
194+
}
195+
196+
if (class_exists($type) && is_subclass_of($type, Model::class)) {
197+
return true;
198+
}
199+
200+
if (function_exists('enum_exists') && enum_exists($type)) {
201+
return true;
202+
}
203+
204+
return false;
205+
}
236206
}

0 commit comments

Comments
 (0)