MY_Loader::module PHP Method

module() public method

This function lets users load and instantiate module.
public module ( $module_uri, $vars = [], $return = FALSE ) : void
return void
    public function module($module_uri, $vars = array(), $return = FALSE)
    {
        if ($module_uri == '') {
            return;
        }
        $module_uri = trim($module_uri, '/');
        $CI =& get_instance();
        $default_controller = $CI->router->default_controller;
        if (strpos($module_uri, '/') === FALSE) {
            $path = '';
            // 只有模块名,使用默认控制器和默认方法
            $module = $module_uri;
            $controller = $default_controller;
            $method = 'index';
            $segments = array();
        } else {
            $segments = explode('/', $module_uri);
            if (file_exists(APPPATH . 'modules/' . $segments[0] . '/controllers/' . ucfirst($segments[1]) . '.php')) {
                $path = '';
                $module = $segments[0];
                $controller = $segments[1];
                $method = isset($segments[2]) ? $segments[2] : 'index';
            } elseif (is_dir(APPPATH . 'modules/' . $segments[0] . '/' . $segments[1] . '/controllers')) {
                // Set the directory and remove it from the segment array
                $path = $segments[0];
                $segments = array_slice($segments, 1);
                if (count($segments) > 0) {
                    // 子目录下有模块?
                    if (is_dir(APPPATH . 'modules/' . $path . '/' . $segments[0] . '/controllers')) {
                        $module = $segments[0];
                        $controller = isset($segments[1]) ? $segments[1] : $default_controller;
                        $method = isset($segments[2]) ? $segments[2] : 'index';
                    }
                } else {
                    throw new RuntimeException('Unable to locate the module you have specified: ' . $path);
                }
            } else {
                throw new RuntimeException('Unable to locate the module you have specified: ' . $module_uri);
            }
            if ($path != '') {
                $path = rtrim($path, '/') . '/';
            }
        }
        // 模块名全部小写
        $module = strtolower($module);
        // 必须是类似这样的模块类名:目录_模块名_控制器名_module (如:Account_Message_Home_module)
        $c = str_replace(' ', '_', ucwords(str_replace('_', ' ', $controller)));
        $class_name = str_replace(' ', '_', ucwords(str_replace('/', ' ', $path . $module . ' ' . $c))) . '_module';
        // Module 的控制器文件的路径
        $controller_path = APPPATH . 'modules/' . $path . $module . '/controllers/' . ucfirst($controller) . '.php';
        if (!file_exists($controller_path)) {
            throw new RuntimeException('Unable to locate the module you have specified: ' . $path . $module . '/controllers/' . $controller . '.php');
        }
        if (!class_exists('CI_Module')) {
            require_once APPPATH . 'core/Module.php';
        }
        if (!isset($CI->{$class_name})) {
            // 装载 Module 控制器文件
            require_once $controller_path;
            // 实例化 Module 控制器
            $CI->{$class_name} = new $class_name();
            // 注意:要操作模块里的 loader 类实例
            $CI->{$class_name}->load->_ci_module_path = $path . $module;
            $CI->{$class_name}->load->_ci_module_class = $class_name;
            $CI->{$class_name}->_ci_module_uri = $path . $module . '/' . $controller;
            $CI->{$class_name}->_ci_module_method = $method;
        }
        $module_load =& $CI->{$class_name}->load;
        if (strncmp($method, '_', 1) != 0 && in_array(strtolower($method), array_map('strtolower', get_class_methods($class_name)))) {
            ob_start();
            log_message('debug', 'Module call: ' . $class_name . '->' . $method);
            // Call the requested method.
            // Any URI segments present (besides the class/function) will be passed to the method for convenience
            $output = call_user_func_array(array($CI->{$class_name}, $method), $module_load->_ci_object_to_array($vars));
            if ($return === TRUE) {
                $buffer = ob_get_contents();
                @ob_end_clean();
                $result = $output ? $output : $buffer;
                return $result;
            } else {
                if (ob_get_level() > $this->_ci_ob_level + 1) {
                    ob_end_flush();
                } else {
                    $buffer = ob_get_contents();
                    $result = $output ? $output : $buffer;
                    $CI->output->append_output($result);
                    @ob_end_clean();
                }
            }
        } else {
            throw new RuntimeException('Unable to locate the ' . $method . ' method you have specified: ' . $class_name);
        }
    }