|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of DivineNii opensource projects. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP version 7.4 and above required |
|
9
|
|
|
* |
|
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
11
|
|
|
* @copyright 2019 DivineNii (https://divinenii.com/) |
|
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
|
13
|
|
|
* |
|
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
15
|
|
|
* file that was distributed with this source code. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace Rade\GraphQL; |
|
19
|
|
|
|
|
20
|
|
|
use GraphQL\Language\AST\DocumentNode; |
|
|
|
|
|
|
21
|
|
|
use GraphQL\Language\Parser; |
|
|
|
|
|
|
22
|
|
|
use GraphQL\Language\Source; |
|
|
|
|
|
|
23
|
|
|
use GraphQL\Type\Schema as TypeSchema; |
|
|
|
|
|
|
24
|
|
|
use GraphQL\Utils\AST; |
|
|
|
|
|
|
25
|
|
|
use GraphQL\Utils\BuildSchema; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Definitions of GraphQL schemas. |
|
29
|
|
|
* |
|
30
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
class Schema implements \Countable |
|
33
|
|
|
{ |
|
34
|
|
|
private const CACHE_PREFIX = '.cache.php'; |
|
35
|
|
|
|
|
36
|
|
|
private array $schemas; |
|
37
|
|
|
private int $count = 0; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(array $schemas = [], private $cacheDir = null) |
|
40
|
|
|
{ |
|
41
|
|
|
foreach ($schemas as $name => $schema) { |
|
42
|
|
|
if ($schema instanceof TypeSchema) { |
|
43
|
|
|
$this->schemas[$name] = $schema; |
|
44
|
|
|
++$this->count; |
|
45
|
|
|
|
|
46
|
|
|
continue; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->set($name, $schema); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function count(): int |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->count; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function set(string $name, string|TypeSchema|\Closure $schema): void |
|
62
|
|
|
{ |
|
63
|
|
|
++$this->count; |
|
64
|
|
|
|
|
65
|
|
|
if (null !== $cache = $this->cacheDir) { |
|
66
|
|
|
if (\file_exists($cache = ($cache . $name . self::CACHE_PREFIX))) { |
|
67
|
|
|
$this->schemas[$name] = AST::fromArray(require $cache); |
|
68
|
|
|
|
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$this->schemas[$name] = $schema = $this->resolveSchema($schema instanceof \Closure ? $schema() : $schema, $cache); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function get(string $name): ?TypeSchema |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->schemas[$name] ?? null; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function resolveSchema($schema, ?string $cache): TypeSchema |
|
82
|
|
|
{ |
|
83
|
|
|
if ($schema instanceof TypeSchema) { |
|
84
|
|
|
return $schema; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if ($schema instanceof DocumentNode) { |
|
88
|
|
|
$schema = Parser::parse($schema); |
|
89
|
|
|
} elseif (\is_string($schema)) { |
|
90
|
|
|
if (!\is_file($schema)) { |
|
91
|
|
|
throw new \InvalidArgumentException(\sprintf('Schema file "%s" does not exist.', $schema)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if ('php' === \pathinfo($schema, \PATHINFO_EXTENSION)) { |
|
95
|
|
|
return require $schema; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$schema = Parser::parse(new Source(\file_get_contents($schema), \pathinfo($schema, \PATHINFO_BASENAME))); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if (null !== $cache) { |
|
102
|
|
|
if (!\is_dir($directory = \dirname($cache))) { |
|
103
|
|
|
@\mkdir($directory, 0775, true); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
\file_put_contents($cache, "<?php\nreturn " . \var_export(AST::toArray($schema), true) . ";\n"); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return (new BuildSchema($schema))->buildSchema(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths