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