route registration cleanup

This commit is contained in:
robert dennis
2026-10-06 20:11:59 -04:00
parent 4adda9b8df
commit db5f27bdcd
3 changed files with 96 additions and 12 deletions
@@ -58,11 +58,7 @@ final class ExtensionRegistration
$actions = $this->actions->snapshot();
$console = $this->console->snapshot();
$headTags = $this->headTags->snapshot();
$router = clone $this->router;
$routes = $this->router->getRoutes();
if ($routes instanceof RouteCollection) {
$router->setRoutes(clone $routes);
}
$router = new ExtensionStagingRouter($this->router);
Route::swap($router);
$registrations = $this->pending;
@@ -72,13 +68,7 @@ final class ExtensionRegistration
$registration();
}
$routes = $router->getRoutes();
if ($routes instanceof RouteCollection) {
$routes->refreshNameLookups();
$routes->refreshActionLookups();
$this->router->setRoutes($routes);
}
$router->apply();
$this->active = true;
} catch (Throwable $throwable) {
$this->settings->unregister($identifier);
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace Pterodactyl\Services\Extensions;
use Illuminate\Routing\RouteCollection;
use Illuminate\Routing\Router;
/**
* The router extension route files register against while a provider's registration
* is staged. It starts as a copy of the panel's router, and the routes, binders
* (`Route::bind`, `Route::model`) and patterns (`Route::pattern`) the files add only
* reach the panel's router through apply(), so a failed commit leaves none behind.
*/
final class ExtensionStagingRouter extends Router
{
public function __construct(private readonly Router $router)
{
parent::__construct($router->events, $router->container);
$this->middleware = $router->middleware;
$this->middlewareGroups = $router->middlewareGroups;
$this->middlewarePriority = $router->middlewarePriority;
$this->binders = $router->binders;
$this->patterns = $router->patterns;
$this->groupStack = $router->groupStack;
$this->implicitBindingCallback = $router->implicitBindingCallback;
$routes = $router->getRoutes();
if ($routes instanceof RouteCollection) {
$this->setRoutes(clone $routes);
} else {
$this->routes = $routes;
}
}
public function apply(): void
{
$routes = $this->getRoutes();
if ($routes instanceof RouteCollection) {
$routes->refreshNameLookups();
$routes->refreshActionLookups();
$this->router->setRoutes($routes);
}
$this->router->binders = [...$this->router->binders, ...$this->binders];
$this->router->patterns = [...$this->router->patterns, ...$this->patterns];
}
}
@@ -301,6 +301,31 @@ test('a single claimed prefix can be mounted and undeclared prefixes are refused
expect(fn () => provider('plain')->rootRoutes($path))->toThrow(InvalidExtensionException::class, 'declares no "routes.root" prefixes');
});
test('route binders and patterns declared in a staged route file reach the panel router', function (): void {
extensions('lookup');
$provider = provider('lookup', ['routes' => ['root' => ['lookup']]]);
$provider->beginRegistration();
$provider->rootRoutes(writeBindingRouteFile());
$provider->commitRegistration();
$this->get('/lookup/abc')->assertOk()->assertContent('ABC');
expect(Route::getBindingCallback('code'))->not->toBeNull();
expect(Route::getPatterns())->toHaveKey('code', '[a-z]+');
});
test('route binders and patterns of a provider that fails to commit are discarded', function (): void {
extensions('lookup');
$provider = provider('lookup', ['routes' => ['root' => ['lookup']]]);
$provider->beginRegistration();
$provider->rootRoutes(writeBindingRouteFile());
$provider->permissions('Broken.', []);
expect(fn () => $provider->commitRegistration())->toThrow(InvalidArgumentException::class);
expect(Route::getPatterns())->not->toHaveKey('code');
expect(Route::getBindingCallback('code'))->toBeNull();
expect(Route::getRoutes()->match(Request::create('/lookup/abc'))->uri())->toBe('{react}');
});
/** Marks the given extensions as enabled and collects every failure recorded against one. */
function extensions(string ...$identifiers): void
{
@@ -390,6 +415,25 @@ function writeRouteFile(string $filename, string $uri, string $name): string
})->call(pterodactylTestCase());
}
/** A route file that scopes its parameter through Route::pattern() and Route::bind(). */
function writeBindingRouteFile(): string
{
return (function (): string {
$path = $this->directory.DIRECTORY_SEPARATOR.'binding.php';
File::put($path, <<<'PHP'
<?php
use Illuminate\Support\Facades\Route;
Route::pattern('code', '[a-z]+');
Route::bind('code', fn (string $value): string => strtoupper($value));
Route::get('/{code}', fn (string $code): string => $code)->name('code');
PHP);
return $path;
})->call(pterodactylTestCase());
}
final class CoreDeleter implements DeletesServers
{
/** @var list<string> */