pull/596/head
n0nag0n 8 months ago
parent 7127c3ba17
commit a12d474006

@ -42,7 +42,7 @@ use flight\net\Route;
* Routes a PATCH URL to a callback function. * Routes a PATCH URL to a callback function.
* @method Route delete(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '') * @method Route delete(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '')
* Routes a DELETE URL to a callback function. * 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. * Adds standardized RESTful routes for a controller.
* @method Router router() Gets router * @method Router router() Gets router
* @method string getUrl(string $alias) Gets a url from an alias * @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); return $this->router()->map('DELETE ' . $pattern, $callback, $pass_route, $route_alias);
} }
/** /**
* Create a resource controller customizing the methods names mapping. * Create a resource controller customizing the methods names mapping.
* *
* @param class-string $controllerClass * @param class-string $controllerClass
@ -748,56 +748,57 @@ class Engine
// 'GET /@id/edit' => 'edit', // 'GET /@id/edit' => 'edit',
// 'PUT /@id' => 'update', // 'PUT /@id' => 'update',
// 'DELETE /@id' => 'destroy' // 'DELETE /@id' => 'destroy'
// ]; // ];
$defaultMapping = [ $defaultMapping = [
'index' => 'GET ', 'index' => 'GET ',
'create' => 'GET /create', 'create' => 'GET /create',
'store' => 'POST ', 'store' => 'POST ',
'show' => 'GET /@id', 'show' => 'GET /@id',
'edit' => 'GET /@id/edit', 'edit' => 'GET /@id/edit',
'update' => 'PUT /@id', 'update' => 'PUT /@id',
'destroy' =>'DELETE /@id' 'destroy' => 'DELETE /@id'
]; ];
// Create a custom alias base // Create a custom alias base
$aliasBase = trim(basename($pattern), '/'); $aliasBase = trim(basename($pattern), '/');
if(isset($options['alias_base']) === true) { if (isset($options['alias_base']) === true) {
$aliasBase = $options['alias_base']; $aliasBase = $options['alias_base'];
} }
// Only use these controller methods // Only use these controller methods
if(isset($options['only']) === true) { if (isset($options['only']) === true) {
$only = $options['only']; $only = $options['only'];
$defaultMapping = array_filter($defaultMapping, function($key) use ($only) { $defaultMapping = array_filter($defaultMapping, function ($key) use ($only) {
return in_array($key, $only, true) === true; return in_array($key, $only, true) === true;
}, ARRAY_FILTER_USE_KEY); }, ARRAY_FILTER_USE_KEY);
// Exclude these controller methods // Exclude these controller methods
} else if(isset($options['except']) === true) { } elseif (isset($options['except']) === true) {
$except = $options['except']; $except = $options['except'];
$defaultMapping = array_filter($defaultMapping, function($key) use ($except) { $defaultMapping = array_filter($defaultMapping, function ($key) use ($except) {
return in_array($key, $except, true) === false; return in_array($key, $except, true) === false;
}, ARRAY_FILTER_USE_KEY); }, ARRAY_FILTER_USE_KEY);
} }
// Add group middleware // Add group middleware
$middleware = []; $middleware = [];
if(isset($options['middleware']) === true) { if (isset($options['middleware']) === true) {
$middleware = $options['middleware']; $middleware = $options['middleware'];
} }
$this->group( $this->group(
$pattern, $pattern,
function (Router $router) use ($controllerClass, $defaultMapping, $aliasBase): void { function (Router $router) use ($controllerClass, $defaultMapping, $aliasBase): void {
foreach ($defaultMapping as $controllerMethod => $methodPattern) { foreach ($defaultMapping as $controllerMethod => $methodPattern) {
$router->map( $router->map(
$methodPattern, $methodPattern,
$controllerClass.'->'.$controllerMethod $controllerClass . '->' . $controllerMethod
)->setAlias($aliasBase.'.'.$controllerMethod); )->setAlias($aliasBase . '.' . $controllerMethod);
} }
} },
, $middleware); $middleware
);
} }
/** /**

@ -42,7 +42,7 @@ require_once __DIR__ . '/autoload.php';
* Routes a PATCH URL to a callback function. * Routes a PATCH URL to a callback function.
* @method static Route delete(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '') * @method static Route delete(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '')
* Routes a DELETE URL to a callback function. * 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. * Adds standardized RESTful routes for a controller.
* @method static Router router() Returns Router instance. * @method static Router router() Returns Router instance.
* @method static string getUrl(string $alias, array<string, mixed> $params = []) Gets a url from an alias * @method static string getUrl(string $alias, array<string, mixed> $params = []) Gets a url from an alias

@ -361,8 +361,7 @@ class FlightTest extends TestCase
string $output, string $output,
array $renderParams, array $renderParams,
string $regexp string $regexp
): void ): void {
{
Flight::view()->preserveVars = false; Flight::view()->preserveVars = false;
$this->expectOutputString($output); $this->expectOutputString($output);

@ -2,13 +2,15 @@
namespace tests\groupcompactsyntax; namespace tests\groupcompactsyntax;
final class UsersController { final class UsersController
{
public function list(): void public function list(): void
{ {
echo __METHOD__; echo __METHOD__;
} }
public function handleRegister(): void { public function handleRegister(): void
{
echo __METHOD__; echo __METHOD__;
} }
} }

Loading…
Cancel
Save