-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCodeTransformerKernel.php
269 lines (227 loc) · 6.9 KB
/
CodeTransformerKernel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
namespace Okapi\CodeTransformer;
use Closure;
use DI\Attribute\Inject;
use Okapi\CodeTransformer\Core\AutoloadInterceptor;
use Okapi\CodeTransformer\Core\Cache\CacheStateManager;
use Okapi\CodeTransformer\Core\CachedStreamFilter;
use Okapi\CodeTransformer\Core\Container\TransformerManager;
use Okapi\CodeTransformer\Core\DI;
use Okapi\CodeTransformer\Core\Exception\Kernel\DirectKernelInitializationException;
use Okapi\CodeTransformer\Core\Options;
use Okapi\CodeTransformer\Core\Options\Environment;
use Okapi\CodeTransformer\Core\StreamFilter;
use Okapi\Singleton\Singleton;
/**
* # Code Transformer Kernel
*
* This class is the heart of the Code Transformer library.
* It manages an environment for Code Transformation.
*
* 1. Extend this class and define a list of transformers in the
* {@link $transformers} property.
* 2. Call the {@link init()} method early in the application lifecycle.
*
* If you want to modify the kernel options dynamically, override the
* {@link configureOptions()} method.
*/
abstract class CodeTransformerKernel
{
use Singleton;
// region DI
#[Inject]
private Options $options;
/** @internal */
#[Inject]
protected TransformerManager $transformerManager;
#[Inject]
private CacheStateManager $cacheStateManager;
#[Inject]
private StreamFilter $streamFilter;
#[Inject]
private CachedStreamFilter $cachedStreamFilter;
#[Inject]
private AutoloadInterceptor $autoloadInterceptor;
/**
* Make the constructor public to allow the DI container to instantiate the
* class.
*
* @internal
*/
public function __construct() {}
// endregion
// region Settings
/**
* The cache directory.
* <br><b>Default:</b> ROOT_DIR/cache/code-transformer<br>
*
* @var string|null
*/
protected ?string $cacheDir = null;
/**
* The exclude paths. Paths/directories in this array will be excluded
*
* @var array
*/
protected array $excludePaths = [];
/**
* The cache file mode.
* <br><b>Default:</b> 0777 & ~{@link umask()}<br>
*
* @var int|null
*/
protected ?int $cacheFileMode = null;
/**
* Enable debug mode. This will disable the cache.
* <br><b>Default:</b> {@link false}<br>
*
* @var bool
*/
protected bool $debug = false;
/**
* The environment in which the application is running.
* <br><b>Default:</b> {@link Environment::DEVELOPMENT}<br><br>
*
* If {@link Environment::PRODUCTION}, the cache will not be checked for
* updates (better performance).<br>
* If {@link Environment::DEVELOPMENT}, the cache will be checked for
* updates (better development experience).
*
* @var Environment
*/
protected Environment $environment = Environment::DEVELOPMENT;
/**
* Throw an exception if the kernel is initialized twice.
* <br><b>Default:</b> {@link false}<br>
*
* If {@link false}, any subsequent call to {@link init()} will be
* ignored.
*
* @var bool
*/
protected bool $throwExceptionOnDoubleInitialization = false;
// endregion
/**
* List of transformers to be applied.
*
* @var class-string<Transformer>[]
*/
protected array $transformers = [];
/**
* Resolve instance with dependency injection.
*
* @inheritDoc
*/
public static function getInstance(): static
{
if (!isset(static::$instance)) {
static::registerDependencyInjection();
static::$instance = DI::get(static::class);
}
return static::$instance;
}
/**
* Initialize the kernel.
*
* @return void
*/
public static function init(): void
{
static::ensureNotKernelNamespace();
$instance = static::getInstance();
if ($instance->throwExceptionOnDoubleInitialization) {
$instance->ensureNotInitialized();
} elseif ($instance->initialized) {
return;
}
// Initialize the services
$instance->preInit();
$instance->configureOptions();
$instance->registerServices();
$instance->registerAutoloadInterceptor();
$instance->setInitialized();
}
/**
* Internal dependency injection.
*
* @return void
*/
protected static function registerDependencyInjection(): void
{
DI::getInstance()->register();
}
/**
* Pre-initialize the services.
*
* @return void
*/
protected function preInit(): void
{
// Set options
$this->options->setOptions(
cacheDir: $this->cacheDir,
cacheFileMode: $this->cacheFileMode,
debug: $this->debug,
environment: $this->environment,
excludePaths: $this->excludePaths,
);
// Add the transformers
$this->transformerManager->addTransformers($this->transformers);
}
/**
* Configure or modify kernel options.
*
* @return void
*/
protected function configureOptions(): void
{
// Override this method to configure the options dynamically
}
/**
* Custom dependency injection handler.
*
* Pass a closure that takes a transformer class name as the argument and
* returns a transformer instance.
*
* @return null|Closure(class-string<Transformer>): Transformer
*/
protected function dependencyInjectionHandler(): ?Closure
{
// Override this method to configure the dependency injection handler
return null;
}
protected function registerServices(): void
{
$this->options->register();
$this->transformerManager->registerCustomDependencyInjectionHandler(
$this->dependencyInjectionHandler(),
);
$this->transformerManager->register();
$this->cacheStateManager->register();
$this->streamFilter->register();
$this->cachedStreamFilter->register();
}
/**
* Register the autoload interceptor.
*
* @return void
*/
protected function registerAutoloadInterceptor(): void
{
// Overload the composer class loaders
$this->autoloadInterceptor->register();
}
/**
* Make sure that the kernel is not called from this class.
*
* @return void
*/
protected static function ensureNotKernelNamespace(): void
{
// Get current namespace and class name
$namespace = get_called_class();
if ($namespace === CodeTransformerKernel::class) {
throw new DirectKernelInitializationException;
}
}
}