Completed
Pull Request — 8.x-3.x (#401)
by Sebastian
02:54
created

TypeSystemPluginManager::getSchemaManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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