<commit_before><?php
/**
* OmniApp Framework
*
* @package		OmniApp
* @author		<NAME>
* @copyright	(c) 2011 OmniApp Framework
*/
namespace OMniApp;

/**
 * Core Class
 * @author <NAME>
 * @todo 分离Request和Config，优化Dispatch，优化Config，增加Response，实现Model，优化日志记录
 */
class Core
{
    private static $config;
    private static $request;
    private static $env;

    private static $logs = array();

    const LOG_LEVEL_DEBUG = 0;
    const LOG_LEVEL_NOTICE = 1;
    const LOG_LEVEL_WARN = 2;
    const LOG_LEVEL_ERROR = 3;
    const LOG_LEVEL_CRITICAL = 4;

    public static function init($config = array())
    {
        self::$config = new ArrayObjectWrapper($config);

        spl_autoload_register(array(__CLASS__, '__autoloader'));
        register_shutdown_function(array(__CLASS__, '__shutdown'));

        self::__init();
        self::register_error_handler();
    }

    public static function start()
    {
        self::dispatch(self::$request['path']);
    }

    public static function dispatch($path)
    {
        list($params, $route, $controller) = self::route($path);

        $controller = new $controller($route);
        $request_methods = array('GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'HEAD');
        $method = self::$request['method'];

        if(!in_array($method, $request_methods) || !method_exists($controller, $method))
        {
            if( ! method_exists($controller, 'run'))
            {
                throw new Exception('Invalid Request Method.');
            }
            $method = 'run';
        }

        $controller->before($method);
        if($params)
        {
            call_user_func_array(array($controller, $method), $params);
        }
        else
        {
            $controller->$method();
        }
        $controller->after($method);

        return $controller;
    }

    public static function model($name)
    {
        return Model($name);
    }

    public static function view($tpl)
    {
        return View($tpl);
    }

    public static function route($path)
    {
        $routes = self::config('route');

        $path = trim($path, '/');
        if($path === '')
        {
            return array(array(), '', $routes['']);
        }

        if($path AND ! preg_match('/^[\w\-~\/\.]{1,400}$/', $path))
        {
            $path = '404';
        }

        foreach($routes as $route => $controller)
        {
            if( ! $route) continue;

            if($route{0} === '/')
            {
                if(preg_match($route, $path, $matches))
                {
                    $complete = array_shift($matches);
                    $params = explode('/', trim(mb_substr($path, mb_strlen($complete)), '/'));
                    if($params[0])
                    {
                        foreach($matches as $match)
                        {
                            array_unshift($params, $match);
                        }
                    }
                    else
                  {
                        $params = $matches;
                    }

                    return array($params, $complete, $controller);
                }
            }
            else
            {
                if(mb_substr($path, 0, mb_strlen($route)) === $route)
                {
                    $params = explode('/', trim(mb_substr($path, mb_strlen($route)), '/'));
                    return array($params, $route, $controller);
                }
            }
        }

        return array(array($path), $path, $routes['404']);
    }

    public static function config($key = false)
    {
        return self::__arrayObjectWrapper(self::$config[$key]);
    }

    public static function request($key = false)
    {
        return self::__arrayObjectWrapper(self::$request[$key]);
    }

    public static function env($key = false)
    {
        return self::__arrayObjectWrapper(self::$env[$key]);
    }

    public static function register_error_handler()
    {
        set_error_handler(array(__CLASS__, '__error'));
        set_exception_handler(array(__CLASS__, '__exception'));
    }

    public static function log($text, $level = self::LOG_LEVEL_NOTICE)
    {
        if($level < self::config('log')->level)
        {
            return;
        }
        $log = array (
            'id' => isset($_SERVER['HTTP_REQUEST_ID']) ? $_SERVER['HTTP_REQUEST_ID'] : '',<commit_msg>add restore_handler<commit_after><?php
/**
* OmniApp Framework
*
* @package		OmniApp
* @author		<NAME>
* @copyright	(c) 2011 OmniApp Framework
*/
namespace OMniApp;

/**
 * Core Class
 * @author <NAME>
 * @todo 分离Request和Config，优化Dispatch，优化Config，增加Response，实现Model，优化日志记录
 */
class Core
{
    private static $config;
    private static $request;
    private static $env;

    private static $logs = array();

    const LOG_LEVEL_DEBUG = 0;
    const LOG_LEVEL_NOTICE = 1;
    const LOG_LEVEL_WARN = 2;
    const LOG_LEVEL_ERROR = 3;
    const LOG_LEVEL_CRITICAL = 4;

    public static function init($config = array())
    {
        self::$config = new ArrayObjectWrapper($config);

        spl_autoload_register(array(__CLASS__, '__autoloader'));
        register_shutdown_function(array(__CLASS__, '__shutdown'));

        self::__init();
        self::register_handler();
    }

    public static function start()
    {
        self::dispatch(self::$request['path']);
    }

    public static function dispatch($path)
    {
        list($params, $route, $controller) = self::route($path);

        $controller = new $controller($route);
        $request_methods = array('GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'HEAD');
        $method = self::$request['method'];

        if(!in_array($method, $request_methods) || !method_exists($controller, $method))
        {
            if( ! method_exists($controller, 'run'))
            {
                throw new Exception('Invalid Request Method.');
            }
            $method = 'run';
        }

        $controller->before($method);
        if($params)
        {
            call_user_func_array(array($controller, $method), $params);
        }
        else
        {
            $controller->$method();
        }
        $controller->after($method);

        return $controller;
    }

    public static function model($name)
    {
        return Model($name);
    }

    public static function view($tpl)
    {
        return View($tpl);
    }

    public static function route($path)
    {
        $routes = self::config('route');

        $path = trim($path, '/');
        if($path === '')
        {
            return array(array(), '', $routes['']);
        }

        if($path AND ! preg_match('/^[\w\-~\/\.]{1,400}$/', $path))
        {
            $path = '404';
        }

        foreach($routes as $route => $controller)
        {
            if( ! $route) continue;

            if($route{0} === '/')
            {
                if(preg_match($route, $path, $matches))
                {
                    $complete = array_shift($matches);
                    $params = explode('/', trim(mb_substr($path, mb_strlen($complete)), '/'));
                    if($params[0])
                    {
                        foreach($matches as $match)
                        {
                            array_unshift($params, $match);
                        }
                    }
                    else
                  {
                        $params = $matches;
                    }

                    return array($params, $complete, $controller);
                }
            }
            else
            {
                if(mb_substr($path, 0, mb_strlen($route)) === $route)
                {
                    $params = explode('/', trim(mb_substr($path, mb_strlen($route)), '/'));
                    return array($params, $route, $controller);
                }
            }
        }

        return array(array($path), $path, $routes['404']);
    }

    public static function config($key = false)
    {
        return self::__arrayObjectWrapper(self::$config[$key]);
    }

    public static function request($key = false)
    {
        return self::__arrayObjectWrapper(self::$request[$key]);
    }

    public static function env($key = false)
    {
        return self::__arrayObjectWrapper(self::$env[$key]);
    }

    public static function register_handler()
    {
        set_error_handler(array(__CLASS__, '__error'));
        set_exception_handler(array(__CLASS__, '__exception'));
    }
    
    public static function restore_handler()
    {
        restore_error_handler();
        restore_exception_handler();
    }

    public static function log($text, $level = self::LOG_LEVEL_NOTICE)
    {
        if($level < self::config('log')->level)
        {
            return;
        }
        $log = array (
            'id' => isset($_SERVER['HTTP_REQUEST_ID']) ? $_SERVER['HTTP_REQUEST_ID'] : '',
{'fraction_non_alphanumeric': 0.1445593525179856, 'fraction_numerical': 0.0049460431654676255, 'mean_word_length': 1.7794439237738207, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3565939', 'n_tokens_mistral': 2531, 'n_tokens_neox': 2450, 'n_words': 473}