Skip to content

Commit ca466f7

Browse files
authored
Merge pull request #6 from okapi-web/develop
Develop
2 parents d58c1b4 + 6100236 commit ca466f7

File tree

3 files changed

+91
-89
lines changed

3 files changed

+91
-89
lines changed

README.md

Lines changed: 85 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,19 @@ composer require okapi/code-transformer
5959

6060
## 📖 List of contents
6161

62-
- [Create a kernel](#create-a-kernel)
63-
- [Create a transformer](#create-a-transformer)
64-
- [Initialize the kernel](#initialize-the-kernel)
62+
- [Create a Kernel](#create-a-kernel)
63+
- [Create a Transformer](#create-a-transformer)
64+
- [Target Class](#target-class)
65+
- [Initialize the Kernel](#initialize-the-kernel)
66+
- [Target Class (transformed)](#target-class-transformed)
6567
- [Result](#result)
6668
- [Limitations](#limitations)
69+
- [How it works](#how-it-works)
70+
- [Testing](#testing)
6771

6872

6973

70-
## Create a kernel
74+
## Create a Kernel
7175

7276
```php
7377
<?php
@@ -82,11 +86,19 @@ class Kernel extends CodeTransformerKernel
8286
StringTransformer::class,
8387
UnPrivateTransformer::class,
8488
];
89+
90+
// Define the settings of the kernel from the "protected" properties
91+
92+
// The directory where the transformed source code will be stored
93+
protected ?string $cacheDir = __DIR__ . '/var/cache';
94+
95+
// The cache file mode
96+
protected ?int $cacheFileMode = 0777;
8597
}
8698
```
8799

88100

89-
## Create a transformer
101+
## Create a Transformer
90102

91103
```php
92104
// String Transformer
@@ -127,7 +139,7 @@ class StringTransformer extends Transformer
127139
&& $node->getStringContentsText() === 'Hello World!'
128140
) {
129141
// Replace it with 'Hello from Code Transformer!'
130-
// Edit method accepts a Token class
142+
// Edit method accepts a Token or Node class
131143
$code->edit(
132144
$node->children,
133145
"'Hello from Code Transformer!'",
@@ -184,59 +196,43 @@ class UnPrivateTransformer extends Transformer
184196
```
185197

186198

187-
## Initialize the kernel
199+
## Target Class
188200

189201
```php
190-
// Initialize the kernel early in the application lifecycle
191-
192202
<?php
193203

194-
use MyKernel;
195-
196-
require_once __DIR__ . '/vendor/autoload.php';
204+
class MyTargetClass
205+
{
206+
private string $myPrivateProperty = "You can't get me!";
197207

198-
$kernel = new MyKernel(
199-
// The directory where the transformed source code will be stored
200-
cacheDir: __DIR__ . '/var/cache',
201-
202-
// The cache file mode
203-
cacheFileMode: 0777,
204-
);
208+
private function myPrivateMethod(): void
209+
{
210+
echo 'Hello World!';
211+
}
212+
}
205213
```
206214

207215

208-
## Result
216+
## Initialize the Kernel
209217

210218
```php
211-
<?php
212-
213-
// Just use your classes as usual
214-
$myTargetClass = new MyTargetClass();
219+
// Initialize the kernel early in the application lifecycle
220+
// Preferably after the autoloader is registered
215221

216-
$myTargetClass->myPrivateProperty; // You can't get me!
217-
$myTargetClass->myPrivateMethod(); // Hello from Code Transformer!
218-
```
222+
<?php
219223

224+
use MyKernel;
220225

221-
```php
222-
// MyTargetClass.php
226+
require_once __DIR__ . '/vendor/autoload.php';
223227

224-
<?php
228+
// Initialize the Code Transformer Kernel
229+
$kernel = MyKernel::init();
230+
```
225231

226-
class MyTargetClass
227-
{
228-
private string $myPrivateProperty = "You can't get me!";
229232

230-
private function myPrivateMethod(): void
231-
{
232-
echo 'Hello World!';
233-
}
234-
}
235-
```
233+
## Target Class (transformed)
236234

237235
```php
238-
// MyTargetClass.php (transformed)
239-
240236
<?php
241237

242238
class MyTargetClass
@@ -252,6 +248,20 @@ $iAmAppended = true;
252248
```
253249

254250

251+
## Result
252+
253+
```php
254+
<?php
255+
256+
// Just use your classes as usual
257+
$myTargetClass = new MyTargetClass();
258+
259+
$myTargetClass->myPrivateProperty; // You can't get me!
260+
$myTargetClass->myPrivateMethod(); // Hello from Code Transformer!
261+
```
262+
263+
264+
255265
# Limitations
256266

257267
- Normally xdebug will point to the original source code, not the transformed
@@ -263,36 +273,47 @@ $iAmAppended = true;
263273

264274
# How it works
265275

266-
- The `Kernel` registers multiple services
276+
- The `CodeTransformerKernel` registers multiple services
267277

268-
- The `TransformerContainer` service stores the list of transformers and their configuration
278+
- The `TransformerManager` service stores the list of transformers and their
279+
configuration
269280

270281
- The `CacheStateManager` service manages the cache state
271282

272-
- The `StreamFilter` service registers a [PHP Stream Filter](https://www.php.net/manual/wrappers.php.php#wrappers.php.filter)
283+
- The `StreamFilter` service registers a
284+
[PHP Stream Filter](https://www.php.net/manual/wrappers.php.php#wrappers.php.filter)
273285
which allows to modify the source code before it is loaded by PHP
274286

275-
- The `AutoloadInterceptor` service overloads the Composer autoloader, which handles the loading of classes
287+
- The `AutoloadInterceptor` service overloads the Composer autoloader,
288+
which handles the loading of classes
276289

277290

278291
## General workflow when a class is loaded
279292

280293
- The `AutoloadInterceptor` service intercepts the loading of a class
281-
- It expects a class file path
282294

283-
- The `TransformerContainer` matches the class name with the list of transformer target classes
295+
- The `TransformerMatcher` matches the class name with the list of transformer
296+
target classes
297+
298+
- If the class is matched, query the cache state to see if the transformed
299+
source code is already cached
284300

285-
- If the class is matched, we query the cache state to see if the transformed source code is already cached
286301
- Check if the cache is valid:
287-
- Modification time of the caching process is less than the modification time of the source file or the transformers
302+
- Modification time of the caching process is less than the modification
303+
time of the source file or the transformers
288304
- Check if the cache file, the source file and the transformers exist
289-
- Check if the number of transformers is the same as the number of transformers in the cache
290-
- If the cache is valid, we load the transformed source code from the cache
291-
- If not, we convert the class file path to a stream filter path
305+
- Check if the number of transformers is the same as the number of
306+
transformers in the cache
307+
308+
- If the cache is valid, load the transformed source code from the cache
309+
- If not, return a stream filter path to the `AutoloadInterceptor` service
292310

293-
- The `StreamFilter` modifies the source code by applying the matching transformers
294-
- If the modified source code is different from the original source code, we cache the transformed source code
295-
- If not, we cache it anyway, but without a cached source file path, so that the transformation process is not repeated
311+
- The `StreamFilter` modifies the source code by applying the matching
312+
transformers
313+
- If the modified source code is different from the original source code,
314+
cache the transformed source code
315+
- If not, cache it anyway, but without a cached source file path,
316+
so that the transformation process is not repeated
296317

297318

298319

@@ -310,6 +331,15 @@ Give a ⭐ if this project helped you!
310331

311332

312333

334+
## 🙏 Thanks
335+
336+
- Big thanks to [lisachenko](https://github.com/lisachenko) for their pioneering
337+
work on the [Go! Aspect-Oriented Framework for PHP](https://github.com/goaop/framework).
338+
This project drew inspiration from their innovative approach and served as a
339+
foundation for this project.
340+
341+
342+
313343
## 📝 License
314344

315345
Copyright © 2023 [Valentin Wotschel](https://github.com/WalterWoshid).<br>

TODO.md

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/CodeTransformerKernel.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ abstract class CodeTransformerKernel
3131
#[Inject]
3232
private Options $options;
3333

34+
/** @internal */
3435
#[Inject]
3536
protected TransformerManager $transformerManager;
3637

@@ -44,7 +45,10 @@ abstract class CodeTransformerKernel
4445
private AutoloadInterceptor $autoloadInterceptor;
4546

4647
/**
47-
* Make the constructor public to allow the DI container to instantiate the class.
48+
* Make the constructor public to allow the DI container to instantiate the
49+
* class.
50+
*
51+
* @internal
4852
*/
4953
public function __construct() {}
5054

@@ -70,7 +74,7 @@ public function __construct() {}
7074

7175
/**
7276
* Enable debug mode. This will disable the cache.
73-
* <br><b>Default:</b> false<br>
77+
* <br><b>Default:</b> {@link false}<br>
7478
*
7579
* @var bool
7680
*/

0 commit comments

Comments
 (0)