<?php


namespace Lime\Cache;

use \Exception;
use \ReflectionClass;

/**
 * Class Cache
 * @package Lime\Cache
 *
 * @method static mixed    get(string $key)
 * @method static void     put(string $key, mixed $value, int $minutes=0)
 * @method static void     increment($key, $value = 1)
 * @method static void     decrement($key, $value = 1);
 * @method static void     remove($key);
 * @method static void     flush();
 * @method static mixed     getDriver();
 * @method static string     getPrefix();
 */
class Cache
{
    /**
     * Cache library version
     */
    const VERSION = '0.1-Dev';

    /**
     * @var array
     */
    protected static $_config = array();

    /**
     * @var ICacheDriver
     */
    protected static $_driver = null;

    /**
     * Configure cache servers
     *
     * @param array $params
     */
    public static function setup($config = array())
    {
        self::$_config = $config;

        if( !isset($config['driver']) ) {
            throw new Exception('Driver is not specified.');
        }

        $driverName = $config['driver'].'Driver';
        $prefix = isset($config['prefix']) ? $config['prefix'] : '';
        $servers = isset($config['servers']) ? $config['servers'] : null;

        $rf = new ReflectionClass($driverName);
        self::$_driver = $rf->newInstanceArgs(array($servers, $prefix));
    }

    public static function __callStatic($name, $arguments = array())
    {
        if( self::$_driver == null )
            throw new \Exception('Driver is not loaded. Run setup first.');

        return call_user_func_array(array( self::$_driver, $name ), $arguments);
    }
} 