Download the PHP package macfja/composer-class-rewrite without Composer
On this page you can find all versions of the php package macfja/composer-class-rewrite. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download macfja/composer-class-rewrite
More information about macfja/composer-class-rewrite
Files in macfja/composer-class-rewrite
Package composer-class-rewrite
Short Description Composer plugin to handle class rewrite
License MIT
Informations about the package composer-class-rewrite
Composer Class-Rewrite
What is Composer Class-Rewrite
Composer Class-Rewrite is a Composer plugin that allow you to rewrite almost1 any classes of your project.
Principle2
The idea is to scan every classes of the project to find classes declared as a rewrite. Then we make some modification on the parent (rewritten) class (a copy of the parent class) and the rewriter class (a copy of the rewriter class) and finally add them to the Composer autoload (before PSR-0 and PSR-4 classes). So, when a class ask for the rewritten class, Composer will return our modified rewiter class.
Installation
With Composer:
Usage
Limitations
- Only work for PSR-0 and PSR-4 namespace.
- Only work if Composer is the first autoloader.
- Only work on non-dev dependency.
- Only work on class (not on trait, nor on interface).
- The rewriter class MUST have the same namespace than the rewritten class.
- You have to rebuild the autoload for every rewriter/rewritten changes.
- Have some side effects (see example below)
- Class context is changed (magic constants).
- Multiple call if you instanciate the rewriter class.
Side effects
Multiple call
`
`
Magic constants
Explored ideas
- Inject my code during the Composer autoloading. Issue: Composer don't provide event in autoloader.
- Change the Composer autoloader code to add my logic. Issue: Change core code. So if Composer change it, he have to change mine too. Hard to maintain.
- Prepend a customer autoloader before Composer autoloader. Issue: Loose all the power of Composer autloading.
Under the hood
This plugin use:
composer-plugin-api
(obvious)nikic/php-parser
: For parsing php class, and rewrite them
How it works
Just before Composer autoloader generation, the plugin is parsing every PSR-0 and PSR-4 namespace. It search for class that implement the interface \MacFJA\ClassRewrite\Rewriter
, store in memory the the rewriter class and the rewritten class.
When the plugin have parse every classes. It rebuild every Rewriter and Rewritten class for rename the Rewritten classname with a hard to guess class name (in fact, it's a sha1 of the source file) and to rename the Rewriter classname into the original Rewritten classname.
Finally it add the rewrite destination directory into Composer classmap autoload.
Then it let Composer do it's stuff.
It's works because Composer start searching class in the classmap autoload.
License
The MIT License (MIT). Please see License File for more information.