Skip to content

Commit

Permalink
Added Enum binding suppport for Laravel 9. Also added mixed type supp…
Browse files Browse the repository at this point in the history
…ort for PHP 8.
  • Loading branch information
izniburak committed Feb 14, 2022
1 parent b91d82e commit 4198979
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 61 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
86 changes: 28 additions & 58 deletions src/AutoRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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+)',
Expand All @@ -70,9 +52,6 @@ class AutoRoute

/**
* AutoRoute constructor.
*
* @param Container $app
*
* @throws
*/
public function __construct(Container $app)
Expand All @@ -83,8 +62,6 @@ public function __construct(Container $app)

/**
* Initialize for the AutoRoute
*
* @param array $config
*/
public function setConfigurations(array $config): void
{
Expand All @@ -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
Expand Down Expand Up @@ -162,11 +134,6 @@ function () use ($controller, $only, $except, $patterns) {
);
}

/**
* @param string $methodName
*
* @return array
*/
private function getHttpMethodAndName(string $methodName): array
{
$httpMethods = $this->defaultHttpMethods;
Expand All @@ -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 = [];
Expand All @@ -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] ??
Expand All @@ -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);
Expand All @@ -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;
}
}

0 comments on commit 4198979

Please sign in to comment.