Example #1
0
    private function process($router)
    {
        extract($router);

        if (!is_callable($handler)) {
            if (is_string($handler)) {
                $params = array_filter(explode('/', $handler));
            } else {
                $params = uri::params();
            }

            $directory_name = array_shift($params);
            $directory_real = rtrim(CONTROLLER_PATH.$directory_name.DIRECTORY_SEPARATOR,'/').DIRECTORY_SEPARATOR;

            if (!is_dir($directory_real)) {
                array_unshift($params, $directory_name);
                $directory_name = null;
                $directory_real = CONTROLLER_PATH;
            }

            switch (count($params)) {
                case 0:
                    array_push($params, DEFAULT_CONTROLLER,DEFAULT_METHOD);
                    break;
                case 1:
                    array_push($params, DEFAULT_METHOD);
                    break;
                default:
                    $params_chunked = array_chunk($params, 2, false);
                    $params         = array_shift($params_chunked);
                    break;
            }

            $class_name = ucwords(array_shift($params));
            if (!file_exists($directory_real.$class_name.PHP_EXT)) {
                throw new \Exception("Page Not Found", 404);
            }

            $class_real  = str_replace(DIRECTORY_SEPARATOR, '\\', str_replace(APP_PATH, '', $directory_real.$class_name));

            $controller  = $class_real::get_instance();
            self::$controller = $controller;

            $method_name = array_shift($params);
            if (is_callable([$controller, $method_name])){
                $processor = [$controller, $method_name];
            } else {
                throw new \Exception("Page Not Found", 404);
            }

        } else {
            $processor = $router['handler'];
        }

        $args = uri::params(3);
        if(preg_match('/'.$router['pattern'].'/i', uri::request_uri(), $matches )) {
            if(count($matches) >= 2) {
                $request_uri_end = array_pop($matches);
                $args = array_filter(explode('/', $request_uri_end));
            }
        }
        call_user_func_array($processor,$args);
    }