<commit_before><?php
// +------------------------------------------------------------
// | Mini Framework
// +------------------------------------------------------------
// | Source: https://github.com/jasonweicn/MiniFramework
// +------------------------------------------------------------
// | Author: Jason.wei <<EMAIL>>
// +------------------------------------------------------------

namespace Mini;

class App
{
    /**
     * 控制器
     * @var string
     */
    public $controller;
    
    /**
     * 动作
     * @var string
     */
    public $action;
    
    /**
     * 函数库清单数组
     * @var array
     */
    private static $_funcs = array();
    
    /**
     * Router实例
     * 
     * @var Router
     */
    protected $_router;
    
    /**
     * Params实例
     * 
     * @var Params
     */
    protected $_params;
    
    /**
     * Request实例
     *
     * @var Request
     */
    protected $_request;
    
    /**
     * App实例
     * 
     * @var App
     */
    protected static $_instance;
    
    /**
     * 获取实例
     * 
     */
    public static function getInstance()
    {
        if (self::$_instance === null) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    
    /**
     * 构造
     * 
     */
    protected function __construct()
    {
        $this->_params = Params::getInstance();
        $this->getRouter();
        
        $this->_request = Request::getInstance();
    }
    
    /**
     * 开始
     * 
     */
    public function run()
    {
        $requestParams = $this->_request->parseRequestParams($this->_router->getRouteType());
        
        if (!empty($requestParams)) {
            $this->_params->setParams($requestParams);
        }
        
        $this->loadFunc('global');
        
        $this->dispatch();
    }
    
    /**
     * 调派
     */
    public function dispatch()
    {
        $request = Request::getInstance();
        $this->controller = $request->_controller;
        $this->action = $request->_action;
        
        $controllerName = ucfirst($this->controller);
        $controllerFile = APP_PATH . DIRECTORY_SEPARATOR . 'Controller' . DIRECTORY_SEPARATOR . $controllerName . '.php';
                
        if (!file_exists($controllerFile)) {
            throw new Exceptions('Controller file "' . $controllerFile . '" not found.', 404);
        }
        
        $controllerName = APP_NAMESPACE . '\\Controller\\' . $controllerName;
        if (class_exists($controllerName)) {
            $controller = new $controllerName();
        } else {
            throw new Exceptions('Controller "' . $controllerName . '" does not exist.', 404);
        }
        
        $action = $this->action . 'Action';
        
        if (method_exists($controller, $action)) {
            $controller->$action();
        } else {
            throw new Exceptions('Action "' . $this->action . '" does not exist.', 404);
        }
    }
    
    /**
     * 获取路由器对象
     * 
     */
    public function getRouter()
    {
        if ($this->_router === null) {
            $this->_router = new Router();
        }
        return $this->_router;
    }
    
    /**
     * 提取地址中的参数
     * 
     * @param array $uriArray
     */
    private function uriToParams($uriArray = null)
    {
        $array = null;
        if (is_array($uriArray)) array_splice($uriArray, 0, 3);
        
        if (!empty($uriArray)) {
            foreach ($uriArray as $key => $value) {
                if ($key % 2 == 0) {
                    $array[$value] = null;
                } else {
                    $array[$uriArray[$key - 1]] = $value;
                }
            }
            
            foreach ($array as $key => $value) {
                if ($value !== null) {
                    $this->_params->setParam($key, $value);
                }
            }
        }
    }
    
    /**
     * 加载函数库
     * @param string $func
     * @throws Exception
     * @return boolean
     */
    private function loadFunc($func)<commit_msg>删除已经废弃的方法<commit_after><?php
// +------------------------------------------------------------
// | Mini Framework
// +------------------------------------------------------------
// | Source: https://github.com/jasonweicn/MiniFramework
// +------------------------------------------------------------
// | Author: Jason.wei <<EMAIL>>
// +------------------------------------------------------------

namespace Mini;

class App
{
    /**
     * 控制器
     * @var string
     */
    public $controller;
    
    /**
     * 动作
     * @var string
     */
    public $action;
    
    /**
     * 函数库清单数组
     * @var array
     */
    private static $_funcs = array();
    
    /**
     * Router实例
     * 
     * @var Router
     */
    protected $_router;
    
    /**
     * Params实例
     * 
     * @var Params
     */
    protected $_params;
    
    /**
     * Request实例
     *
     * @var Request
     */
    protected $_request;
    
    /**
     * App实例
     * 
     * @var App
     */
    protected static $_instance;
    
    /**
     * 获取实例
     * 
     */
    public static function getInstance()
    {
        if (self::$_instance === null) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    
    /**
     * 构造
     * 
     */
    protected function __construct()
    {
        $this->_params = Params::getInstance();
        $this->getRouter();
        
        $this->_request = Request::getInstance();
    }
    
    /**
     * 开始
     * 
     */
    public function run()
    {
        $requestParams = $this->_request->parseRequestParams($this->_router->getRouteType());
        
        if (!empty($requestParams)) {
            $this->_params->setParams($requestParams);
        }
        
        $this->loadFunc('global');
        
        $this->dispatch();
    }
    
    /**
     * 调派
     */
    public function dispatch()
    {
        $request = Request::getInstance();
        $this->controller = $request->_controller;
        $this->action = $request->_action;
        
        $controllerName = ucfirst($this->controller);
        $controllerFile = APP_PATH . DIRECTORY_SEPARATOR . 'Controller' . DIRECTORY_SEPARATOR . $controllerName . '.php';
                
        if (!file_exists($controllerFile)) {
            throw new Exceptions('Controller file "' . $controllerFile . '" not found.', 404);
        }
        
        $controllerName = APP_NAMESPACE . '\\Controller\\' . $controllerName;
        if (class_exists($controllerName)) {
            $controller = new $controllerName();
        } else {
            throw new Exceptions('Controller "' . $controllerName . '" does not exist.', 404);
        }
        
        $action = $this->action . 'Action';
        
        if (method_exists($controller, $action)) {
            $controller->$action();
        } else {
            throw new Exceptions('Action "' . $this->action . '" does not exist.', 404);
        }
    }
    
    /**
     * 获取路由器对象
     * 
     */
    public function getRouter()
    {
        if ($this->_router === null) {
            $this->_router = new Router();
        }
        return $this->_router;
    }
    
    /**
     * 加载函数库
     * @param string $func
     * @throws Exception
     * @return boolean
     */
    private function loadFunc($func)
{'fraction_non_alphanumeric': 0.2025867937372362, 'fraction_numerical': 0.0031313818924438392, 'mean_word_length': 1.2093233082706767, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 55, 'https://': 2, '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': '7031843', 'n_tokens_mistral': 2096, 'n_tokens_neox': 2071, 'n_words': 379}