|
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\Compiler\Reader\Resolver; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Compiler\Exception\UnknownPragmaException; |
|
13
|
|
|
use Railt\Compiler\Reader\Resolver\PragmaResolver\ConfigResolver; |
|
14
|
|
|
use Railt\Io\Readable; |
|
15
|
|
|
use Railt\Lexer\TokenInterface; |
|
16
|
|
|
use Railt\Parser\Configuration; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class PragmaResolver |
|
20
|
|
|
*/ |
|
21
|
|
|
class PragmaResolver implements ResolverInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private const PARSER_RESOLVER = 'parser'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array|ConfigResolver[] |
|
30
|
|
|
*/ |
|
31
|
|
|
private $resolvers; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private $configs = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* PragmaResolver constructor. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->bootResolvers(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
private function bootResolvers(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$this->resolvers = [ |
|
52
|
|
|
self::PARSER_RESOLVER => new ConfigResolver(self::PARSER_RESOLVER, [ |
|
53
|
|
|
Configuration::PRAGMA_ROOT, |
|
54
|
|
|
Configuration::PRAGMA_LOOKAHEAD, |
|
55
|
|
|
Configuration::PRAGMA_RUNTIME |
|
56
|
|
|
]) |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param Readable $readable |
|
62
|
|
|
* @param TokenInterface $token |
|
63
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
|
64
|
|
|
*/ |
|
65
|
|
|
public function resolve(Readable $readable, TokenInterface $token): void |
|
66
|
|
|
{ |
|
67
|
|
|
[$name, $value] = [$token->value(1), $token->value(2)]; |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
foreach ($this->resolvers as $group => $resolver) { |
|
70
|
|
|
if ($resolver->match($name)) { |
|
|
|
|
|
|
71
|
|
|
$name = $this->resolvePragmaName($readable, $token, $resolver->resolve($name)); |
|
|
|
|
|
|
72
|
|
|
$this->set($group, $name, $value); |
|
73
|
|
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $group |
|
80
|
|
|
* @param string $name |
|
81
|
|
|
* @param string $value |
|
82
|
|
|
*/ |
|
83
|
|
|
private function set(string $group, string $name, string $value): void |
|
84
|
|
|
{ |
|
85
|
|
|
if (! \array_key_exists($group, $this->configs)) { |
|
86
|
|
|
$this->configs[$group] = []; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->configs[$group][$name] = $value; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param Readable $readable |
|
94
|
|
|
* @param TokenInterface $token |
|
95
|
|
|
* @param null|string $name |
|
96
|
|
|
* @return string |
|
97
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
|
98
|
|
|
*/ |
|
99
|
|
|
private function resolvePragmaName(Readable $readable, TokenInterface $token, ?string $name): string |
|
100
|
|
|
{ |
|
101
|
|
|
if ($name === null) { |
|
102
|
|
|
$error = \vsprintf('Unknown configuration pragma rule "%s" with value "%s"', [ |
|
103
|
|
|
$token->value(1), |
|
104
|
|
|
$token->value(2), |
|
105
|
|
|
]); |
|
106
|
|
|
|
|
107
|
|
|
throw (new UnknownPragmaException($error))->throwsIn($readable, $token->offset()); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $name; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param string $group |
|
115
|
|
|
* @return iterable |
|
116
|
|
|
*/ |
|
117
|
|
|
public function all(string $group): iterable |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->configs[$group] ?? []; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$xwould be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.