1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\GraphQL\QueryProvider; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\FileSystem\RegexDirectoryIterator; |
6
|
|
|
use Drupal\Core\Cache\CacheBackendInterface; |
7
|
|
|
use Drupal\Core\Config\ConfigFactoryInterface; |
8
|
|
|
use GraphQL\Server\OperationParams; |
9
|
|
|
|
10
|
|
|
class GqlQueryMapQueryProvider implements QueryProviderInterface { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The cache backend for storing query map file paths. |
14
|
|
|
* |
15
|
|
|
* @var \Drupal\Core\Cache\CacheBackendInterface |
16
|
|
|
*/ |
17
|
|
|
protected $cacheBackend; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The paths to use for finding query maps. |
21
|
|
|
* |
22
|
|
|
* @var string[] |
23
|
|
|
*/ |
24
|
|
|
protected $lookupPaths; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* QueryProvider constructor. |
28
|
|
|
* |
29
|
|
|
* @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend |
30
|
|
|
* The cache backend for storing query map file paths. |
31
|
|
|
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory |
32
|
|
|
* The config factory. |
33
|
|
|
*/ |
34
|
|
|
public function __construct(CacheBackendInterface $cacheBackend, ConfigFactoryInterface $configFactory) { |
35
|
|
|
$this->lookupPaths = $configFactory->get('graphql.query_map_json.config')->get('lookup_paths') ?: []; |
36
|
|
|
$this->cacheBackend = $cacheBackend; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function getQuery($id, OperationParams $operation) { |
43
|
|
|
// Check that the id is properly formatted. |
44
|
|
|
if (empty($id)) { |
45
|
|
|
return NULL; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
if (!(($cache = $this->cacheBackend->get('graphql_query_map_gql_versions')) && ($versions = $cache->data) !== NULL)) { |
|
|
|
|
49
|
|
|
$this->cacheBackend->set('graphql_query_map_gql_versions', $versions = $this->discoverQueryMaps()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (isset($versions) && isset($versions[$id]) && file_exists($versions[$id])) { |
53
|
|
|
return file_get_contents($versions[$id]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return NULL; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Discovers the available query maps within the configured lookup paths. |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
* An associative array of query maps with the query map versions as keys. |
64
|
|
|
*/ |
65
|
|
|
protected function discoverQueryMaps() { |
66
|
|
|
$maps = []; |
67
|
|
|
foreach ($this->lookupPaths as $path) { |
68
|
|
View Code Duplication |
if (is_dir($path)) { |
|
|
|
|
69
|
|
|
$iterator = new RegexDirectoryIterator($path, '/\.gql/i'); |
70
|
|
|
|
71
|
|
|
/** @var \SplFileInfo $file */ |
72
|
|
|
foreach ($iterator as $file) { |
73
|
|
|
$filename = $file->getBasename('.gql'); |
74
|
|
|
$maps[$filename] = $file->getPathname(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (is_file($path)) { |
79
|
|
|
$file = new \SplFileInfo($path); |
80
|
|
|
$filename = $file->getBasename('.gql'); |
81
|
|
|
$maps[$filename] = $file->getPathname(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $maps; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
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.