Completed
Pull Request — 8.x-3.x (#404)
by Sebastian
03:56 queued 01:36
created

SchemaPluginManager::getSchemaBuilder()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL;
4
5
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
6
use Drupal\Core\Extension\ModuleHandlerInterface;
7
use Drupal\Core\Plugin\DefaultPluginManager;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
use Traversable;
10
11
class SchemaPluginManager extends DefaultPluginManager {
12
13
  /**
14
   * Static cache for plugin instances.
15
   *
16
   * @var object[]
17
   */
18
  protected $instances = [];
19
20
  /**
21
   * The dependency injection container.
22
   *
23
   * @var \Symfony\Component\DependencyInjection\ContainerInterface
24
   */
25
  protected $container;
26
27
  /**
28
   * {@inheritdoc}
29
   */
30
  public function __construct(
31
    $pluginSubdirectory,
32
    Traversable $namespaces,
33
    ModuleHandlerInterface $moduleHandler,
34
    $pluginInterface,
35
    $pluginAnnotationName,
36
    $pluginType,
37
    ContainerInterface $container
38
  ) {
39
    $this->container = $container;
40
    $this->alterInfo($pluginType);
41
42
    parent::__construct(
43
      $pluginSubdirectory,
44
      $namespaces,
45
      $moduleHandler,
46
      $pluginInterface,
47
      $pluginAnnotationName
48
    );
49
  }
50
51
  /**
52
   * {@inheritdoc}
53
   */
54
  public function createInstance($pluginId, array $configuration = []) {
55
    if (!array_key_exists($pluginId, $this->instances)) {
56
      $this->instances[$pluginId] = parent::createInstance($pluginId);
57
      if (!$this->instances[$pluginId] instanceof SchemaPluginInterface) {
58
        throw new \LogicException(sprintf('Plugin %s does not implement \Drupal\graphql\Plugin\GraphQL\SchemaPluginInterface.', $pluginId));
59
      }
60
61
      $schemaBuilder = $this->getSchemaBuilder($pluginId);
62
      $this->instances[$pluginId]->buildConfig($schemaBuilder);
63
    }
64
65
    return $this->instances[$pluginId];
66
  }
67
68
  /**
69
   * Retrieves a schema builder instance.
70
   *
71
   * @param string $pluginId
72
   *   The plugin id.
73
   *
74
   * @return \Drupal\graphql\Plugin\GraphQL\SchemaBuilderInterface|NULL The schema builder.
75
   *   The schema builder.
76
   *
77
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
78
   */
79
  public function getSchemaBuilder($pluginId) {
80
    $pluginDefinition = $this->getDefinition($pluginId);
81
    if (empty($pluginDefinition['builder'])) {
82
      return NULL;
83
    }
84
85
    $class = $pluginDefinition['builder'];
86
    if (!is_subclass_of($class, '\Drupal\graphql\Plugin\GraphQL\SchemaBuilderInterface')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
87
      throw new InvalidPluginDefinitionException(sprintf('The schema builder for plugin %s does not implement \Drupal\graphql\Plugin\GraphQL\SchemaBuilderInterface.', $schema->getPluginId()));
88
    }
89
90
    return $class::createInstance($this->container, $pluginId, $pluginDefinition);
91
  }
92
93
}
94