1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\Routing; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Routing\RouteSubscriberBase; |
6
|
|
|
use Drupal\graphql\Plugin\GraphQL\SchemaPluginManager; |
7
|
|
|
use Symfony\Component\Routing\Route; |
8
|
|
|
use Symfony\Component\Routing\RouteCollection; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Registers routes for all configurable schemas. |
12
|
|
|
*/ |
13
|
|
View Code Duplication |
class ConfigurationRoutes extends RouteSubscriberBase { |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The graphql schema plugin manager. |
17
|
|
|
* |
18
|
|
|
* @var \Drupal\graphql\Plugin\GraphQL\SchemaPluginManager |
19
|
|
|
*/ |
20
|
|
|
protected $schemaManager; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructs a QueryRoutes object. |
24
|
|
|
* |
25
|
|
|
* @param \Drupal\graphql\Plugin\GraphQL\SchemaPluginManager $schemaManager |
26
|
|
|
* The graphql schema plugin manager. |
27
|
|
|
*/ |
28
|
|
|
public function __construct(SchemaPluginManager $schemaManager) { |
29
|
|
|
$this->schemaManager = $schemaManager; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Alters existing routes for a specific collection. |
34
|
|
|
* |
35
|
|
|
* @param \Symfony\Component\Routing\RouteCollection $collection |
36
|
|
|
* The route collection for adding routes. |
37
|
|
|
*/ |
38
|
|
|
protected function alterRoutes(RouteCollection $collection) { |
39
|
|
|
$routes = new RouteCollection(); |
40
|
|
|
|
41
|
|
|
foreach ($this->schemaManager->getDefinitions() as $key => $definition) { |
42
|
|
|
if (empty($definition['uses_plugins'])) { |
43
|
|
|
continue; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$routes->add("graphql.configuration.$key", new Route("{$definition['path']}/configure", [ |
47
|
|
|
'schema' => $key, |
48
|
|
|
'_controller' => '\Drupal\graphql\Controller\ConfigurationController::configurationOverview', |
49
|
|
|
'_title' => 'Configuration', |
50
|
|
|
], [ |
51
|
|
|
'_admin_route' => 'TRUE', |
52
|
|
|
])); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$collection->addCollection($routes); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.