Passed
Pull Request — master (#196)
by Christoffer
03:09
created

GraphQL::lex()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Digia\GraphQL;
4
5
use Digia\GraphQL\Cache\CacheProvider;
6
use Digia\GraphQL\Execution\ExecutionInterface;
7
use Digia\GraphQL\Execution\ExecutionProvider;
8
use Digia\GraphQL\Execution\ExecutionResult;
9
use Digia\GraphQL\Language\LanguageProvider;
10
use Digia\GraphQL\Language\Node\DocumentNode;
11
use Digia\GraphQL\Language\Node\NodeInterface;
12
use Digia\GraphQL\Language\Node\TypeNodeInterface;
13
use Digia\GraphQL\Language\Node\ValueNodeInterface;
14
use Digia\GraphQL\Language\ParserInterface;
15
use Digia\GraphQL\Language\PrinterInterface;
16
use Digia\GraphQL\Language\Source;
17
use Digia\GraphQL\Schema\Building\SchemaBuilderInterface;
18
use Digia\GraphQL\Schema\Building\SchemaBuildingProvider;
19
use Digia\GraphQL\Schema\Extension\SchemaExtenderInterface;
20
use Digia\GraphQL\Schema\Extension\SchemaExtensionProvider;
21
use Digia\GraphQL\Schema\Resolver\ResolverRegistry;
22
use Digia\GraphQL\Schema\Resolver\ResolverRegistryInterface;
23
use Digia\GraphQL\Schema\SchemaInterface;
24
use Digia\GraphQL\Schema\Validation\SchemaValidationProvider;
25
use Digia\GraphQL\Schema\Validation\SchemaValidatorInterface;
26
use Digia\GraphQL\Type\CoercerProvider;
27
use Digia\GraphQL\Type\DirectivesProvider;
28
use Digia\GraphQL\Type\IntrospectionProvider;
29
use Digia\GraphQL\Type\ScalarTypesProvider;
30
use Digia\GraphQL\Util\UtilityProvider;
31
use Digia\GraphQL\Validation\RulesProvider;
32
use Digia\GraphQL\Validation\ValidationProvider;
33
use Digia\GraphQL\Validation\ValidatorInterface;
34
use League\Container\Container;
35
use League\Container\ContainerInterface;
36
37
class GraphQL
38
{
39
    public const BOOLEAN = 'GraphQLBoolean';
40
    public const FLOAT   = 'GraphQLFloat';
41
    public const INT     = 'GraphQLInt';
42
    public const ID      = 'GraphQLID';
43
    public const STRING  = 'GraphQLString';
44
45
    public const DEPRECATED_DIRECTIVE = 'GraphQLDeprecatedDirective';
46
    public const INCLUDE_DIRECTIVE    = 'GraphQLIncludeDirective';
47
    public const SKIP_DIRECTIVE       = 'GraphQLSkipDirective';
48
49
    public const SCHEMA_INTROSPECTION             = '__Schema';
50
    public const DIRECTIVE_INTROSPECTION          = '__Directive';
51
    public const DIRECTIVE_LOCATION_INTROSPECTION = '__DirectiveLocation';
52
    public const TYPE_INTROSPECTION               = '__Type';
53
    public const FIELD_INTROSPECTION              = '__Field';
54
    public const INPUT_VALUE_INTROSPECTION        = '__InputValue';
55
    public const ENUM_VALUE_INTROSPECTION         = '__EnumValue';
56
    public const TYPE_KIND_INTROSPECTION          = '__TypeKind';
57
58
    public const SCHEMA_META_FIELD_DEFINITION    = 'SchemaMetaFieldDefinition';
59
    public const TYPE_META_FIELD_DEFINITION      = 'TypeMetaFieldDefinition';
60
    public const TYPE_NAME_META_FIELD_DEFINITION = 'TypeNameMetaFieldDefinition';
61
62
    /**
63
     * @var array
64
     */
65
    private static $providers = [
66
        CacheProvider::class,
67
        LanguageProvider::class,
68
        SchemaBuildingProvider::class,
69
        SchemaExtensionProvider::class,
70
        SchemaValidationProvider::class,
71
        CoercerProvider::class,
72
        IntrospectionProvider::class,
73
        ScalarTypesProvider::class,
74
        DirectivesProvider::class,
75
        RulesProvider::class,
76
        ValidationProvider::class,
77
        ExecutionProvider::class,
78
        UtilityProvider::class,
79
    ];
80
81
    /**
82
     * @var GraphQL
83
     */
84
    private static $instance;
85
86
    /**
87
     * @var Container
88
     */
89
    protected $container;
90
91
    /**
92
     * GraphQL constructor.
93
     */
94
    private function __construct()
95
    {
96
        $container = new Container();
97
98
        $this->registerProviders($container);
99
100
        $this->container = $container;
101
    }
102
103
    /**
104
     * @return GraphQL
105
     */
106
    public static function getInstance(): self
107
    {
108
        if (null === static::$instance) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $instance to at least protected.
Loading history...
109
            static::$instance = new static();
110
        }
111
112
        return static::$instance;
113
    }
114
115
    /**
116
     * @param string $id
117
     * @param array  $args
118
     * @return mixed
119
     */
120
    public static function make(string $id, array $args = [])
121
    {
122
        /** @noinspection ExceptionsAnnotatingAndHandlingInspection */
123
        return static::getInstance()
124
            ->getContainer()
125
            ->get($id, $args);
126
    }
127
128
    /**
129
     * @param string|Source                   $source
130
     * @param array|ResolverRegistryInterface $resolverRegistry
131
     * @param array                           $options
132
     * @return SchemaInterface
133
     */
134
    public static function buildSchema($source, $resolverRegistry, array $options = []): SchemaInterface
135
    {
136
        return static::make(SchemaBuilderInterface::class)
137
            ->build(
138
                static::parse($source, $options),
139
                $resolverRegistry instanceof ResolverRegistryInterface
140
                    ? $resolverRegistry
141
                    : new ResolverRegistry($resolverRegistry),
142
                $options
143
            );
144
    }
145
146
    /**
147
     * @param SchemaInterface                 $schema
148
     * @param string|Source                   $source
149
     * @param array|ResolverRegistryInterface $resolverRegistry
150
     * @param array                           $options
151
     * @return SchemaInterface
152
     */
153
    public static function extendSchema(
154
        SchemaInterface $schema,
155
        $source,
156
        $resolverRegistry,
157
        array $options = []
158
    ): SchemaInterface {
159
        return static::make(SchemaExtenderInterface::class)
160
            ->extend(
161
                $schema,
162
                static::parse($source, $options),
163
                $resolverRegistry instanceof ResolverRegistryInterface
164
                    ? $resolverRegistry
165
                    : new ResolverRegistry($resolverRegistry),
166
                $options
167
            );
168
    }
169
170
    /**
171
     * @param SchemaInterface $schema
172
     * @return array
173
     */
174
    public static function validateSchema(SchemaInterface $schema): array
175
    {
176
        return static::make(SchemaValidatorInterface::class)->validate($schema);
177
    }
178
179
    /**
180
     * @param string|Source $source
181
     * @param array         $options
182
     * @return DocumentNode
183
     * @throws Error\InvariantException
184
     */
185
    public static function parse($source, array $options = []): DocumentNode
186
    {
187
        return static::make(ParserInterface::class)->parse($source, $options);
188
    }
189
190
    /**
191
     * @param string|Source $source
192
     * @param array         $options
193
     * @return ValueNodeInterface
194
     */
195
    public static function parseValue($source, array $options = []): ValueNodeInterface
196
    {
197
        return static::make(ParserInterface::class)->parseValue($source, $options);
198
    }
199
200
    /**
201
     * @param string|Source $source
202
     * @param array         $options
203
     * @return TypeNodeInterface
204
     */
205
    public static function parseType($source, array $options = []): TypeNodeInterface
206
    {
207
        return static::make(ParserInterface::class)->parseType($source, $options);
208
    }
209
210
    /**
211
     * @param SchemaInterface $schema
212
     * @param DocumentNode    $document
213
     * @return array
214
     */
215
    public static function validate(SchemaInterface $schema, DocumentNode $document): array
216
    {
217
        return static::make(ValidatorInterface::class)->validate($schema, $document);
218
    }
219
220
    /**
221
     * @param SchemaInterface $schema
222
     * @param DocumentNode    $document
223
     * @param null            $rootValue
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $rootValue is correct as it would always require null to be passed?
Loading history...
Documentation Bug introduced by
Are you sure the doc-type for parameter $contextValue is correct as it would always require null to be passed?
Loading history...
Documentation Bug introduced by
Are you sure the doc-type for parameter $operationName is correct as it would always require null to be passed?
Loading history...
224
     * @param null            $contextValue
225
     * @param array           $variableValues
226
     * @param null            $operationName
227
     * @param callable|null   $fieldResolver
228
     * @return ExecutionResult
229
     */
230
    public static function execute(
231
        SchemaInterface $schema,
232
        DocumentNode $document,
233
        $rootValue = null,
234
        $contextValue = null,
235
        array $variableValues = [],
236
        $operationName = null,
237
        callable $fieldResolver = null
238
    ): ExecutionResult {
239
        return static::make(ExecutionInterface::class)
240
            ->execute(
241
                $schema,
242
                $document,
243
                $rootValue,
244
                $contextValue,
245
                $variableValues,
246
                $operationName,
247
                $fieldResolver
248
            );
249
    }
250
251
    /**
252
     * @param NodeInterface $node
253
     * @return string
254
     */
255
    public static function print(NodeInterface $node): string
256
    {
257
        return static::make(PrinterInterface::class)->print($node);
258
    }
259
260
    /**
261
     * @return Container
262
     */
263
    public function getContainer(): Container
264
    {
265
        return $this->container;
266
    }
267
268
    /**
269
     * Registers the service provides with the container.
270
     *
271
     * @param ContainerInterface $container
272
     */
273
    protected function registerProviders(ContainerInterface $container): void
274
    {
275
        foreach (static::$providers as $className) {
0 ignored issues
show
Bug introduced by
Since $providers is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $providers to at least protected.
Loading history...
276
            $container->addServiceProvider($className);
277
        }
278
    }
279
}
280