Verified Commit 620cbeb9 authored by Dave Long's avatar Dave Long
Browse files

fix: #3600697 Attribute route discovery does not handle arrays for JSON:API contrib modules

By: ptmkenny
By: kieran.cott
By: guillaumeg
parent 482b10f9
Loading
Loading
Loading
Loading
Loading
+21 −19
Original line number Diff line number Diff line
@@ -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,
@@ -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)
@@ -60,6 +61,7 @@ protected function collectRoutes(): iterable {
        }
      }
    }
  }

  /**
   * Creates a route collection from a class's attributed methods.
+20 −0
Original line number Diff line number Diff line
@@ -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
   */