Download the PHP package okapi/code-transformer without Composer
On this page you can find all versions of the php package okapi/code-transformer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download okapi/code-transformer
More information about okapi/code-transformer
Files in okapi/code-transformer
Package code-transformer
Short Description PHP Code Transformer is a PHP library that allows you to modify and transform the source code of a loaded PHP class.
License MIT
Homepage https://github.com/okapi-web/php-code-transformer
Informations about the package code-transformer
PHP Code Transformer
PHP Code Transformer is a PHP library that allows you to modify and transform the source code of a loaded PHP class.
Installation
Usage
📖 List of contents
- Create a Kernel
- Create a Transformer
- Target Class
- Initialize the Kernel
- Target Class (transformed)
- Result
- Limitations
- How it works
- Testing
Create a Kernel
Create a Transformer
Target Class
Initialize the Kernel
Target Class (transformed)
Result
Limitations
- Normally xdebug will point to the original source code, not the transformed
one. The problem with this is if you add or remove a line of code, xdebug
will point to the wrong line, so try to keep the number of lines the same
as the original source code.
How it works
-
The CodeTransformerKernel
registers multiple services
-
The TransformerManager
service stores the list of transformers and their
configuration
-
The CacheStateManager
service manages the cache state
-
The StreamFilter
service registers a
PHP Stream Filter
which allows to modify the source code before it is loaded by PHP
- The
AutoloadInterceptor
service overloads the Composer autoloader,
which handles the loading of classes
General workflow when a class is loaded
-
The AutoloadInterceptor
service intercepts the loading of a class
-
The TransformerMatcher
matches the class name with the list of transformer
target classes
-
If the class is matched, query the cache state to see if the transformed
source code is already cached
- Check if the cache is valid:
- Modification time of the caching process is less than the modification
time of the source file or the transformers
- Check if the cache file, the source file and the transformers exist
-
Check if the number of transformers is the same as the number of
transformers in the cache
- If the cache is valid, load the transformed source code from the cache
- If not, return a stream filter path to the
AutoloadInterceptor
service
- The
StreamFilter
modifies the source code by applying the matching
transformers
- If the modified source code is different from the original source code,
cache the transformed source code
- If not, cache it anyway, but without a cached source file path,
so that the transformation process is not repeated
Testing
- Run
composer run-script test
or
- Run
composer run-script test-coverage
Show your support
The CodeTransformerKernel
registers multiple services
-
The
TransformerManager
service stores the list of transformers and their configuration -
The
CacheStateManager
service manages the cache state -
The
StreamFilter
service registers a PHP Stream Filter which allows to modify the source code before it is loaded by PHP - The
AutoloadInterceptor
service overloads the Composer autoloader, which handles the loading of classes
The AutoloadInterceptor
service intercepts the loading of a class
The TransformerMatcher
matches the class name with the list of transformer
target classes
If the class is matched, query the cache state to see if the transformed source code is already cached
- Check if the cache is valid:
- Modification time of the caching process is less than the modification time of the source file or the transformers
- Check if the cache file, the source file and the transformers exist
-
Check if the number of transformers is the same as the number of transformers in the cache
- If the cache is valid, load the transformed source code from the cache
- If not, return a stream filter path to the
AutoloadInterceptor
service
StreamFilter
modifies the source code by applying the matching
transformers
- If the modified source code is different from the original source code, cache the transformed source code
- If not, cache it anyway, but without a cached source file path, so that the transformation process is not repeated
composer run-script test
or
composer run-script test-coverage
Give a ⭐ if this project helped you!
🙏 Thanks
- Big thanks to lisachenko for their pioneering work on the Go! Aspect-Oriented Framework for PHP. This project drew inspiration from their innovative approach and served as a foundation for this project.
📝 License
Copyright © 2023 Valentin Wotschel.
This project is MIT licensed.
All versions of code-transformer with dependencies
microsoft/tolerant-php-parser Version ^0.1.2
okapi/filesystem Version ^1.0
okapi/path Version ^1.0
okapi/singleton Version ^1.0
okapi/wildcards Version ^1.0
php-di/php-di Version ^7.0
roave/better-reflection Version ^6.8