2017 © Pedro Peláez
 

library autoload_patcher

Patch any class file before autoloading it

image

addvilz/autoload_patcher

Patch any class file before autoloading it

  • Wednesday, February 10, 2016
  • by addvilz
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1,444 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 2 % Grown

The README.md

Addvilz/autoload_patcher

Purpose of this library

Purpose of this library is to allow patching the source code of class files during runtime., (*1)

There are some cases when you need to modify functionality of vendor code but you are not able to replace the code, or extend it (static calls from other vendor code is a good example)., (*2)

This library allows you to replace, augment or otherwise change original source code and eval it afterwards., (*3)

Installation

composer require addvilz/autoload_patcher, (*4)

Example use:

$loader = require 'vendor/autoload.php';
$patcher = new \Addvilz\AutoloadPatcher\Patcher($loader);
$patcher
    ->register()
    ->addPatcher('Some\Vendor\UtilityClass', function ($code) {

        // Let's rename the class
        $code = str_replace(
            'class UtilityClass',
            'class VendorUtilityClass',
            $code
        );

        // ... More modification here, runtime code generation, etc.

        return $code;
    });

How does it work?

Patcher->register() method call appends an autoload callback to autoload stack. This means, all class loading will be proxied tru Patcher instance, and if patcher callback for given class is registered, it will be executed against source code from file determined by Composer ClassLoader class., (*5)

NB: Before the source code of the class is passed to the callback function, opening PHP tags are removed!, (*6)

NB: Whatever the patcher callback function returns is passed on to eval()., (*7)

License

Licensed under terms and conditions of Apache 2.0 license., (*8)

The Versions

10/02 2016

dev-master

9999999-dev

Patch any class file before autoloading it

  Sources   Download

Apache-2.0

10/02 2016

v1.0.0

1.0.0.0

Patch any class file before autoloading it

  Sources   Download

Apache-2.0