1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\Utility\UrlHelper; |
6
|
|
|
use Drupal\Core\Path\PathValidatorInterface; |
7
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
8
|
|
|
use Drupal\Core\Url; |
9
|
|
|
use Drupal\graphql\GraphQL\Cache\CacheableValue; |
10
|
|
|
use Drupal\graphql\GraphQL\Execution\ResolveContext; |
11
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
12
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Retrieve a route object based on a path. |
17
|
|
|
* |
18
|
|
|
* @GraphQLField( |
19
|
|
|
* id = "url_route", |
20
|
|
|
* secure = true, |
21
|
|
|
* name = "route", |
22
|
|
|
* description = @Translation("Loads a route by its path."), |
23
|
|
|
* type = "Url", |
24
|
|
|
* arguments = { |
25
|
|
|
* "path" = "String!" |
26
|
|
|
* } |
27
|
|
|
* ) |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
class Route extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The path validator service. |
33
|
|
|
* |
34
|
|
|
* @var \Drupal\Core\Path\PathValidatorInterface |
35
|
|
|
*/ |
36
|
|
|
protected $pathValidator; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
42
|
|
|
return new static( |
43
|
|
|
$configuration, |
44
|
|
|
$plugin_id, |
45
|
|
|
$plugin_definition, |
46
|
|
|
$container->get('path.validator') |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Route constructor. |
52
|
|
|
* |
53
|
|
|
* @param array $configuration |
54
|
|
|
* The plugin configuration. |
55
|
|
|
* @param string $pluginId |
56
|
|
|
* The plugin id. |
57
|
|
|
* @param mixed $pluginDefinition |
58
|
|
|
* The plugin definition. |
59
|
|
|
* @param \Drupal\Core\Path\PathValidatorInterface $pathValidator |
60
|
|
|
* The path validator service. |
61
|
|
|
*/ |
62
|
|
|
public function __construct(array $configuration, $pluginId, $pluginDefinition, PathValidatorInterface $pathValidator) { |
63
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
64
|
|
|
$this->pathValidator = $pathValidator; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { |
71
|
|
|
if ($url = $this->pathValidator->getUrlIfValid($args['path'])) { |
72
|
|
|
yield $url; |
73
|
|
|
} |
74
|
|
|
else { |
75
|
|
|
yield (new CacheableValue(NULL))->addCacheTags(['4xx-response']); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
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.