From db5f27bdcd6acca73847cce6fafb9f8ada5cda81 Mon Sep 17 00:00:00 2001 From: robert dennis <31261583+robertdrakedennis@users.noreply.github.com> Date: Tue, 6 Oct 2026 13:44:37 -0400 Subject: [PATCH] route registration cleanup --- .../Extensions/ExtensionRegistration.php | 14 +----- .../Extensions/ExtensionStagingRouter.php | 50 +++++++++++++++++++ .../ExtensionProviderRuntimeTest.php | 44 ++++++++++++++++ 3 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 app/Services/Extensions/ExtensionStagingRouter.php diff --git a/app/Services/Extensions/ExtensionRegistration.php b/app/Services/Extensions/ExtensionRegistration.php index 9ece1606e..7a1148edd 100644 --- a/app/Services/Extensions/ExtensionRegistration.php +++ b/app/Services/Extensions/ExtensionRegistration.php @@ -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); diff --git a/app/Services/Extensions/ExtensionStagingRouter.php b/app/Services/Extensions/ExtensionStagingRouter.php new file mode 100644 index 000000000..57b5cd574 --- /dev/null +++ b/app/Services/Extensions/ExtensionStagingRouter.php @@ -0,0 +1,50 @@ +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]; + } +} diff --git a/tests/Unit/Extensions/ExtensionProviderRuntimeTest.php b/tests/Unit/Extensions/ExtensionProviderRuntimeTest.php index d58d44143..051eb684a 100644 --- a/tests/Unit/Extensions/ExtensionProviderRuntimeTest.php +++ b/tests/Unit/Extensions/ExtensionProviderRuntimeTest.php @@ -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' + strtoupper($value)); + Route::get('/{code}', fn (string $code): string => $code)->name('code'); + PHP); + + return $path; + })->call(pterodactylTestCase()); +} + final class CoreDeleter implements DeletesServers { /** @var list */