@@ -73,6 +73,7 @@ composer require okapi/code-transformer
7373
7474use Okapi\CodeTransformer\CodeTransformerKernel;
7575
76+ // Extend from the "CodeTransformerKernel" class
7677class Kernel extends CodeTransformerKernel
7778{
7879 // Define a list of transformer classes
@@ -94,23 +95,29 @@ class Kernel extends CodeTransformerKernel
9495use Okapi\CodeTransformer\Service\StreamFilter\Metadata\Code;
9596use Okapi\CodeTransformer\Transformer;
9697
98+ // Extend from the "Transformer" class
9799class StringTransformer extends Transformer
98100{
101+ // Define the target class(es)
99102 public function getTargetClass(): string|array
100103 {
101104 // You can specify a single class or an array of classes
102105 // You can also use wildcards, see https://github.com/okapi-web/php-wildcards
103106 return MyTargetClass::class;
104107 }
105108
109+ // The "transform" method will be called when the target class is loaded
110+ // Here you can modify the source code of the target class(es)
106111 public function transform(Code $code): void
107112 {
108- // Get the source file node which contains all the nodes of the source code
109- $sourceFileNode = $code->sourceFileNode;
113+ // I recommend using the Microsoft\PhpParser library to parse the source
114+ // code. It's already included in the dependencies of this package and
115+ // the "$code->sourceFileNode" property contains the parsed source code.
116+
117+ // But you can also use any other library or manually parse the source
118+ // code with basic PHP string functions and "$code->getOriginalSource()"
110119
111- // I recommend using the Microsoft\PhpParser library to parse the source code,
112- // but you can also use any other library or manually parse the source code
113- // or just use basic PHP functions with `$code->getOriginalSource()`
120+ $sourceFileNode = $code->sourceFileNode;
114121
115122 // Iterate over all nodes
116123 foreach ($sourceFileNode->getDescendantNodes() as $node) {
@@ -127,9 +134,9 @@ class StringTransformer extends Transformer
127134
128135 // You can also manually edit the source code
129136 $code->editAt(
130- $node->getStartPosition(),
131- $node->getWidth(),
132- "' Hello from Code Transformer!' ",
137+ $node->getStartPosition() + 1 ,
138+ $node->getWidth() - 2 ,
139+ "Hello from Code Transformer!",
133140 );
134141
135142 // Append a new line of code
@@ -151,6 +158,7 @@ use Microsoft\PhpParser\TokenKind;
151158use Okapi\CodeTransformer\Service\StreamFilter\Metadata\Code;
152159use Okapi\CodeTransformer\Transformer;
153160
161+ // Replace all "private" keywords with "public"
154162class UnPrivateTransformer extends Transformer
155163{
156164 public function getTargetClass(): string|array
@@ -164,8 +172,9 @@ class UnPrivateTransformer extends Transformer
164172
165173 // Iterate over all tokens
166174 foreach ($sourceFileNode->getDescendantTokens() as $token) {
167- // Replace all private keywords with public
175+ // Find " private" keyword
168176 if ($token->kind === TokenKind::PrivateKeyword) {
177+ // Replace it with "public"
169178 $code->edit($token, 'public');
170179 }
171180 }
@@ -264,9 +273,10 @@ $iAmAppended = true;
264273- The ` TransformerContainer ` matches the class name with the list of transformer target classes
265274
266275- If the class is matched, we query the cache state to see if the transformed source code is already cached
267- - We check if the cache is valid
276+ - Check if the cache is valid:
268277 - Modification time of the caching process is less than the modification time of the source file or the transformers
269278 - Check if the cache file, the source file and the transformers exist
279+ - Check if the number of transformers is the same as the number of transformers in the cache
270280 - If the cache is valid, we load the transformed source code from the cache
271281 - If not, we convert the class file path to a stream filter path
272282
0 commit comments