jgswift/recompilr

运行时类编译器

0.1.1 2014-09-28 06:30 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:47:27 UTC


README

PHP 5.5+ 工厂运行时类编译器

Build Status Scrutinizer Code Quality Latest Stable Version License Coverage Status

描述

recompilr 使用类定义并在运行时通过 eval 和唯一的哈希标识符重新编译它。在修改类定义后,无需重新启动应用程序即可重新编译类。这实际上允许应用程序在运行时重新声明类。

安装

使用 composer 通过命令行安装

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

使用 composer 通过 composer.json 安装

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

依赖项

用法

基本编译和实例化

// # 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

从自动加载的类编译

// 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

重新编译所有内容

当期望类定义已更改时,可以使用 recompilr\all 重新编译所有类。

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

recompilr\all();

注意:不会编译使用括号命名空间定义的文件

注意:所有编译后的类都是最终的,不能被继承

二进制处理

保存到文件

recompilr\execute('MyNamespace\FooClass');

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

从文件加载

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

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

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