nazonohito51 / monkey-patch
Monkey patch for php.
0.1.0
2019-07-22 17:09 UTC
Requires
- nikic/php-parser: ^4.0
Requires (Dev)
- nazonohito51/dependency-analyzer: ^0.2.2
- phpunit/phpunit: ~8
This package is auto-updated.
Last update: 2025-02-06 15:05:55 UTC
README
Monkey patch for php. Transform php code when autoload. (php file itself will be not changed)
Example
When include SomeClass (as below) by autoload or include/require.
<?php namespace SomeNamespace; class SomeClass { public function someMethod() { $client = new \GuzzleHttp\Client(); return $client->request('GET', 'https://your.production.env.com/api/end_point'); } }
You can fix php code.
(new \MonkeyPatch\Patcher())->whenLoad('/path/to/src')->patchBy(new class extends \MonkeyPatch\Filters\AbstractCodeFilter { public function transformCode(string $code): string { // fix url. // 'https://your.production.env.com' -> 'https://your.test.env.com' return preg_replace('/https:\/\/your\.production\.env\.com/', 'https://your.test.env.com', $code); } });
In this case, SomeClass will be included as below. (Of course, original php file will be not changed.)
<?php namespace SomeNamespace; class SomeClass { public function someMethod() { $client = new \GuzzleHttp\Client(); return $client->request('GET', 'https://your.test.env.com/api/end_point'); } }
Usage
(new \MonkeyPatch\Patcher())->whenLoad('/path/to/src')->patchBy(new class extends \MonkeyPatch\Filters\AbstractCodeFilter { public function transformCode(string $code): string { // fix $code by your logic. return $code; } });
If you want to use php-parser, use AbstractAstFilter.
(new \MonkeyPatch\Patcher())->whenLoad('/path/to/src')->patchBy(new class extends \MonkeyPatch\Filters\AbstractAstFilter { protected function getVisitor(): NodeVisitorAbstract { // fix AST by your visitor. return new class extends NodeVisitorAbstract { //... } } });
TODO
- [] one time patch