Completed
Push — 8.x-1.x ( 2f413c...494d52 )
by Philipp
01:14
created

GraphQLTwigRouter::routes()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 0
dl 0
loc 20
rs 8.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_twig\Routing;
4
5
use Drupal\Core\Extension\ThemeHandlerInterface;
6
use Drupal\graphql_twig\Controller\RouteController;
7
use Symfony\Component\Routing\Route;
8
9
class GraphQLTwigRouter {
10
11
  /**
12
   * The theme handler to collect routes from theme info files.
13
   *
14
   * @var \Drupal\Core\Extension\ThemeHandlerInterface
15
   */
16
  protected $themeHandler;
17
18
  /**
19
   * GraphQLTwigRouter constructor.
20
   *
21
   * @param \Drupal\Core\Extension\ThemeHandlerInterface $themeHandler
22
   *   The theme handler to collect routes from theme info files.
23
   */
24
  public function __construct(ThemeHandlerInterface $themeHandler) {
25
    $this->themeHandler = $themeHandler;
26
  }
27
28
  /**
29
   * Generate a list of routes based on theme info files.
30
   *
31
   * @return \Symfony\Component\Routing\Route[]
32
   *   A list of routes defined by themes.
33
   */
34
  public function routes() {
35
    $routes = [];
36
    foreach ($this->themeHandler->listInfo() as $info) {
37
      if (isset($info->info['routes'])) {
38
        foreach ($info->info['routes'] as $name => $route) {
39
          $routes['graphql_twig.dynamic.' . $name] = new Route($route['path'], [
40
            '_controller' => RouteController::class . ':page',
41
            '_title_callback' => RouteController::class . ':title',
42
            '_title' => isset($route['title']) ? $route['title'] : NULL,
43
            '_title_query' => isset($route['title_query']) ? $route['title_query'] : NULL,
44
            '_graphql_theme_hook' => $name,
45
          ], isset($route['requirements']) ? $route['requirements'] : [
46
            '_access' => 'TRUE',
47
          ]);
48
        }
49
50
      }
51
    }
52
    return $routes;
53
  }
54
55
}
56