back to devpro   download
<?php
function __autofactory($fileName, $require = true){
    // (C) Andrea Giammarchi - WebReflection - Mit Style License
    if(@$fp = fopen($fileName, 'wb')){
        $function_list = array('<?php', '');
        $__factory = array('class __factory{static ', '', ';function init(){foreach(array(', '', ')as $key=>$value)self::$$key=$value;}}__factory::init();');
        foreach(get_declared_classes() as $value){
            if(!function_exists($value)){
                $class  = new ReflectionClass($value);
                $method = $class->getConstructor();
                if(is_null($method))
                    $function_list[] = "function {$value}(){return new {$value};}";
                elseif($method->isPublic()){
                    $__factory[1] .= "\${$value},";
                    $__factory[3] .= "'{$value}'=>new ReflectionClass('{$value}'),";
                    $function_list[] = "function {$value}(){return 0<func_num_args()?__factory::\${$value}->newInstanceArgs(func_get_args()):new {$value};}";
                }
            }
        }
        $__factory[1] = substr($__factory[1], 0, -1);
        $__factory[3] = substr($__factory[3], 0, -1);
        $function_list[] = '?>';
        $function_list[1] = implode('', $__factory);
        fwrite($fp, implode(PHP_EOL, $function_list));
        fclose($fp);
        if(function_exists('apc_compile_file'))
            apc_compile_file($fileName);
        if($require)
            require $fileName;
        return  true;
    }
    return  false;
}
?>
back to devpro   download
Creative Commons License