Loading core/lib/Drupal/Core/Routing/AttributeRouteDiscovery.php +21 −19 Original line number Diff line number Diff line Loading @@ -18,9 +18,9 @@ class AttributeRouteDiscovery extends StaticRouteDiscoveryBase { /** * @param \Traversable<string, string> $namespaces * An object that implements \Traversable which contains the root paths * keyed by the corresponding namespace to look for plugin implementations. * @param \Traversable<string, string|array<int, string>> $namespaces * An object implementing \Traversable containing root paths keyed by the * corresponding namespace to look for controller implementations. */ public function __construct( protected readonly \Traversable $namespaces, Loading @@ -37,9 +37,10 @@ protected static function getPriority(): int { * {@inheritdoc} */ protected function collectRoutes(): iterable { foreach ($this->namespaces as $namespace => $directory) { $directory .= '/Controller'; foreach ($this->namespaces as $namespace => $directories) { $namespace .= '\\Controller'; foreach ((array) $directories as $directory) { $directory .= '/Controller'; if (is_dir($directory)) { $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS) Loading @@ -60,6 +61,7 @@ protected function collectRoutes(): iterable { } } } } /** * Creates a route collection from a class's attributed methods. Loading core/tests/Drupal/Tests/Core/Routing/AttributeRouteDiscoveryTest.php +20 −0 Original line number Diff line number Diff line Loading @@ -42,6 +42,26 @@ protected function setUp(): void { $this->routeCollection = $event->getRouteCollection(); } /** * @legacy-covers ::onRouteBuild */ public function testOnRouteBuildWithArrayNamespaceDirectories(): void { $event = new RouteBuildEvent(new RouteCollection()); $namespaces = new \ArrayObject([ 'Drupal\router_test' => [ $this->root . '/core/modules/system/tests/modules/router_test_directory/missing', $this->root . '/core/modules/system/tests/modules/router_test_directory/src', ], ]); $discovery = new AttributeRouteDiscovery($namespaces); $discovery->onRouteBuild($event); $route = $event->getRouteCollection()->get('router_test.method_attribute'); $this->assertNotNull($route); $this->assertSame('/test_method_attribute', $route->getPath()); $this->assertSame(TestAttributes::class . '::attributeMethod', $route->getDefault('_controller')); } /** * @legacy-covers ::onRouteBuild */ Loading Loading
core/lib/Drupal/Core/Routing/AttributeRouteDiscovery.php +21 −19 Original line number Diff line number Diff line Loading @@ -18,9 +18,9 @@ class AttributeRouteDiscovery extends StaticRouteDiscoveryBase { /** * @param \Traversable<string, string> $namespaces * An object that implements \Traversable which contains the root paths * keyed by the corresponding namespace to look for plugin implementations. * @param \Traversable<string, string|array<int, string>> $namespaces * An object implementing \Traversable containing root paths keyed by the * corresponding namespace to look for controller implementations. */ public function __construct( protected readonly \Traversable $namespaces, Loading @@ -37,9 +37,10 @@ protected static function getPriority(): int { * {@inheritdoc} */ protected function collectRoutes(): iterable { foreach ($this->namespaces as $namespace => $directory) { $directory .= '/Controller'; foreach ($this->namespaces as $namespace => $directories) { $namespace .= '\\Controller'; foreach ((array) $directories as $directory) { $directory .= '/Controller'; if (is_dir($directory)) { $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS) Loading @@ -60,6 +61,7 @@ protected function collectRoutes(): iterable { } } } } /** * Creates a route collection from a class's attributed methods. Loading
core/tests/Drupal/Tests/Core/Routing/AttributeRouteDiscoveryTest.php +20 −0 Original line number Diff line number Diff line Loading @@ -42,6 +42,26 @@ protected function setUp(): void { $this->routeCollection = $event->getRouteCollection(); } /** * @legacy-covers ::onRouteBuild */ public function testOnRouteBuildWithArrayNamespaceDirectories(): void { $event = new RouteBuildEvent(new RouteCollection()); $namespaces = new \ArrayObject([ 'Drupal\router_test' => [ $this->root . '/core/modules/system/tests/modules/router_test_directory/missing', $this->root . '/core/modules/system/tests/modules/router_test_directory/src', ], ]); $discovery = new AttributeRouteDiscovery($namespaces); $discovery->onRouteBuild($event); $route = $event->getRouteCollection()->get('router_test.method_attribute'); $this->assertNotNull($route); $this->assertSame('/test_method_attribute', $route->getPath()); $this->assertSame(TestAttributes::class . '::attributeMethod', $route->getDefault('_controller')); } /** * @legacy-covers ::onRouteBuild */ Loading