<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/** 
 * 
 * 模块机制类
 * 
 */
class Thirdparty extends CI_Controller {

    public function __construct() {
        parent::__construct();
        //$this->output->enable_profiler(TRUE);
    }

    /*public function action()
    {
        $third_party = 'third_party';
        $segment_array   = $this->uri->segment_array();
        $module_key      = array_search('thirdparty',$segment_array);
        $segment_count   = count($segment_array);
        //组装传递参数
        $parameter = array();
        for ($i=6; $i <= $segment_count; $i++) { 
            $parameter[]=$this->uri->segment($i);
        }
        //获取模块名
        $module_name     = $this->uri->segment(($module_key+2));
        if (!$module_name) {
            send_404($this);
            return false;
        }
        //模块控制器名
        $controller_name = strtolower($this->uri->segment(($module_key+3), 'index'));
        //模块控制器调用的方法名
        $action_name     = $this->uri->segment(($module_key+4), 'index');
        
        //加载指定模块(独立应用)
        $module_path=APPPATH.$third_party."/".$module_name; //存放模块的目录
        $view_cascade=TRUE;                                 //允许加载模块内、外的视图
        $this->load->add_package_path($module_path."/",$view_cascade);
        
        //加载指定的模块控制器文件
        if ( ! file_exists($module_path.'/controllers/'.$controller_name.'.php')) {
            return false;
        }
        require_once($module_path.'/controllers/'.$controller_name.'.php');
        $controller_name = ucfirst($controller_name);

        //实例化指定模块de控制器
        if (!class_exists($controller_name,false)) {
           return false;
        }
        $ctrl_object = new $controller_name();

        //调用指定模块的控制器方法,并传递参数
        if (!method_exists($ctrl_object,$action_name)) {
            return false;
        }
        call_user_func_array(array($ctrl_object, $action_name), $parameter);
        
        //停止加载该模块
        $this->load->remove_package_path($module_path."/");
    }*/

    public function _remap($app,$param)
    {
        //第三方应用存放目录
        $third_party = 'third_party';

        //请求的应用、制器、方法
        $app_name        = strtolower($app);
        $controller_name = isset($param[0])?strtolower($param[0]):'index';
        $action_name     = isset($param[1])?strtolower($param[1]):'index';

        //加载应用包
        $app_path=APPPATH.$third_party."/".$app_name."/"; //存放模块的目录
        $view_cascade=TRUE;                           //允许加载模块内、外的视图
        $this->load->add_package_path($app_path,$view_cascade);

        //加载控制器
        if ( ! file_exists($app_path.'controllers/'.$controller_name.'.php')) {
            echo 'Controller file is not exists!';
            return false;
        }
        require_once($app_path.'controllers/'.$controller_name.'.php');
        $controller_name = ucfirst($controller_name);
        
        //实例化控制器并调用请求的方法
        if (class_exists($controller_name,false)) 
        {
            $controllerHandler = new $controller_name();
            if(method_exists($controllerHandler,$action_name)) {
                call_user_func_array(array($controllerHandler, $action_name), array_slice($param, 2));
            }else{
                echo 'Method is not exists!';
            }
        }
        else
        {
            echo 'Controller is not exists!';
        }
        
        //停止加载应用包
        $this->load->remove_package_path($app_path);      
    }
}