|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace RulerZ\Target; |
|
6
|
|
|
|
|
7
|
|
|
use RulerZ\Compiler\Context; |
|
8
|
|
|
use RulerZ\Compiler\CompilationTarget; |
|
9
|
|
|
use RulerZ\Model; |
|
10
|
|
|
use RulerZ\Target\Operators\Definitions as OperatorsDefinitions; |
|
11
|
|
|
use RulerZ\Target\Operators\Definitions; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Generic visitor intended to be extended. |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class AbstractCompilationTarget implements CompilationTarget |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var OperatorsDefinitions |
|
20
|
|
|
*/ |
|
21
|
|
|
private $customOperators; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Create a rule visitor for a given compilation context. |
|
25
|
|
|
* |
|
26
|
|
|
* @param Context $context The compilation context. |
|
27
|
|
|
* |
|
28
|
|
|
* @return \RulerZ\Compiler\RuleVisitor |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract protected function createVisitor(Context $context); |
|
31
|
|
|
|
|
32
|
|
|
abstract protected function getExecutorTraits(); |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param array<callable> $operators A list of additional operators to register. |
|
36
|
|
|
* @param array<callable> $inlineOperators A list of additional inline operators to register. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(array $operators = [], array $inlineOperators = []) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->customOperators = new OperatorsDefinitions($operators, $inlineOperators); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function compile(Model\Rule $rule, Context $compilationContext): Model\Executor |
|
47
|
|
|
{ |
|
48
|
|
|
$visitor = $this->createVisitor($compilationContext); |
|
49
|
|
|
$compiledCode = $visitor->visit($rule); |
|
50
|
|
|
|
|
51
|
|
|
return new Model\Executor( |
|
52
|
|
|
$this->getExecutorTraits(), |
|
53
|
|
|
$compiledCode, |
|
54
|
|
|
$visitor->getCompilationData() |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function createCompilationContext($target): Context |
|
62
|
|
|
{ |
|
63
|
|
|
return new Context(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function defineOperator(string $name, callable $transformer): void |
|
70
|
|
|
{ |
|
71
|
|
|
$this->customOperators->defineOperator($name, $transformer); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function defineInlineOperator(string $name, callable $transformer): void |
|
78
|
|
|
{ |
|
79
|
|
|
$this->customOperators->defineInlineOperator($name, $transformer); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getOperators(): Definitions |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->customOperators; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getRuleIdentifierHint(string $rule, Context $context): string |
|
94
|
|
|
{ |
|
95
|
|
|
return ''; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|