<?php
 
/**
 
 * @package QuickAPI
 
 * @version 1.0
 
 * @author RReverser
 
 */
 
 
/** Interface to mark class for auto-registering of methods */
 
interface QAPI_Interface {}
 
 
/** Main class for API methods manipulation */
 
class QAPI implements Serializable
 
{
 
    /** Registered methods */
 
    private $methods = array();
 
    
 
    /**
 
     * Register callable as API handler
 
     * @param string|array $funcName Callable API handler
 
     * @param string $name Name for API method
 
     * @return bool
 
     */
 
    function registerFunction($funcName, $name = null)
 
    {
 
        if(is_callable($funcName))
 
        {
 
            if($name === null)
 
            {
 
                $name = is_array($funcName) ? $funcName[1] : $funcName;
 
            }
 
            $name = strtolower($name);
 
            $this->methods[$name] = $funcName;
 
            return true;
 
        } else
 
        {
 
            return false;
 
        }
 
    }
 
    
 
    /**
 
     * Register object method
 
     * @param object $object Object with method to be registered as API handler
 
     * @param string $methodName Name of method to be registered
 
     * @param string $name Name for API method
 
     * @return bool
 
     */
 
    function registerMethod($object, $methodName, $name = null)
 
    {
 
        if(substr($methodName, 0, 2) == '__')
 
        {
 
            return false;
 
        }
 
        if($name === null)
 
        {
 
            $name = $methodName;
 
        }
 
        return $this->registerFunction(array($object, $methodName), $name);
 
    }
 
    
 
    /**
 
     * Register class static method
 
     * @param string $className Class with static method to be registered as API handler
 
     * @param string $methodName Name of method to be registered
 
     * @param string $name Name for API method
 
     * @return bool
 
     */
 
    function registerStatic($className, $methodName, $name = null)
 
    {
 
        if(substr($methodName, 0, 2) == '__')
 
        {
 
            return false;
 
        }
 
        if($name === null)
 
        {
 
            $name = $methodName;
 
        }
 
        return $this->registerFunction($className . '::' . $methodName, $name);
 
    }
 
    
 
    /**
 
     * Register object
 
     * @param object $object Object with methods to be registered as API handlers
 
     * @param string $prefix Prefix for object and class methods (use '$' to insert class name)
 
     * @return bool
 
     */
 
    function registerObject($object, $prefix = '')
 
    {
 
        if(!is_object($object)) return false;
 
        $className = get_class($object);
 
        $prefix = str_replace('$', $className, $prefix);
 
        $methods = get_class_methods($className);
 
        $result = 1;
 
        foreach($methods as $methodName)
 
        {
 
            $result &= $this->registerMethod($object, $methodName, $prefix . $methodName);
 
        }
 
        return (bool)$result;
 
    }
 
 
    /**
 
     * Register class
 
     * @param string $className Class with static methods to be registered as API handlers
 
     * @param string $prefix Prefix for object and class methods (use '$' to insert class name)
 
     * @return bool
 
     */
 
    function registerClass($className, $prefix = '')
 
    {
 
        if(!class_exists($className)) return false;
 
        $prefix = str_replace('$', $className, $prefix);
 
        $methods = get_class_methods($className);
 
        $result = 1;
 
        foreach($methods as $methodName)
 
        {
 
            $result &= $this->registerStatic($className, $methodName, $prefix . $methodName);
 
        }
 
        return (bool)$result;
 
    }
 
   
 
    /**
 
     * Register classes that implement QAPI_Interface
 
     * @param string $prefix Prefix for object and class methods (use '$' to insert class name)
 
     * @return bool
 
     */
 
    function registerClasses($prefix = '')
 
    {
 
        $classes = get_declared_classes();
 
        $result = 1;
 
        foreach($classes as $className)
 
        {
 
            if(in_array('QAPI_Interface', class_implements($className)))
 
            {
 
                $result &= $this->registerClass($className, $prefix);
 
            }
 
        }
 
        return (bool)$result;
 
    }
 
    
 
    /**
 
     * Register global objects that implement QAPI_Interface
 
     * @param string $prefix Prefix for object and class methods (use '$' to insert class name)
 
     * @return bool
 
     */
 
    function registerObjects($prefix = '')
 
    {
 
        $objects = $GLOBALS;
 
        $result = 1;
 
        foreach($objects as $object)
 
        {
 
            if(in_array('QAPI_Interface', class_implements(get_class($object))))
 
            {
 
                $result &= $this->registerObject($object, $prefix);
 
            }
 
        }
 
        return (bool)$result;
 
    }
 
    
 
    /**
 
     * Auto-recognize and register API handler(s)
 
     * @param object|string|array $handler Object, class or callable to be registered
 
     * @param string $prefix Prefix for object and class methods (use '$' to insert class name)
 
     * @return bool
 
     */
 
    function register($handler = null, $prefix = '')
 
    {
 
        if($handler === null)
 
        {
 
            return $this->registerClasses($prefix);
 
        }
 
        if(is_object($handler))
 
            return $this->registerObject($handler, $prefix);
 
        elseif(class_exists((string)$handler))
 
            return $this->registerClass($handler, $prefix);
 
        elseif(is_callable($handler))
 
            return $this->registerFunction($handler);
 
        else
 
            return false;
 
    }
 
    
 
    /**
 
     * Call registered method
 
     * @param string $name API method name
 
     * @return string
 
     */
 
    function call($name)
 
    {
 
        return $this->__call($name, $this->getParams());
 
    }
 
    
 
    /**
 
     * Magic wrapper for call method
 
     * @ignore
 
     */
 
    function __call($name, $params)
 
    {
 
        $name = strtolower($name);
 
        return isset($this->methods[$name]) ? $this->dataToString(call_user_func_array($this->methods[$name], $params)) : null;
 
    }
 
 
    /**
 
     * Serialize formed array of registered methods
 
     * @ignore
 
     */
 
    function serialize()
 
    {
 
        return serialize($this->methods);
 
    }
 
    
 
    /**
 
     * Unserialize methods
 
     * @ignore
 
     */
 
    function unserialize($methods)
 
    {
 
        $this->methods = unserialize($methods);
 
    }
 
    
 
    /**
 
     * Wrapper for converting result to string (JSON here, may be overridden in descendant class)
 
     * @param mixed $result Data to be converted
 
     * @return string
 
     */
 
    function dataToString($result)
 
    {
 
        return json_encode($result);
 
    }
 
 
    /**
 
     * Wrapper for getting params for method being called (URI 'params' value here, may be overridden in descendant class)
 
     * @return array
 
     */
 
    function getParams()
 
    {
 
        return $_GET['params'];
 
    }
 
}
 
 |