-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: preparing for new version with full Laravel 10 and Livewire sup…
…port
- Loading branch information
Showing
8 changed files
with
106 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
/node_modules | ||
/vendor | ||
.phpunit.result.cache | ||
.phpunit.cache/ | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,15 +16,15 @@ | |
{ | ||
"name": "İzni Burak Demirtaş", | ||
"email": "[email protected]", | ||
"homepage": "https://burakdemirtas.org" | ||
"homepage": "https://buki.dev" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.2.5|^8.0.2" | ||
"php": "^8.1" | ||
}, | ||
"require-dev": { | ||
"illuminate/routing": "^8.0", | ||
"phpunit/phpunit": "^8.5 || ^9.4" | ||
"illuminate/routing": "^10.0", | ||
"phpunit/phpunit": "^10.1" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="vendor/autoload.php" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
verbose="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="Laravel Auto Routes Tests"> | ||
<directory>tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src/</directory> | ||
</whitelist> | ||
</filter> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false"> | ||
<coverage/> | ||
<testsuites> | ||
<testsuite name="Laravel Auto Routes Tests"> | ||
<directory>tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<source> | ||
<include> | ||
<directory suffix=".php">src/</directory> | ||
</include> | ||
</source> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,40 +10,33 @@ | |
use ReflectionClass; | ||
use ReflectionMethod; | ||
|
||
// for Livewire support | ||
use Livewire\{Volt\Volt, Component}; | ||
|
||
/** | ||
* Class AutoRoute | ||
* | ||
* @package Buki\AutoRoute | ||
* @author İzni Burak Demirtaş <info@burakdemirtasorg> | ||
* @author İzni Burak Demirtaş <[email protected]> | ||
* @web https://buki.dev | ||
*/ | ||
class AutoRoute | ||
{ | ||
/** @var Container */ | ||
protected $app; | ||
|
||
/** @var Router */ | ||
protected $router; | ||
protected Router $router; | ||
|
||
/** @var string */ | ||
protected $namespace; | ||
protected string $namespace; | ||
|
||
/** @var string[] Laravel Routing Available Methods. */ | ||
protected $availableMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']; | ||
protected array $availableMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD']; | ||
|
||
/** @var string[] Custom methods for the package */ | ||
protected $customMethods = ['XGET', 'XPOST', 'XPUT', 'XPATCH', 'XDELETE', 'XOPTIONS', 'XANY']; | ||
protected array $customMethods = ['XGET', 'XPOST', 'XPUT', 'XPATCH', 'XDELETE', 'XOPTIONS', 'XANY', 'VOLT', 'WIRE']; | ||
|
||
/** @var string Main Method */ | ||
protected $mainMethod; | ||
protected string $mainMethod; | ||
|
||
/** @var string */ | ||
protected $defaultHttpMethods; | ||
protected string|array $defaultHttpMethods; | ||
|
||
/** @var string Ajax Middleware class for the Requests */ | ||
protected $ajaxMiddleware; | ||
protected string $ajaxMiddleware; | ||
|
||
/** @var string[] */ | ||
protected $defaultPatterns = [ | ||
protected array $defaultPatterns = [ | ||
':any' => '([^/]+)', | ||
':int' => '(\d+)', | ||
':float' => '[+-]?([0-9]*[.])?[0-9]+', | ||
|
@@ -54,9 +47,8 @@ class AutoRoute | |
* AutoRoute constructor. | ||
* @throws | ||
*/ | ||
public function __construct(Container $app) | ||
public function __construct(protected Container $app) | ||
{ | ||
$this->app = $app; | ||
$this->router = $app->get('router'); | ||
} | ||
|
||
|
@@ -103,7 +95,7 @@ function () use ($controller, $only, $except, $patterns) { | |
if (in_array($method->class, [BaseController::class, "{$this->namespace}\\Controller"]) | ||
|| $method->getDeclaringClass()->getParentClass()->getName() === BaseController::class | ||
|| !$method->isPublic() | ||
|| strpos($method->name, '__') === 0) { | ||
|| str_starts_with($method->name, '__')) { | ||
continue; | ||
} | ||
|
||
|
@@ -116,46 +108,72 @@ function () use ($controller, $only, $except, $patterns) { | |
} | ||
|
||
// Find the HTTP method which will be used and method name. | ||
[$httpMethods, $methodName, $middleware] = $this->getHttpMethodAndName($methodName); | ||
[$httpMethods, $path, $middleware] = $this->getHttpMethodAndName($methodName); | ||
|
||
// Get endpoints and parameter patterns for Route | ||
[$endpoints, $routePatterns] = $this->getRouteValues($method, $patterns); | ||
|
||
$endpoints = implode('/', $endpoints); | ||
$this->router->addRoute( | ||
array_map(function ($method) { | ||
return strtoupper($method); | ||
}, $httpMethods), | ||
($methodName !== $this->mainMethod ? $methodName : '') . "/{$endpoints}", | ||
[$classRef->getName(), $method->name] | ||
)->where($routePatterns)->name("{$method->name}")->middleware($middleware); | ||
|
||
$handler = [$classRef->getName(), $method->name]; | ||
$routePath = ($path !== $this->mainMethod ? $path : '') . "/{$endpoints}"; | ||
|
||
// for volt | ||
if (str_starts_with($method->name, 'volt')) { | ||
if (class_exists(Volt::class) && $method->getReturnType()?->getName() === 'string') { | ||
Volt::route($routePath, $method->invoke(new ($classRef->getName()))) | ||
->where($routePatterns)->name("{$method->name}")->middleware($middleware); | ||
} | ||
|
||
continue; | ||
} | ||
|
||
// for livewire | ||
if (str_starts_with($method->name, 'wire')) { | ||
if (!(class_exists(Component::class) && $method->getReturnType()?->getName() === 'string')) { | ||
continue; | ||
} | ||
|
||
$handler = $method->invoke(new ($classRef->getName())); | ||
|
||
if (!is_subclass_of($handler, Component::class)) { | ||
continue; | ||
} | ||
} | ||
|
||
$this->router | ||
->addRoute(array_map(fn ($method) => strtoupper($method), $httpMethods), $routePath, $handler) | ||
->where($routePatterns)->name("{$method->name}")->middleware($middleware); | ||
} | ||
} | ||
); | ||
} | ||
|
||
private function getHttpMethodAndName(string $methodName): array | ||
private function getHttpMethodAndName(string $controllerMethod): array | ||
{ | ||
$httpMethods = $this->defaultHttpMethods; | ||
$middleware = null; | ||
foreach (array_merge($this->availableMethods, $this->customMethods) as $httpMethod) { | ||
$httpMethod = strtolower($httpMethod); | ||
if (stripos($methodName, $httpMethod, 0) === 0) { | ||
if ($httpMethod !== 'xany') { | ||
$httpMethods = [ltrim($httpMethod, 'x')]; | ||
foreach (array_merge($this->availableMethods, $this->customMethods) as $method) { | ||
$method = strtolower($method); | ||
if (stripos($controllerMethod, $method, 0) === 0) { | ||
if (in_array($method, ['volt', 'wire'])) { | ||
$httpMethods = ['GET', 'HEAD']; | ||
} elseif ($method !== 'xany') { | ||
$httpMethods = [ltrim($method, 'x')]; | ||
} | ||
$middleware = strpos($httpMethod, 'x') === 0 ? $this->ajaxMiddleware : null; | ||
$methodName = lcfirst( | ||
preg_replace('/' . $httpMethod . '_?/i', '', $methodName, 1) | ||
|
||
$middleware = str_starts_with($method, 'x') ? $this->ajaxMiddleware : null; | ||
$controllerMethod = lcfirst( | ||
preg_replace('/' . $method . '_?/i', '', $controllerMethod, 1) | ||
); | ||
break; | ||
} | ||
} | ||
|
||
// Convert URL from camelCase to snake-case. | ||
$methodName = strtolower(preg_replace('%([a-z]|[0-9])([A-Z])%', '\1-\2', $methodName)); | ||
$controllerMethod = strtolower(preg_replace('%([a-z]|[0-9])([A-Z])%', '\1-\2', $controllerMethod)); | ||
|
||
return [$httpMethods, $methodName, $middleware]; | ||
return [$httpMethods, $controllerMethod, $middleware]; | ||
} | ||
|
||
private function getRouteValues(ReflectionMethod $method, array $patterns = []): array | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Buki\AutoRoute\Facades; | ||
|
||
/** | ||
* Class Route | ||
* | ||
* @package Buki\AutoRoute | ||
* @author İzni Burak Demirtaş <[email protected]> | ||
* | ||
* @method static void auto(string $prefix, string $controller, array $options = []) | ||
*/ | ||
class Route extends \Illuminate\Support\Facades\Route | ||
{ | ||
} |