jgswift/recompilr

runtime class compiler

0.1.1 2014-09-28 06:30 UTC

This package is not auto-updated.

Last update: 2024-12-17 04:02:33 UTC


README

PHP 5.5+ factory runtime class compiler

Build Status Scrutinizer Code Quality Latest Stable Version License Coverage Status

Description

recompilr uses a class definition and recompiles it at runtime using eval and a unique hash identifier. Classes may be recompiled after changes are made to the class definition without requiring the application to restart. This effectively allows an application to redeclare classes at runtime.

Installation

Install via cli using composer:

php composer.phar require jgswift/recompilr:0.1.*

Install via composer.json using composer:

{
    "require": {
        "jgswift/recompilr": "0.1.*"
    }
}

Dependency

Usage

Basic compiling and instantiation

// # path/to/FooClass.php
class FooClass {
    /* */
}

// compiles FooClass from given class definition file
recompilr\execute('FooClass','path/to/FooClass.php');

// factory creates an instance of FooClass
$foo = recompilr\make('FooClass');

var_dump($foo); // (object) FooClass_*hash

Compile from autoloaded class

// class must be available to autoloader
namespace MyNamespace;
class FooClass {
    /* */
}

// compiles FooClass without an explicit class file, relying on the autoloader to find the class definition
recompilr\execute('MyNamespace\FooClass');

// factory creates an instance of FooClass
$foo = recompilr\make('MyNamespace\FooClass');

var_dump($foo); // (object) FooClass_*hash

Recompiling everything

When class definitions are expected to have changed, all classes may be recompiled using recompilr\all.

// change path/to/FooClass.php while application is running

recompilr\all();

Note: will not compile files where bracketed namespaces are used

Note: all compiled classes are final and may not be inherited from

Binary handling

Saving to file

recompilr\execute('MyNamespace\FooClass');

recompilr\binary('path/to/binary.rcx');

Loading from file

recompilr\load('path/to/binary.rcx');

$foo = recompilr\make('MyNamespace\FooClass');

var_dump($foo); // (object) FooClass_*hash