1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\SDL; |
11
|
|
|
|
12
|
|
|
use Railt\Io\File; |
13
|
|
|
use Railt\Io\Readable; |
14
|
|
|
use Railt\Reflection\Contracts\Definition; |
15
|
|
|
use Railt\Reflection\Contracts\Document as DocumentInterface; |
16
|
|
|
use Railt\Reflection\Contracts\Reflection as ReflectionInterface; |
17
|
|
|
use Railt\Reflection\Dictionary\CallbackDictionary; |
18
|
|
|
use Railt\Reflection\Document; |
19
|
|
|
use Railt\Reflection\Reflection; |
20
|
|
|
use Railt\SDL\Compiler\Backend; |
21
|
|
|
use Railt\SDL\Compiler\Frontend; |
22
|
|
|
use Railt\SDL\Compiler\Process; |
23
|
|
|
use Railt\SDL\Exception\InternalErrorException; |
24
|
|
|
use Railt\SDL\Exception\SyntaxException; |
25
|
|
|
use Railt\SDL\Exception\TypeException; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class Compiler |
29
|
|
|
*/ |
30
|
|
|
class Compiler implements CompilerInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var ReflectionInterface |
34
|
|
|
*/ |
35
|
|
|
private $reflection; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Frontend |
39
|
|
|
*/ |
40
|
|
|
private $frontend; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Backend |
44
|
|
|
*/ |
45
|
|
|
private $backend; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Process |
49
|
|
|
*/ |
50
|
|
|
private $process; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Compiler constructor. |
54
|
|
|
*/ |
55
|
|
|
public function __construct() |
56
|
|
|
{ |
57
|
|
|
$this->process = new Process(); |
58
|
|
|
$this->frontend = new Frontend(); |
59
|
|
|
$this->backend = new Backend($this->process); |
60
|
|
|
$this->reflection = new Reflection(new CallbackDictionary($this->typeLoader())); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return \Closure |
65
|
|
|
*/ |
66
|
|
|
private function typeLoader(): \Closure |
67
|
|
|
{ |
68
|
|
|
return function (string $type, Definition $from = null) { |
|
|
|
|
69
|
|
|
$this->compile(File::fromPathname($type . '.graphqls')); |
|
|
|
|
70
|
|
|
}; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param Readable $schema |
75
|
|
|
* @return DocumentInterface |
76
|
|
|
* @throws InternalErrorException |
77
|
|
|
* @throws SyntaxException |
78
|
|
|
* @throws TypeException |
79
|
|
|
*/ |
80
|
|
|
public function compile(Readable $schema): DocumentInterface |
81
|
283 |
|
{ |
82
|
|
|
$document = $this->runCompiler($schema); |
83
|
283 |
|
|
84
|
283 |
|
$this->process->run(); |
85
|
283 |
|
|
86
|
283 |
|
return $document; |
87
|
283 |
|
} |
88
|
|
|
|
89
|
283 |
|
/** |
90
|
|
|
* @param Readable $schema |
91
|
283 |
|
* @return DocumentInterface |
92
|
283 |
|
* @throws InternalErrorException |
93
|
|
|
* @throws SyntaxException |
94
|
|
|
* @throws TypeException |
95
|
|
|
*/ |
96
|
|
|
private function runCompiler(Readable $schema): DocumentInterface |
97
|
|
|
{ |
98
|
|
|
return $this->run(clone $this->reflection, $schema); |
99
|
|
|
} |
100
|
283 |
|
|
101
|
|
|
/** |
102
|
|
|
* @param ReflectionInterface $reflection |
103
|
283 |
|
* @param Readable $schema |
104
|
|
|
* @return DocumentInterface |
105
|
|
|
* @throws InternalErrorException |
106
|
|
|
* @throws SyntaxException |
107
|
|
|
* @throws TypeException |
108
|
283 |
|
*/ |
109
|
|
|
private function run(ReflectionInterface $reflection, Readable $schema): DocumentInterface |
110
|
|
|
{ |
111
|
|
|
return $this->backend->each( |
112
|
|
|
$this->backend->context($this->document($reflection, $schema)), |
113
|
|
|
$this->frontend->exec($schema) |
114
|
|
|
); |
115
|
283 |
|
} |
116
|
|
|
|
117
|
283 |
|
/** |
118
|
283 |
|
* @param ReflectionInterface $reflection |
119
|
|
|
* @param Readable $schema |
120
|
|
|
* @return DocumentInterface |
121
|
283 |
|
*/ |
122
|
283 |
|
private function document(ReflectionInterface $reflection, Readable $schema): DocumentInterface |
123
|
|
|
{ |
124
|
|
|
return new Document($reflection, $schema); |
125
|
283 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param Readable $schema |
129
|
|
|
* @return ReflectionInterface |
130
|
|
|
* @throws InternalErrorException |
131
|
|
|
* @throws SyntaxException |
132
|
283 |
|
* @throws TypeException |
133
|
|
|
*/ |
134
|
283 |
|
public function preload(Readable $schema): ReflectionInterface |
135
|
|
|
{ |
136
|
|
|
$this->run($this->reflection, $schema); |
137
|
|
|
|
138
|
|
|
return $this->reflection; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..