1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\Plugin; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Cache\Cache; |
6
|
|
|
use Drupal\Core\Cache\CacheBackendInterface; |
7
|
|
|
use Drupal\Core\Extension\ModuleHandlerInterface; |
8
|
|
|
use Drupal\Core\Plugin\DefaultPluginManager; |
9
|
|
|
use Drupal\graphql\GraphQLLanguageContext; |
10
|
|
|
|
11
|
|
|
class FieldPluginManager extends DefaultPluginManager { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Static cache of plugin instances. |
15
|
|
|
* |
16
|
|
|
* @var \Drupal\graphql\Plugin\FieldPluginInterface[] |
17
|
|
|
*/ |
18
|
|
|
protected $instances; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The language context. |
22
|
|
|
* |
23
|
|
|
* Will be passed on to all plugin instances. |
24
|
|
|
* |
25
|
|
|
* @var \Drupal\graphql\GraphQLLanguageContext |
26
|
|
|
*/ |
27
|
|
|
protected $languageContext; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* FieldPluginManager constructor. |
31
|
|
|
* |
32
|
|
|
* @param bool|string $pluginSubdirectory |
33
|
|
|
* The plugin's subdirectory. |
34
|
|
|
* @param \Traversable $namespaces |
35
|
|
|
* An object that implements \Traversable which contains the root paths |
36
|
|
|
* keyed by the corresponding namespace to look for plugin implementations. |
37
|
|
|
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler |
38
|
|
|
* The module handler. |
39
|
|
|
* @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend |
40
|
|
|
* The cache backend. |
41
|
|
|
* @param \Drupal\graphql\GraphQLLanguageContext $languageContext |
42
|
|
|
* The language context. |
43
|
|
|
* @param string|null $pluginInterface |
44
|
|
|
* The interface each plugin should implement. |
45
|
|
|
* @param string $pluginAnnotationName |
46
|
|
|
* The name of the annotation that contains the plugin definition. |
47
|
|
|
* @param array $config |
48
|
|
|
* The configuration service parameter. |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
$pluginSubdirectory, |
52
|
|
|
\Traversable $namespaces, |
53
|
|
|
ModuleHandlerInterface $moduleHandler, |
54
|
|
|
CacheBackendInterface $cacheBackend, |
55
|
|
|
GraphQLLanguageContext $languageContext, |
56
|
|
|
$pluginInterface, |
57
|
|
|
$pluginAnnotationName, |
58
|
|
|
array $config |
59
|
|
|
) { |
60
|
|
|
|
61
|
|
|
parent::__construct( |
62
|
|
|
$pluginSubdirectory, |
63
|
|
|
$namespaces, |
64
|
|
|
$moduleHandler, |
65
|
|
|
$pluginInterface, |
66
|
|
|
$pluginAnnotationName |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$this->languageContext = $languageContext; |
70
|
|
|
$this->alterInfo('graphql_fields'); |
71
|
|
|
$this->useCaches(empty($config['development'])); |
72
|
|
|
$this->setCacheBackend($cacheBackend, 'fields', ['graphql']); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function createInstance($plugin_id, array $configuration = []) { |
79
|
|
|
/** @var \Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase $plugin */ |
80
|
|
|
$plugin = parent::createInstance($plugin_id, $configuration); |
81
|
|
|
$plugin->setLanguageContext($this->languageContext); |
82
|
|
|
return $plugin; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function getInstance(array $options) { |
89
|
|
|
if (!isset($this->instances[$options['id']])) { |
90
|
|
|
$this->instances[$options['id']] = $this->createInstance($options['id']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->instances[$options['id']]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function clearCachedDefinitions() { |
100
|
|
|
parent::clearCachedDefinitions(); |
101
|
|
|
$this->instances = []; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
protected function setCachedDefinitions($definitions) { |
108
|
|
|
$this->definitions = $definitions; |
109
|
|
|
$this->cacheSet($this->cacheKey, $definitions, $this->getCacheMaxAge(), $this->getCacheTags()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function getCacheTags() { |
116
|
|
|
$definitions = $this->getDefinitions(); |
117
|
|
|
return array_reduce($definitions, function ($carry, $current) { |
118
|
|
|
if (!empty($current['schema_cache_tags'])) { |
119
|
|
|
return Cache::mergeTags($carry, $current['schema_cache_tags']); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $carry; |
123
|
|
|
}, $this->cacheTags); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function getCacheMaxAge() { |
130
|
|
|
$definitions = $this->getDefinitions(); |
131
|
|
|
$age = Cache::PERMANENT; |
132
|
|
|
foreach ($definitions as $definition) { |
133
|
|
|
if (!isset($definition['schema_cache_max_age'])) { |
134
|
|
|
continue; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Bail out early if the cache max age is 0. |
138
|
|
|
if (($age = Cache::mergeMaxAges($age, $definition['schema_cache_max_age'])) === 0) { |
139
|
|
|
return $age; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $age; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|