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\Definition; |
11
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\RuleInterface; |
13
|
|
|
use Railt\Parser\Environment; |
14
|
|
|
use Railt\Reflection\AbstractTypeDefinition; |
15
|
|
|
use Railt\Reflection\Contracts\Definition\TypeDefinition; |
16
|
|
|
use Railt\Reflection\Contracts\Dictionary; |
17
|
|
|
use Railt\Reflection\Document; |
18
|
|
|
use Railt\SDL\Compiler\Pipeline; |
19
|
|
|
use Railt\SDL\Compiler\Value\StringValue; |
20
|
|
|
use Railt\SDL\Exception\TypeConflictException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class TypeDefinitionDelegate |
24
|
|
|
*/ |
25
|
|
|
abstract class TypeDefinitionDelegate extends DefinitionDelegate |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param Environment $env |
29
|
|
|
*/ |
30
|
|
|
public function boot(Environment $env): void |
31
|
|
|
{ |
32
|
|
|
parent::boot($env); |
33
|
|
|
|
34
|
|
|
$this->transaction($this->definition, function () { |
35
|
|
|
$this->register(); |
36
|
|
|
}); |
37
|
|
|
|
38
|
|
|
$this->future(Pipeline::PRIORITY_DEFINITION, function () { |
39
|
|
|
$this->addDescription($this->definition, $this); |
40
|
|
|
}); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
45
|
|
|
* @throws \Railt\Reflection\Exception\TypeNotFoundException |
46
|
|
|
*/ |
47
|
|
|
private function register(): void |
48
|
|
|
{ |
49
|
|
|
/** @var Document $document */ |
50
|
|
|
$document = $this->definition->getDocument(); |
51
|
|
|
|
52
|
|
|
$this->verifyDuplication($this->definition, $document->getDictionary()); |
53
|
|
|
|
54
|
|
|
$document->withDefinition($this->definition); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param TypeDefinition $type |
60
|
|
|
* @param Dictionary $dict |
61
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
62
|
|
|
* @throws \Railt\Reflection\Exception\TypeNotFoundException |
63
|
|
|
*/ |
64
|
|
|
private function verifyDuplication(TypeDefinition $type, Dictionary $dict): void |
65
|
|
|
{ |
66
|
|
|
if ($dict->has($type->getName())) { |
67
|
|
|
$prev = $dict->get($type->getName(), $type); |
68
|
|
|
$error = 'Could not redeclare type %s by %s'; |
69
|
|
|
$error = \sprintf($error, $prev, $type); |
70
|
|
|
|
71
|
|
|
throw $this->error(new TypeConflictException($error)); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param TypeDefinition|AbstractTypeDefinition $type |
77
|
|
|
* @param RuleInterface $rule |
78
|
|
|
*/ |
79
|
|
|
protected function addDescription(TypeDefinition $type, RuleInterface $rule): void |
80
|
|
|
{ |
81
|
|
|
/** @var RuleInterface $description */ |
82
|
|
|
$description = $rule->first('Description', 1); |
83
|
|
|
|
84
|
|
|
if ($description) { |
85
|
|
|
$type->withDescription($this->parseString($description)); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param RuleInterface $rule |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
private function parseString(RuleInterface $rule): string |
94
|
|
|
{ |
95
|
|
|
$value = new StringValue($rule->getChild(0)); |
|
|
|
|
96
|
|
|
|
97
|
|
|
return $value->toScalar(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: