diff --git a/Extractor/ExposedRoutesExtractor.php b/Extractor/ExposedRoutesExtractor.php index 1c8e475..c151222 100644 --- a/Extractor/ExposedRoutesExtractor.php +++ b/Extractor/ExposedRoutesExtractor.php @@ -58,12 +58,12 @@ public function getRoutes(): RouteCollection $expose = $route->getOption('expose'); if (false !== $expose && 'false' !== $expose) { - $routes->add($name, $route); + $routes->add((string)$name, $route); } continue; } - preg_match('#^'.$this->pattern.'$#', $name, $matches); + preg_match('#^'.$this->pattern.'$#', (string)$name, $matches); if (0 === count($matches)) { continue; @@ -77,7 +77,7 @@ public function getRoutes(): RouteCollection $route = clone $route; $route->setOption('expose', $domain); - $routes->add($name, $route); + $routes->add((string)$name, $route); } return $routes; @@ -172,10 +172,10 @@ public function getResources(): array /** * {@inheritDoc} */ - public function isRouteExposed(Route $route, $name): bool + public function isRouteExposed(Route $route, string $name): bool { if (false === $route->hasOption('expose')) { - return '' !== $this->pattern && preg_match('#^'.$this->pattern.'$#', $name); + return '' !== $this->pattern && preg_match('#^'.$this->pattern.'$#', (string)$name); } $status = $route->getOption('expose'); diff --git a/Tests/Extractor/ExposedRoutesExtractorTest.php b/Tests/Extractor/ExposedRoutesExtractorTest.php index e631b95..9331ac4 100644 --- a/Tests/Extractor/ExposedRoutesExtractorTest.php +++ b/Tests/Extractor/ExposedRoutesExtractorTest.php @@ -175,7 +175,25 @@ public function provideTestGetHostOverHttps(): array 'HTTPS Non-Standard' => ['127.0.0.1', 9876, '127.0.0.1:9876'], ]; } + public function testGetRoutesWithNumericNames(): void + { + $expected = new RouteCollection(); + $expected->add('404', new Route('/404')); + $expected->add('500', new Route('/500')); + $expected->add('literal', new Route('/literal')); + + $router = $this->getRouter($expected); + $extractor = new ExposedRoutesExtractor($router, ['.*'], $this->cacheDir, []); + $routes = $extractor->getRoutes(); + $this->assertCount(3, $routes); + + // Verify that numeric route names are handled correctly + $this->assertTrue($extractor->isRouteExposed($routes->get('404'), '404')); + $this->assertTrue($extractor->isRouteExposed($routes->get('500'), '500')); + // Test with integer key as well, since PHP may convert string keys to int + $this->assertTrue($extractor->isRouteExposed($routes->get('404'), '404')); + } /** * Get a mock object which represents a Router. */