Loading core/lib/Drupal/Core/Routing/AttributeRouteDiscovery.php +26 −24 Original line number Diff line number Diff line Loading @@ -19,9 +19,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 @@ -43,7 +43,8 @@ protected function collectRoutes(): iterable { 'Form' => $this->createFormRouteCollection(...), ]; foreach ($this->namespaces as $namespace => $directory) { foreach ($this->namespaces as $namespace => $directories) { foreach ((array) $directories as $directory) { foreach ($routeTypes as $routeType => $factory) { $routeDirectory = $directory . '/' . $routeType; $routeNamespace = $namespace . '\\' . $routeType; Loading Loading @@ -71,6 +72,7 @@ protected function collectRoutes(): iterable { } } } } /** * Creates a route collection from a controller class's attributed methods. Loading core/tests/Drupal/Tests/Core/Routing/AttributeRouteDiscoveryTest.php +20 −0 Original line number Diff line number Diff line Loading @@ -45,6 +45,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 +26 −24 Original line number Diff line number Diff line Loading @@ -19,9 +19,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 @@ -43,7 +43,8 @@ protected function collectRoutes(): iterable { 'Form' => $this->createFormRouteCollection(...), ]; foreach ($this->namespaces as $namespace => $directory) { foreach ($this->namespaces as $namespace => $directories) { foreach ((array) $directories as $directory) { foreach ($routeTypes as $routeType => $factory) { $routeDirectory = $directory . '/' . $routeType; $routeNamespace = $namespace . '\\' . $routeType; Loading Loading @@ -71,6 +72,7 @@ protected function collectRoutes(): iterable { } } } } /** * Creates a route collection from a controller class's attributed methods. Loading
core/tests/Drupal/Tests/Core/Routing/AttributeRouteDiscoveryTest.php +20 −0 Original line number Diff line number Diff line Loading @@ -45,6 +45,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