1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Tokenizer\Config; |
6
|
|
|
|
7
|
|
|
use Spiral\Core\InjectableConfig; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Tokenizer component configuration. |
11
|
|
|
* |
12
|
|
|
* @psalm-type TDirectories = array<array-key, string> |
13
|
|
|
* |
14
|
|
|
* @psalm-type TScope = array{ |
15
|
|
|
* "directories": TDirectories, |
16
|
|
|
* "exclude": TDirectories |
17
|
|
|
* } |
18
|
|
|
*/ |
19
|
|
|
final class TokenizerConfig extends InjectableConfig |
20
|
|
|
{ |
21
|
|
|
public const CONFIG = 'tokenizer'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array{ |
|
|
|
|
25
|
|
|
* cache: array{directory: null, enabled: bool}, |
26
|
|
|
* load: array{classes:bool, enums: bool, interfaces: bool}, |
27
|
|
|
* debug: bool, |
28
|
|
|
* directories: TDirectories, |
29
|
|
|
* exclude: TDirectories, |
30
|
|
|
* scopes: array<non-empty-string, TScope> |
31
|
|
|
* } |
32
|
|
|
*/ |
33
|
|
|
protected array $config = [ |
34
|
|
|
'cache' => [ |
35
|
|
|
'directory' => null, |
36
|
|
|
'enabled' => false, |
37
|
|
|
], |
38
|
|
|
'load' => [ |
39
|
|
|
'classes' => true, |
40
|
|
|
'enums' => false, |
41
|
|
|
'interfaces' => false, |
42
|
|
|
], |
43
|
|
|
'debug' => false, |
44
|
|
|
'directories' => [], |
45
|
|
|
'exclude' => [], |
46
|
|
|
'scopes' => [], |
47
|
|
|
]; |
48
|
|
|
|
49
|
439 |
|
public function isDebug(): bool |
50
|
|
|
{ |
51
|
439 |
|
return (bool)($this->config['debug'] ?? false); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return TDirectories |
|
|
|
|
56
|
|
|
*/ |
57
|
434 |
|
public function getDirectories(): array |
58
|
|
|
{ |
59
|
434 |
|
return $this->config['directories'] ?? [(string)\getcwd()]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return TDirectories |
64
|
|
|
*/ |
65
|
440 |
|
public function getExcludes(): array |
66
|
|
|
{ |
67
|
440 |
|
return $this->config['exclude'] ?? ['vendor', 'tests']; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return TScope |
|
|
|
|
72
|
|
|
*/ |
73
|
21 |
|
public function getScope(string $scope): array |
74
|
|
|
{ |
75
|
21 |
|
$directories = $this->config['scopes'][$scope]['directories'] ?? $this->getDirectories(); |
76
|
21 |
|
$excludes = $this->config['scopes'][$scope]['exclude'] ?? $this->getExcludes(); |
77
|
|
|
|
78
|
21 |
|
return [ |
79
|
21 |
|
'directories' => $directories, |
80
|
21 |
|
'exclude' => $excludes, |
81
|
21 |
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function getScopes(): array |
85
|
|
|
{ |
86
|
1 |
|
return $this->config['scopes'] ?? []; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Check if tokenizer listeners cache is enabled. |
91
|
|
|
*/ |
92
|
403 |
|
public function isCacheEnabled(): bool |
93
|
|
|
{ |
94
|
403 |
|
return (bool)($this->config['cache']['enabled'] ?? false); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get tokenizer listeners cache directory. |
99
|
|
|
*/ |
100
|
403 |
|
public function getCacheDirectory(): ?string |
101
|
|
|
{ |
102
|
403 |
|
$dir = $this->config['cache']['directory'] ?? null; |
103
|
403 |
|
\assert(\is_string($dir) || $dir === null, 'Invalid cache directory.'); |
104
|
|
|
|
105
|
403 |
|
return $dir; |
106
|
|
|
} |
107
|
|
|
|
108
|
401 |
|
public function isLoadClassesEnabled(): bool |
109
|
|
|
{ |
110
|
401 |
|
return (bool)($this->config['load']['classes'] ?? true); |
111
|
|
|
} |
112
|
|
|
|
113
|
401 |
|
public function isLoadEnumsEnabled(): bool |
114
|
|
|
{ |
115
|
401 |
|
return (bool)($this->config['load']['enums'] ?? false); |
116
|
|
|
} |
117
|
|
|
|
118
|
401 |
|
public function isLoadInterfacesEnabled(): bool |
119
|
|
|
{ |
120
|
401 |
|
return (bool)($this->config['load']['interfaces'] ?? false); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|