1. Go to this page and download the library: Download okapi/code-transformer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
okapi / code-transformer example snippets
use Okapi\CodeTransformer\CodeTransformerKernel;
// Extend from the "CodeTransformerKernel" class
class Kernel extends CodeTransformerKernel
{
// Define a list of transformer classes
protected array $transformers = [
StringTransformer::class,
UnPrivateTransformer::class,
];
// Define the settings of the kernel from the "protected" properties
// The directory where the transformed source code will be stored
protected ?string $cacheDir = __DIR__ . '/var/cache';
// The cache file mode
protected ?int $cacheFileMode = 0777;
}
// String Transformer
use Okapi\CodeTransformer\Transformer;
use Okapi\CodeTransformer\Transformer\Code;
// Extend from the "Transformer" class
class StringTransformer extends Transformer
{
// Define the target class(es)
public function getTargetClass(): string|array
{
// You can specify a single class or an array of classes
// You can also use wildcards, see https://github.com/okapi-web/php-wildcards
return MyTargetClass::class;
}
// The "transform" method will be called when the target class is loaded
// Here you can modify the source code of the target class(es)
public function transform(Code $code): void
{
// I recommend using the Microsoft\PhpParser library to parse the source
// code. It's already o from Code Transformer!'",
);
// You can also manually edit the source code
$code->editAt(
$node->getStartPosition() + 1,
$node->getWidth() - 2,
"Hello from Code Transformer!",
);
// Append a new line of code
$code->append('$iAmAppended = true;');
}
}
}
}
// UnPrivate Transformer
namespace Okapi\CodeTransformer\Tests\Stubs\Transformer;
use Microsoft\PhpParser\TokenKind;
use Okapi\CodeTransformer\Transformer;
use Okapi\CodeTransformer\Transformer\Code;
// Replace all "private" keywords with "public"
class UnPrivateTransformer extends Transformer
{
public function getTargetClass(): string|array
{
return MyTargetClass::class;
}
public function transform(Code $code): void
{
$sourceFileNode = $code->getSourceFileNode();
// Iterate over all tokens
foreach ($sourceFileNode->getDescendantTokens() as $token) {
// Find "private" keyword
if ($token->kind === TokenKind::PrivateKeyword) {
// Replace it with "public"
$code->edit($token, 'public');
}
}
}
}
class MyTargetClass
{
private string $myPrivateProperty = "You can't get me!";
private function myPrivateMethod(): void
{
echo 'Hello World!';
}
}
// Initialize the kernel early in the application lifecycle
// Preferably after the autoloader is registered
use MyKernel;
class MyTargetClass
{
public string $myPrivateProperty = "You can't get me!";
public function myPrivateMethod(): void
{
echo 'Hello from Code Transformer!';
}
}
$iAmAppended = true;
// Just use your classes as usual
$myTargetClass = new MyTargetClass();
$myTargetClass->myPrivateProperty; // You can't get me!
$myTargetClass->myPrivateMethod(); // Hello from Code Transformer!
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.