Giter Site home page Giter Site logo

autoload_patcher's Introduction

Addvilz/autoload_patcher

Purpose of this library

Purpose of this library is to allow patching the source code of class files during runtime.

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).

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

Installation

composer require addvilz/autoload_patcher

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.

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

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

License

Licensed under terms and conditions of Apache 2.0 license.

autoload_patcher's People

Contributors

addvilz avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.