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

SchemaPluginBase::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 4
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL\Schemas;
4
5
use Drupal\Component\Plugin\PluginBase;
6
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilder;
8
use Drupal\graphql\Plugin\GraphQL\SchemaPluginInterface;
9
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface;
10
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManagerAggregator;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Youshido\GraphQL\Schema\InternalSchemaMutationObject;
13
use Youshido\GraphQL\Schema\InternalSchemaQueryObject;
14
use Youshido\GraphQL\Schema\Schema;
15
16
abstract class SchemaPluginBase extends PluginBase implements SchemaPluginInterface, ContainerFactoryPluginInterface {
17
18
  /**
19
   * Static cache of plugin definitions.
20
   *
21
   * @var array
22
   */
23
  protected $definitions;
24
25
  /**
26
   * The schema builder object.
27
   *
28
   * @var \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilder
29
   */
30
  protected $schemaBuilder;
31
32
  /**
33
   * {@inheritdoc}
34
   */
35
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
36
    return new static(
37
      $configuration,
38
      $plugin_id,
39
      $plugin_definition,
40
      $container->get('graphql.plugin_manager_aggregator')
41
    );
42
  }
43
44
  /**
45
   * SchemaPluginBase constructor.
46
   *
47
   * @param array $configuration
48
   *   The plugin configuration arra.
49
   * @param string $pluginId
50
   *   The plugin id.
51
   * @param mixed $pluginDefinition
52
   *   The plugin definition array.
53
   * @param \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManagerAggregator $pluginManagers
54
   *   Type system plugin manager aggregator service.
55
   */
56
  public function __construct($configuration, $pluginId, $pluginDefinition, TypeSystemPluginManagerAggregator $pluginManagers) {
57
    parent::__construct($configuration, $pluginId, $pluginDefinition);
58
    $this->schemaBuilder = new PluggableSchemaBuilder($this->getDefinitions($pluginManagers));
59
  }
60
61
  /**
62
   * {@inheritdoc}
63
   */
64
  public function getSchema() {
65
    $extract = function (array $input) {
66
      return array_map(function (TypeSystemPluginInterface $plugin) {
67
        return $plugin->getDefinition($this->schemaBuilder);
68
      }, $input);
69
    };
70
71
    $mutation = new InternalSchemaMutationObject(['name' => 'RootMutation']);
72
    $mutation->addFields($extract($this->schemaBuilder->getMutations()));
73
74
    $query = new InternalSchemaQueryObject(['name' => 'RootQuery']);
75
    $query->addFields($extract($this->schemaBuilder->getRootFields()));
76
77
    $types = $extract($this->schemaBuilder->find(function() {
78
      return TRUE;
79
    }, [
80
      GRAPHQL_UNION_TYPE_PLUGIN,
81
      GRAPHQL_TYPE_PLUGIN,
82
      GRAPHQL_INPUT_TYPE_PLUGIN,
83
    ]));
84
85
    return new Schema([
86
      'query' => $query,
87
      'mutation' => $mutation,
88
      'types' => $types,
89
    ]);
90
  }
91
92
  /**
93
   * Collects and aggregates all plugin definitions.
94
   *
95
   * @param \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManagerAggregator $pluginManagers
96
   *   Type system plugin manager aggregator service.
97
   *
98
   * @return array
99
   *   The plugin definitions array.
100
   */
101
  protected function getDefinitions(TypeSystemPluginManagerAggregator $pluginManagers) {
102
    $definitions = [];
103
    foreach ($pluginManagers as $manager) {
104
      foreach ($manager->getDefinitions() as $pluginId => $definition) {
105
        $definitions[] = [
106
          'id' => $pluginId,
107
          'type' => $definition['pluginType'],
108
          'weight' => $definition['weight'],
109
          'manager' => $manager,
110
          'definition' => $definition,
111
        ];
112
      }
113
    }
114
115
    uasort($definitions, '\Drupal\Component\Utility\SortArray::sortByWeightElement');
116
    return array_reverse($definitions);
117
  }
118
119
}
120