|
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\Compiler\System\Provider; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\RuleInterface; |
|
13
|
|
|
use Railt\Reflection\Contracts\Definition; |
|
14
|
|
|
use Railt\Reflection\Contracts\Definition\DirectiveDefinition; |
|
15
|
|
|
use Railt\Reflection\Contracts\Invocation\Behaviour\ProvidesDirectives; |
|
16
|
|
|
use Railt\Reflection\Contracts\Invocation\DirectiveInvocation; |
|
17
|
|
|
use Railt\Reflection\Invocation\Behaviour\HasDirectives; |
|
18
|
|
|
use Railt\SDL\Ast\ProvidesDirectiveNodes; |
|
19
|
|
|
use Railt\SDL\Compiler\System\System; |
|
20
|
|
|
use Railt\SDL\Exception\TypeConflictException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class DirectivesSystem |
|
24
|
|
|
*/ |
|
25
|
|
|
class DirectivesSystem extends System |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @param Definition|HasDirectives $definition |
|
29
|
|
|
* @param RuleInterface $ast |
|
30
|
|
|
*/ |
|
31
|
119 |
|
public function resolve(Definition $definition, RuleInterface $ast): void |
|
32
|
|
|
{ |
|
33
|
119 |
|
if ($definition instanceof ProvidesDirectives && $ast instanceof ProvidesDirectiveNodes) { |
|
34
|
119 |
|
foreach ($ast->getDirectiveNodes() as $child) { |
|
35
|
7 |
|
$this->deferred(function () use ($definition, $child) { |
|
36
|
|
|
/** @var DirectiveInvocation $directive */ |
|
37
|
7 |
|
$directive = $this->process->build($child, $definition); |
|
38
|
|
|
|
|
39
|
7 |
|
$this->linker(function () use ($definition, $directive) { |
|
40
|
7 |
|
$this->validateDefinition($directive); |
|
41
|
|
|
|
|
42
|
7 |
|
$definition->withDirective($directive); |
|
43
|
7 |
|
}); |
|
44
|
7 |
|
}); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
119 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param DirectiveInvocation $directive |
|
51
|
|
|
* @throws \Railt\SDL\Exception\CompilerException |
|
52
|
|
|
*/ |
|
53
|
7 |
|
private function validateDefinition(DirectiveInvocation $directive): void |
|
54
|
|
|
{ |
|
55
|
7 |
|
$definition = $directive->getDefinition(); |
|
56
|
|
|
|
|
57
|
7 |
|
if (! $definition instanceof DirectiveDefinition) { |
|
58
|
|
|
$error = \sprintf('Can not use %s as directive', $definition); |
|
59
|
|
|
throw (new TypeConflictException($error))->in($directive); |
|
60
|
|
|
} |
|
61
|
7 |
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|