1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\Plugin\GraphQL; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Extension\ModuleHandlerInterface; |
6
|
|
|
use Drupal\Core\Plugin\DefaultPluginManager; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Traversable; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Base class for type system plugin managers or all sorts. |
12
|
|
|
*/ |
13
|
|
|
class TypeSystemPluginManager extends DefaultPluginManager { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Static cache for plugin instances. |
17
|
|
|
* |
18
|
|
|
* @var object[] |
19
|
|
|
*/ |
20
|
|
|
protected $instances = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* A logger instance. |
24
|
|
|
* |
25
|
|
|
* @var \Psr\Log\LoggerInterface |
26
|
|
|
*/ |
27
|
|
|
protected $logger; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function __construct( |
33
|
|
|
$pluginSubdirectory, |
34
|
|
|
Traversable $namespaces, |
35
|
|
|
ModuleHandlerInterface $moduleHandler, |
36
|
|
|
$pluginInterface, |
37
|
|
|
$pluginAnnotationName, |
38
|
|
|
$alterInfo, |
39
|
|
|
LoggerInterface $logger |
40
|
|
|
) { |
41
|
|
|
$this->alterInfo($alterInfo); |
42
|
|
|
$this->logger = $logger; |
43
|
|
|
|
44
|
|
|
parent::__construct( |
45
|
|
|
$pluginSubdirectory, |
46
|
|
|
$namespaces, |
47
|
|
|
$moduleHandler, |
48
|
|
|
$pluginInterface, |
49
|
|
|
$pluginAnnotationName |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function createInstance($pluginId, array $configuration = []) { |
57
|
|
|
if (!array_key_exists('schema_manager', $configuration)) { |
58
|
|
|
throw new \LogicException('Missing schema manager instance in configuration.'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!array_key_exists($pluginId, $this->instances)) { |
62
|
|
|
// We deliberately ignore that $configuration could be different, because |
63
|
|
|
// GraphQL plugins don't contain user defined configuration. |
64
|
|
|
$this->instances[$pluginId] = parent::createInstance($pluginId); |
65
|
|
|
if (!$this->instances[$pluginId] instanceof TypeSystemPluginInterface) { |
66
|
|
|
throw new \LogicException(sprintf('Plugin %s does not implement \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface.', $pluginId)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$this->instances[$pluginId]->buildConfig($configuration['schema_manager']); |
71
|
|
|
} |
72
|
|
|
catch (\Exception $exception) { |
73
|
|
|
$this->logger->warning(sprintf('Plugin %s could not be added to the GraphQL schema: %s', $pluginId, $exception->getMessage())); |
74
|
|
|
$this->instances[$pluginId] = NULL; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
return $this->instances[$pluginId]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|