<?php
 
/** 
 
 * @author oliver anan <[email protected]> 
 
 * @version 1.0 
 
 * @copyright oliver anan 
 
 * @license LGPL 3 http://www.gnu.org/licenses/lgpl.html 
 
 * Service provider for creating empty interfaces at runtime.
 
 * If an class implements an interface that is not bundled with the class
 
 * this class can be used to prevent an 'interface not found' error. 
 
 */ 
 
abstract class VirtualInterface{
 
    
 
    /**
 
     * @author oliver anan <[email protected]>
 
     * @version 1.0
 
     * @since 1.0
 
     * @var string[]
 
     * A list of all generated interfaces.
 
     */
 
    protected static $virtualized = array();
 
    
 
    /**
 
     * @author oliver anan <[email protected]>
 
     * @version 1.0
 
     * @since 1.0
 
     * @param $interface The name of the interface including the namespace without a leading '\'.
 
     * @param $autoload Indicates if the method should attemp to autoload the interface. Default is true.
 
     * @return boolean true if the method creates the interface, false if the interface can be autoloaded. 
 
     * @example VirtualInterface::add('MyNamespace\foo'); class bar implements MyNamespace\foo{...} 
 
     * loads MyNamespace\foo if it is available. If it can not be loaded a empty interface 'foo' will be generated
 
     * in the namespace 'myNamespace'
 
     */
 
    public static function add($interface,$autoload=true){
 
        if(! interface_exists($interface,$autoload)){
 
            //seperate interface name and namespace
 
            $parts = explode('\\',$interface);
 
            $interfaceName = array_pop($parts);
 
            $namespace = implode('\\',$parts);
 
            //generate source code
 
            $code = '';
 
            if($namespace) $code .= "namespace $namespace;";
 
            $code .= " interface $interfaceName {}";
 
            //create interface
 
            echo $code;
 
            eval($code);
 
            self::$virtualized[] = $interface;
 
            return true;
 
        }
 
        else return false;
 
    }
 
    
 
    /**
 
     * @author oliver anan <[email protected]>
 
     * @version 1.0
 
     * @since 1.0
 
     * @param $interface the name of the interface including the namespace without a leading '\'.
 
     * @return boolean true if the interface is virtual.
 
     */
 
    public static function isVirtual($interface){
 
        return (array_search($interface, self::$virtualized)!==false);
 
    }
 
    
 
    /**
 
     * @author oliver anan <[email protected]>
 
     * @version 1.0
 
     * @since 1.0
 
     * @return string[] A list of all generated interfaces.
 
     */
 
    public static function getVirtualized(){
 
        return self::$virtualized;
 
    }
 
}
 
 |