From 6530f6ad13e15c1660aacd3f5817892ca0a6bd3f Mon Sep 17 00:00:00 2001 From: w3prodigy Date: Fri, 29 Jul 2016 17:31:59 -0400 Subject: [PATCH 1/5] moved application files into application directory to allow for hmvc module code to be used as an upstream --- application/core/MY_Loader.php | 8 + application/core/MY_Router.php | 8 + application/third_party/HMVC/Loader.php | 527 ++++++++++++++++++++++++ application/third_party/HMVC/Router.php | 297 +++++++++++++ 4 files changed, 840 insertions(+) create mode 100644 application/core/MY_Loader.php create mode 100644 application/core/MY_Router.php create mode 100644 application/third_party/HMVC/Loader.php create mode 100644 application/third_party/HMVC/Router.php diff --git a/application/core/MY_Loader.php b/application/core/MY_Loader.php new file mode 100644 index 0000000..1e3f9a2 --- /dev/null +++ b/application/core/MY_Loader.php @@ -0,0 +1,8 @@ +_ci_get_component('router'); + if ($router->module) { + $this->add_module($router->module); + } + } + + /** + * Controller Loader + * + * This function lets users load and hierarchical controllers to enable HMVC support + * + * @param string the uri to the controller + * @param array parameters for the requested method + * @param boolean return the result instead of showing it + * @return void + */ + public function controller($uri, $params = array(), $return = FALSE) { + // No valid module detected, add current module to uri + list($module) = $this->detect_module($uri); + if (!isset($module)) { + $router = & $this->_ci_get_component('router'); + if ($router->module) { + $module = $router->module; + $uri = $module . '/' . $uri; + } + } + + // Add module + $this->add_module($module); + + // Execute the controller method and capture output + $void = $this->_load_controller($uri, $params, $return); + + // Remove module + $this->remove_module(); + + return $void; + } + + /** + * Class Loader + * + * This function lets users load and instantiate classes. + * It is designed to be called from a user's app controllers. + * + * @param string the name of the class + * @param mixed the optional parameters + * @param string an optional object name + * @return void + */ + public function library($library = '', $params = NULL, $object_name = NULL) { + if (is_array($library)) { + foreach ($library as $class) { + $this->library($class, $params); + } + return; + } + + // Detect module + if (list($module, $class) = $this->detect_module($library)) { + // Module already loaded + if (in_array($module, $this->_ci_modules)) { + return parent::library($class, $params, $object_name); + } + + // Add module + $this->add_module($module); + + // Let parent do the heavy work + $void = parent::library($class, $params, $object_name); + + // Remove module + $this->remove_module(); + + return $void; + } else { + return parent::library($library, $params, $object_name); + } + } + + /** + * Model Loader + * + * This function lets users load and instantiate models. + * + * @param string the name of the class + * @param string name for the model + * @param bool database connection + * @return void + */ + public function model($model, $name = '', $db_conn = FALSE) { + if (is_array($model)) { + foreach ($model as $babe) { + $this->model($babe); + } + return; + } + + // Detect module + if (list($module, $class) = $this->detect_module($model)) { + // Module already loaded + if (in_array($module, $this->_ci_modules)) { + return parent::model($class, $name, $db_conn); + } + + // Add module + $this->add_module($module); + + // Let parent do the heavy work + $void = parent::model($class, $name, $db_conn); + + // Remove module + $this->remove_module(); + + return $void; + } else { + return parent::model($model, $name, $db_conn); + } + } + + /** + * Load View + * + * This function is used to load a "view" file. It has three parameters: + * + * 1. The name of the "view" file to be included. + * 2. An associative array of data to be extracted for use in the view. + * 3. TRUE/FALSE - whether to return the data or load it. In + * some cases it's advantageous to be able to return data so that + * a developer can process it in some way. + * + * @param string + * @param array + * @param bool + * @return void + */ + public function view($view, $vars = array(), $return = FALSE) { + // Detect module + if (list($module, $class) = $this->detect_module($view)) { + // Module already loaded + if (in_array($module, $this->_ci_modules)) { + return parent::view($class, $vars, $return); + } + + // Add module + $this->add_module($module); + + // Let parent do the heavy work + $void = parent::view($class, $vars, $return); + + // Remove module + $this->remove_module(); + + return $void; + } else { + return parent::view($view, $vars, $return); + } + } + + /** + * Loads a config file + * + * @param string + * @param bool + * @param bool + * @return void + */ + public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE) { + // Detect module + if (list($module, $class) = $this->detect_module($file)) { + // Module already loaded + if (in_array($module, $this->_ci_modules)) { + return parent::config($class, $use_sections, $fail_gracefully); + } + + // Add module + $this->add_module($module); + + // Let parent do the heavy work + $void = parent::config($class, $use_sections, $fail_gracefully); + + // Remove module + $this->remove_module(); + + return $void; + } else { + parent::config($file, $use_sections, $fail_gracefully); + } + } + + /** + * Load Helper + * + * This function loads the specified helper file. + * + * @param mixed + * @return void + */ + public function helper($helper = array()) { + if (is_array($helper)) { + foreach ($helper as $help) { + $this->helper($help); + } + return; + } + + // Detect module + if (list($module, $class) = $this->detect_module($helper)) { + // Module already loaded + if (in_array($module, $this->_ci_modules)) { + return parent::helper($class); + } + + // Add module + $this->add_module($module); + + // Let parent do the heavy work + $void = parent::helper($class); + + // Remove module + $this->remove_module(); + + return $void; + } else { + return parent::helper($helper); + } + } + + /** + * Loads a language file + * + * @param array + * @param string + * @return void + */ + public function language($file = array(), $lang = '') { + if (is_array($file)) { + foreach ($file as $langfile) { + $this->language($langfile, $lang); + } + return; + } + + // Detect module + if (list($module, $class) = $this->detect_module($file)) { + // Module already loaded + if (in_array($module, $this->_ci_modules)) { + return parent::language($class, $lang); + } + + // Add module + $this->add_module($module); + + // Let parent do the heavy work + $void = parent::language($class, $lang); + + // Remove module + $this->remove_module(); + + return $void; + } else { + return parent::language($file, $lang); + } + } + + /** + * Load Widget + * + * This function provides support to Jens Segers Template Library for loading + * widget controllers within modules (place in module/widgets folder). + * @author hArpanet - 23-Jun-2014 + * + * @param string $widget Must contain Module name if widget within a module + * (eg. test/nav where module name is 'test') + * @return array|false + */ + public function widget($widget) { + + // Detect module + if (list($module, $widget) = $this->detect_module($widget)) { + // Module already loaded + if (in_array($module, $this->_ci_modules)) { + return array($module, $widget); + } + + // Add module + $this->add_module($module); + + // Look again now we've added new module path + $void = $this->widget($module.'/'.$widget); + + // Remove module if widget not found within it + if (!$void) { + $this->remove_module(); + } + + return $void; + + } else { + // widget not found in module + return FALSE; + } + } + + /** + * Add Module + * + * Allow resources to be loaded from this module path + * + * @param string + * @param boolean + */ + public function add_module($module, $view_cascade = TRUE) { + if ($path = $this->find_module($module)) { + // Mark module as loaded + array_unshift($this->_ci_modules, $module); + + // Add package path + parent::add_package_path($path, $view_cascade); + } + } + + /** + * Remove Module + * + * Remove a module from the allowed module paths + * + * @param type + * @param bool + */ + public function remove_module($module = '', $remove_config = TRUE) { + if ($module == '') { + // Mark module as not loaded + array_shift($this->_ci_modules); + + // Remove package path + parent::remove_package_path('', $remove_config); + } else if (($key = array_search($module, $this->_ci_modules)) !== FALSE) { + if ($path = $this->find_module($module)) { + // Mark module as not loaded + unset($this->_ci_modules[$key]); + + // Remove package path + parent::remove_package_path($path, $remove_config); + } + } + } + + /** + * Controller loader + * + * This function is used to load and instantiate controllers + * + * @param string + * @param array + * @param boolean + * @return object + */ + private function _load_controller($uri = '', $params = array(), $return = FALSE) { + $router = & $this->_ci_get_component('router'); + + // Back up current router values (before loading new controller) + $backup = array(); + foreach (array('directory', 'class', 'method', 'module') as $prop) { + $backup[$prop] = $router->{$prop}; + } + + // Locate the controller + $segments = $router->locate(explode('/', $uri)); + $class = isset($segments[0]) ? $segments[0] : FALSE; + $method = isset($segments[1]) ? $segments[1] : "index"; + + // Controller not found + if (!$class) { + return; + } + + if (!array_key_exists(strtolower($class), $this->_ci_controllers)) { + // Determine filepath + $filepath = APPPATH . 'controllers/' . $router->fetch_directory() . $class . '.php'; + + // Load the controller file + if (file_exists($filepath)) { + include_once ($filepath); + } + + // Controller class not found, show 404 + if (!class_exists($class)) { + show_404("{$class}/{$method}"); + } + + // Create a controller object + $this->_ci_controllers[strtolower($class)] = new $class(); + } + + $controller = $this->_ci_controllers[strtolower($class)]; + + // Method does not exists + if (!method_exists($controller, $method)) { + show_404("{$class}/{$method}"); + } + + // Restore router state + foreach ($backup as $prop => $value) { + $router->{$prop} = $value; + } + + // Capture output and return + ob_start(); + $result = call_user_func_array(array($controller, $method), $params); + + // Return the buffered output + if ($return === TRUE) { + $buffer = ob_get_contents(); + @ob_end_clean(); + return $buffer; + } + + // Close buffer and flush output to screen + ob_end_flush(); + + // Return controller return value + return $result; + } + + /** + * Detects the module from a string. Returns the module name and class if found. + * + * @param string + * @return array|boolean + */ + private function detect_module($class) { + $class = str_replace('.php', '', trim($class, '/')); + if (($first_slash = strpos($class, '/')) !== FALSE) { + $module = substr($class, 0, $first_slash); + $class = substr($class, $first_slash + 1); + + // Check if module exists + if ($this->find_module($module)) { + return array($module, $class); + } + } + + return FALSE; + } + + /** + * Searches a given module name. Returns the path if found, FALSE otherwise + * + * @param string $module + * @return string|boolean + */ + private function find_module($module) { + $config = & $this->_ci_get_component('config'); + + // Check all locations for this module + foreach ($config->item('modules_locations') as $location) { + $path = $location . rtrim($module, '/') . '/'; + if (is_dir($path)) { + return $path; + } + } + + return FALSE; + } +} \ No newline at end of file diff --git a/application/third_party/HMVC/Router.php b/application/third_party/HMVC/Router.php new file mode 100644 index 0000000..2e5f392 --- /dev/null +++ b/application/third_party/HMVC/Router.php @@ -0,0 +1,297 @@ +config =& load_class('Config', 'core'); + + // Process 'modules_locations' from config + $locations = $this->config->item('modules_locations'); + + if (!$locations) { + $locations = array(APPPATH . 'modules/'); + } else if (!is_array($locations)) { + $locations = array($locations); + } + + // Make sure all paths are the same format + foreach ($locations as &$location) { + $location = realpath($location); + $location = str_replace('\\', '/', $location); + $location = rtrim($location, '/') . '/'; + } + + $this->config->set_item('modules_locations', $locations); + + + parent::__construct(); + } + + /** + * Validates the supplied segments. Attempts to determine the path to + * the controller. + * + * @access private + * @param array + * @return array + */ + function _validate_request($segments) { + if (count($segments) == 0) { + return $segments; + } + + // Locate the controller with modules support + if ($located = $this->locate($segments)) { + return $located; + } + + // Is there a 404 override? + if (!empty($this->routes['404_override'])) { + $segments = explode('/', $this->routes['404_override']); + if ($located = $this->locate($segments)) { + return $located; + } + } + + // Nothing else to do at this point but show a 404 + show_404($segments[0]); + } + + /** + * Parse Routes + * + * This function matches any routes that may exist in + * the config/routes.php file against the URI to + * determine if the class/method need to be remapped. + * + * NOTE: The first segment must stay the name of the + * module, otherwise it is impossible to detect + * the current module in this method. + * + * @access private + * @return void + */ + function _parse_routes() { + // Apply the current module's routing config + + // CI v3.x has URI starting at segment 1 + $segstart = (intval(substr(CI_VERSION,0,1)) > 2) ? 1 : 0; + + if ($module = $this->uri->segment($segstart)) { + foreach ($this->config->item('modules_locations') as $location) { + if (is_file($file = $location . $module . '/config/routes.php')) { + include ($file); + + $route = (!isset($route) or !is_array($route)) ? array() : $route; + $this->routes = array_merge($this->routes, $route); + unset($route); + } + } + } + + // Let parent do the heavy routing + return parent::_parse_routes(); + } + + /** + * The logic of locating a controller is grouped in this function + * + * @param array + * @return array + */ + function locate($segments) { + // anon function to ucfirst a string if CI ver > 2 (for backwards compatibility) + $_ucfirst = function($cn) {return (intval(substr(CI_VERSION,0,1)) > 2) ? ucfirst($cn) : $cn;}; + + list($module, $directory, $controller) = array_pad($segments, 3, NULL); + + foreach ($this->config->item('modules_locations') as $location) { + $relative = $location; + + // Make path relative to controllers directory + $start = rtrim(realpath(APPPATH), '/'); + $parts = explode('/', str_replace('\\', '/', $start)); + + // Iterate all parts and replace absolute part with relative part + for ($i = 1; $i <= count($parts); $i++) { + $relative = str_replace(implode('/', $parts) . '/', str_repeat('../', $i), $relative, $count); + array_pop($parts); + + // Stop iteration if found + if ($count) + break; + } + + // Does a module exist? (/modules/xyz/controllers/) + if (is_dir($source = $location . $module . '/controllers/')) { + $this->module = $module; + $this->directory = $relative . $module . '/controllers/'; + + // Module root controller? + if ($directory && is_file($source . $_ucfirst($directory) . '.php')) { + $this->class = $directory; + return array_slice($segments, 1); + } + + // Module sub-directory? + if ($directory && is_dir($source . $directory . '/')) { + $source = $source . $directory . '/'; + $this->directory .= $directory . '/'; + + // Module sub-directory controller? + if (is_file($source . $_ucfirst($directory) . '.php')) { + return array_slice($segments, 1); + } + + // Module sub-directory default controller? + if (is_file($source . $_ucfirst($this->default_controller) . '.php')) { + $segments[1] = $this->default_controller; + return array_slice($segments, 1); + } + + // Module sub-directory sub-controller? + if ($controller && is_file($source . $_ucfirst($controller) . '.php')) { + return array_slice($segments, 2); + } + } + + // Module controller? + if (is_file($source . $_ucfirst($module) . '.php')) { + return $segments; + } + + // Module default controller? + if (is_file($source . $_ucfirst($this->default_controller) . '.php')) { + $segments[0] = $this->default_controller; + return $segments; + } + } + } + + // Root folder controller? + if (is_file(APPPATH . 'controllers/' . $_ucfirst($module) . '.php')) { + return $segments; + } + + // Sub-directory controller? + if ($directory && is_file(APPPATH . 'controllers/' . $module . '/' . $_ucfirst($directory) . '.php')) { + $this->directory = $module . '/'; + return array_slice($segments, 1); + } + + // Default controller? + if (is_file(APPPATH . 'controllers/' . $module . '/' . $_ucfirst($this->default_controller) . '.php')) { + $segments[0] = $this->default_controller; + return $segments; + } + } + + /** + * Set the module name + * + * @param string + * @return void + */ + function set_module($module) { + $this->module = $module; + } + + /** + * Set default controller + * + * First we check in normal APPPATH/controller's location, + * then in Modules named after the default_controller + * @author hArpanet - based on system/core/Router.php + * + * @return void + */ + protected function _set_default_controller() + { + // controller in APPPATH/controllers takes priority over module with same name + parent::_set_default_controller(); + + // see if parent found a controller + $class = $this->fetch_class(); + + if (empty($class)) { + + // no 'normal' controller found, + // get the class/method from the default_controller route + if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) + { + $method = 'index'; + } + + // try to locate default controller in modules + if ($located = $this->locate(array($class, $class, $method))) { + + log_message('debug', 'No URI present. Default module controller set.'); + } + } + + // Nothing found - this will trigger 404 later + } + + // -------------------------------------------------------------------- + + + /** + * Fetch the module + * + * @access public + * @return string + */ + function fetch_module() { + return $this->module; + } +} From 25675e34c83ed40649770a1a9b300a2ff266a740 Mon Sep 17 00:00:00 2001 From: w3prodigy Date: Fri, 29 Jul 2016 17:32:33 -0400 Subject: [PATCH 2/5] moved application files into application directory to allow for hmvc module code to be used as an upstream --- core/MY_Loader.php | 8 - core/MY_Router.php | 8 - third_party/HMVC/Loader.php | 527 ------------------------------------ third_party/HMVC/Router.php | 297 -------------------- 4 files changed, 840 deletions(-) delete mode 100644 core/MY_Loader.php delete mode 100644 core/MY_Router.php delete mode 100644 third_party/HMVC/Loader.php delete mode 100644 third_party/HMVC/Router.php diff --git a/core/MY_Loader.php b/core/MY_Loader.php deleted file mode 100644 index 1e3f9a2..0000000 --- a/core/MY_Loader.php +++ /dev/null @@ -1,8 +0,0 @@ -_ci_get_component('router'); - if ($router->module) { - $this->add_module($router->module); - } - } - - /** - * Controller Loader - * - * This function lets users load and hierarchical controllers to enable HMVC support - * - * @param string the uri to the controller - * @param array parameters for the requested method - * @param boolean return the result instead of showing it - * @return void - */ - public function controller($uri, $params = array(), $return = FALSE) { - // No valid module detected, add current module to uri - list($module) = $this->detect_module($uri); - if (!isset($module)) { - $router = & $this->_ci_get_component('router'); - if ($router->module) { - $module = $router->module; - $uri = $module . '/' . $uri; - } - } - - // Add module - $this->add_module($module); - - // Execute the controller method and capture output - $void = $this->_load_controller($uri, $params, $return); - - // Remove module - $this->remove_module(); - - return $void; - } - - /** - * Class Loader - * - * This function lets users load and instantiate classes. - * It is designed to be called from a user's app controllers. - * - * @param string the name of the class - * @param mixed the optional parameters - * @param string an optional object name - * @return void - */ - public function library($library = '', $params = NULL, $object_name = NULL) { - if (is_array($library)) { - foreach ($library as $class) { - $this->library($class, $params); - } - return; - } - - // Detect module - if (list($module, $class) = $this->detect_module($library)) { - // Module already loaded - if (in_array($module, $this->_ci_modules)) { - return parent::library($class, $params, $object_name); - } - - // Add module - $this->add_module($module); - - // Let parent do the heavy work - $void = parent::library($class, $params, $object_name); - - // Remove module - $this->remove_module(); - - return $void; - } else { - return parent::library($library, $params, $object_name); - } - } - - /** - * Model Loader - * - * This function lets users load and instantiate models. - * - * @param string the name of the class - * @param string name for the model - * @param bool database connection - * @return void - */ - public function model($model, $name = '', $db_conn = FALSE) { - if (is_array($model)) { - foreach ($model as $babe) { - $this->model($babe); - } - return; - } - - // Detect module - if (list($module, $class) = $this->detect_module($model)) { - // Module already loaded - if (in_array($module, $this->_ci_modules)) { - return parent::model($class, $name, $db_conn); - } - - // Add module - $this->add_module($module); - - // Let parent do the heavy work - $void = parent::model($class, $name, $db_conn); - - // Remove module - $this->remove_module(); - - return $void; - } else { - return parent::model($model, $name, $db_conn); - } - } - - /** - * Load View - * - * This function is used to load a "view" file. It has three parameters: - * - * 1. The name of the "view" file to be included. - * 2. An associative array of data to be extracted for use in the view. - * 3. TRUE/FALSE - whether to return the data or load it. In - * some cases it's advantageous to be able to return data so that - * a developer can process it in some way. - * - * @param string - * @param array - * @param bool - * @return void - */ - public function view($view, $vars = array(), $return = FALSE) { - // Detect module - if (list($module, $class) = $this->detect_module($view)) { - // Module already loaded - if (in_array($module, $this->_ci_modules)) { - return parent::view($class, $vars, $return); - } - - // Add module - $this->add_module($module); - - // Let parent do the heavy work - $void = parent::view($class, $vars, $return); - - // Remove module - $this->remove_module(); - - return $void; - } else { - return parent::view($view, $vars, $return); - } - } - - /** - * Loads a config file - * - * @param string - * @param bool - * @param bool - * @return void - */ - public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE) { - // Detect module - if (list($module, $class) = $this->detect_module($file)) { - // Module already loaded - if (in_array($module, $this->_ci_modules)) { - return parent::config($class, $use_sections, $fail_gracefully); - } - - // Add module - $this->add_module($module); - - // Let parent do the heavy work - $void = parent::config($class, $use_sections, $fail_gracefully); - - // Remove module - $this->remove_module(); - - return $void; - } else { - parent::config($file, $use_sections, $fail_gracefully); - } - } - - /** - * Load Helper - * - * This function loads the specified helper file. - * - * @param mixed - * @return void - */ - public function helper($helper = array()) { - if (is_array($helper)) { - foreach ($helper as $help) { - $this->helper($help); - } - return; - } - - // Detect module - if (list($module, $class) = $this->detect_module($helper)) { - // Module already loaded - if (in_array($module, $this->_ci_modules)) { - return parent::helper($class); - } - - // Add module - $this->add_module($module); - - // Let parent do the heavy work - $void = parent::helper($class); - - // Remove module - $this->remove_module(); - - return $void; - } else { - return parent::helper($helper); - } - } - - /** - * Loads a language file - * - * @param array - * @param string - * @return void - */ - public function language($file = array(), $lang = '') { - if (is_array($file)) { - foreach ($file as $langfile) { - $this->language($langfile, $lang); - } - return; - } - - // Detect module - if (list($module, $class) = $this->detect_module($file)) { - // Module already loaded - if (in_array($module, $this->_ci_modules)) { - return parent::language($class, $lang); - } - - // Add module - $this->add_module($module); - - // Let parent do the heavy work - $void = parent::language($class, $lang); - - // Remove module - $this->remove_module(); - - return $void; - } else { - return parent::language($file, $lang); - } - } - - /** - * Load Widget - * - * This function provides support to Jens Segers Template Library for loading - * widget controllers within modules (place in module/widgets folder). - * @author hArpanet - 23-Jun-2014 - * - * @param string $widget Must contain Module name if widget within a module - * (eg. test/nav where module name is 'test') - * @return array|false - */ - public function widget($widget) { - - // Detect module - if (list($module, $widget) = $this->detect_module($widget)) { - // Module already loaded - if (in_array($module, $this->_ci_modules)) { - return array($module, $widget); - } - - // Add module - $this->add_module($module); - - // Look again now we've added new module path - $void = $this->widget($module.'/'.$widget); - - // Remove module if widget not found within it - if (!$void) { - $this->remove_module(); - } - - return $void; - - } else { - // widget not found in module - return FALSE; - } - } - - /** - * Add Module - * - * Allow resources to be loaded from this module path - * - * @param string - * @param boolean - */ - public function add_module($module, $view_cascade = TRUE) { - if ($path = $this->find_module($module)) { - // Mark module as loaded - array_unshift($this->_ci_modules, $module); - - // Add package path - parent::add_package_path($path, $view_cascade); - } - } - - /** - * Remove Module - * - * Remove a module from the allowed module paths - * - * @param type - * @param bool - */ - public function remove_module($module = '', $remove_config = TRUE) { - if ($module == '') { - // Mark module as not loaded - array_shift($this->_ci_modules); - - // Remove package path - parent::remove_package_path('', $remove_config); - } else if (($key = array_search($module, $this->_ci_modules)) !== FALSE) { - if ($path = $this->find_module($module)) { - // Mark module as not loaded - unset($this->_ci_modules[$key]); - - // Remove package path - parent::remove_package_path($path, $remove_config); - } - } - } - - /** - * Controller loader - * - * This function is used to load and instantiate controllers - * - * @param string - * @param array - * @param boolean - * @return object - */ - private function _load_controller($uri = '', $params = array(), $return = FALSE) { - $router = & $this->_ci_get_component('router'); - - // Back up current router values (before loading new controller) - $backup = array(); - foreach (array('directory', 'class', 'method', 'module') as $prop) { - $backup[$prop] = $router->{$prop}; - } - - // Locate the controller - $segments = $router->locate(explode('/', $uri)); - $class = isset($segments[0]) ? $segments[0] : FALSE; - $method = isset($segments[1]) ? $segments[1] : "index"; - - // Controller not found - if (!$class) { - return; - } - - if (!array_key_exists(strtolower($class), $this->_ci_controllers)) { - // Determine filepath - $filepath = APPPATH . 'controllers/' . $router->fetch_directory() . $class . '.php'; - - // Load the controller file - if (file_exists($filepath)) { - include_once ($filepath); - } - - // Controller class not found, show 404 - if (!class_exists($class)) { - show_404("{$class}/{$method}"); - } - - // Create a controller object - $this->_ci_controllers[strtolower($class)] = new $class(); - } - - $controller = $this->_ci_controllers[strtolower($class)]; - - // Method does not exists - if (!method_exists($controller, $method)) { - show_404("{$class}/{$method}"); - } - - // Restore router state - foreach ($backup as $prop => $value) { - $router->{$prop} = $value; - } - - // Capture output and return - ob_start(); - $result = call_user_func_array(array($controller, $method), $params); - - // Return the buffered output - if ($return === TRUE) { - $buffer = ob_get_contents(); - @ob_end_clean(); - return $buffer; - } - - // Close buffer and flush output to screen - ob_end_flush(); - - // Return controller return value - return $result; - } - - /** - * Detects the module from a string. Returns the module name and class if found. - * - * @param string - * @return array|boolean - */ - private function detect_module($class) { - $class = str_replace('.php', '', trim($class, '/')); - if (($first_slash = strpos($class, '/')) !== FALSE) { - $module = substr($class, 0, $first_slash); - $class = substr($class, $first_slash + 1); - - // Check if module exists - if ($this->find_module($module)) { - return array($module, $class); - } - } - - return FALSE; - } - - /** - * Searches a given module name. Returns the path if found, FALSE otherwise - * - * @param string $module - * @return string|boolean - */ - private function find_module($module) { - $config = & $this->_ci_get_component('config'); - - // Check all locations for this module - foreach ($config->item('modules_locations') as $location) { - $path = $location . rtrim($module, '/') . '/'; - if (is_dir($path)) { - return $path; - } - } - - return FALSE; - } -} \ No newline at end of file diff --git a/third_party/HMVC/Router.php b/third_party/HMVC/Router.php deleted file mode 100644 index 2e5f392..0000000 --- a/third_party/HMVC/Router.php +++ /dev/null @@ -1,297 +0,0 @@ -config =& load_class('Config', 'core'); - - // Process 'modules_locations' from config - $locations = $this->config->item('modules_locations'); - - if (!$locations) { - $locations = array(APPPATH . 'modules/'); - } else if (!is_array($locations)) { - $locations = array($locations); - } - - // Make sure all paths are the same format - foreach ($locations as &$location) { - $location = realpath($location); - $location = str_replace('\\', '/', $location); - $location = rtrim($location, '/') . '/'; - } - - $this->config->set_item('modules_locations', $locations); - - - parent::__construct(); - } - - /** - * Validates the supplied segments. Attempts to determine the path to - * the controller. - * - * @access private - * @param array - * @return array - */ - function _validate_request($segments) { - if (count($segments) == 0) { - return $segments; - } - - // Locate the controller with modules support - if ($located = $this->locate($segments)) { - return $located; - } - - // Is there a 404 override? - if (!empty($this->routes['404_override'])) { - $segments = explode('/', $this->routes['404_override']); - if ($located = $this->locate($segments)) { - return $located; - } - } - - // Nothing else to do at this point but show a 404 - show_404($segments[0]); - } - - /** - * Parse Routes - * - * This function matches any routes that may exist in - * the config/routes.php file against the URI to - * determine if the class/method need to be remapped. - * - * NOTE: The first segment must stay the name of the - * module, otherwise it is impossible to detect - * the current module in this method. - * - * @access private - * @return void - */ - function _parse_routes() { - // Apply the current module's routing config - - // CI v3.x has URI starting at segment 1 - $segstart = (intval(substr(CI_VERSION,0,1)) > 2) ? 1 : 0; - - if ($module = $this->uri->segment($segstart)) { - foreach ($this->config->item('modules_locations') as $location) { - if (is_file($file = $location . $module . '/config/routes.php')) { - include ($file); - - $route = (!isset($route) or !is_array($route)) ? array() : $route; - $this->routes = array_merge($this->routes, $route); - unset($route); - } - } - } - - // Let parent do the heavy routing - return parent::_parse_routes(); - } - - /** - * The logic of locating a controller is grouped in this function - * - * @param array - * @return array - */ - function locate($segments) { - // anon function to ucfirst a string if CI ver > 2 (for backwards compatibility) - $_ucfirst = function($cn) {return (intval(substr(CI_VERSION,0,1)) > 2) ? ucfirst($cn) : $cn;}; - - list($module, $directory, $controller) = array_pad($segments, 3, NULL); - - foreach ($this->config->item('modules_locations') as $location) { - $relative = $location; - - // Make path relative to controllers directory - $start = rtrim(realpath(APPPATH), '/'); - $parts = explode('/', str_replace('\\', '/', $start)); - - // Iterate all parts and replace absolute part with relative part - for ($i = 1; $i <= count($parts); $i++) { - $relative = str_replace(implode('/', $parts) . '/', str_repeat('../', $i), $relative, $count); - array_pop($parts); - - // Stop iteration if found - if ($count) - break; - } - - // Does a module exist? (/modules/xyz/controllers/) - if (is_dir($source = $location . $module . '/controllers/')) { - $this->module = $module; - $this->directory = $relative . $module . '/controllers/'; - - // Module root controller? - if ($directory && is_file($source . $_ucfirst($directory) . '.php')) { - $this->class = $directory; - return array_slice($segments, 1); - } - - // Module sub-directory? - if ($directory && is_dir($source . $directory . '/')) { - $source = $source . $directory . '/'; - $this->directory .= $directory . '/'; - - // Module sub-directory controller? - if (is_file($source . $_ucfirst($directory) . '.php')) { - return array_slice($segments, 1); - } - - // Module sub-directory default controller? - if (is_file($source . $_ucfirst($this->default_controller) . '.php')) { - $segments[1] = $this->default_controller; - return array_slice($segments, 1); - } - - // Module sub-directory sub-controller? - if ($controller && is_file($source . $_ucfirst($controller) . '.php')) { - return array_slice($segments, 2); - } - } - - // Module controller? - if (is_file($source . $_ucfirst($module) . '.php')) { - return $segments; - } - - // Module default controller? - if (is_file($source . $_ucfirst($this->default_controller) . '.php')) { - $segments[0] = $this->default_controller; - return $segments; - } - } - } - - // Root folder controller? - if (is_file(APPPATH . 'controllers/' . $_ucfirst($module) . '.php')) { - return $segments; - } - - // Sub-directory controller? - if ($directory && is_file(APPPATH . 'controllers/' . $module . '/' . $_ucfirst($directory) . '.php')) { - $this->directory = $module . '/'; - return array_slice($segments, 1); - } - - // Default controller? - if (is_file(APPPATH . 'controllers/' . $module . '/' . $_ucfirst($this->default_controller) . '.php')) { - $segments[0] = $this->default_controller; - return $segments; - } - } - - /** - * Set the module name - * - * @param string - * @return void - */ - function set_module($module) { - $this->module = $module; - } - - /** - * Set default controller - * - * First we check in normal APPPATH/controller's location, - * then in Modules named after the default_controller - * @author hArpanet - based on system/core/Router.php - * - * @return void - */ - protected function _set_default_controller() - { - // controller in APPPATH/controllers takes priority over module with same name - parent::_set_default_controller(); - - // see if parent found a controller - $class = $this->fetch_class(); - - if (empty($class)) { - - // no 'normal' controller found, - // get the class/method from the default_controller route - if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) - { - $method = 'index'; - } - - // try to locate default controller in modules - if ($located = $this->locate(array($class, $class, $method))) { - - log_message('debug', 'No URI present. Default module controller set.'); - } - } - - // Nothing found - this will trigger 404 later - } - - // -------------------------------------------------------------------- - - - /** - * Fetch the module - * - * @access public - * @return string - */ - function fetch_module() { - return $this->module; - } -} From c108bb332e199ecc4a79b4411b658dd9ec821b04 Mon Sep 17 00:00:00 2001 From: w3prodigy Date: Fri, 29 Jul 2016 18:04:27 -0400 Subject: [PATCH 3/5] adding modules folder and moving README to this folder --- README.md => application/modules/README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename README.md => application/modules/README.md (100%) diff --git a/README.md b/application/modules/README.md similarity index 100% rename from README.md rename to application/modules/README.md From 0014fb53529663d5d9e0fa1a2782d04952671f2d Mon Sep 17 00:00:00 2001 From: w3prodigy Date: Fri, 29 Jul 2016 18:07:28 -0400 Subject: [PATCH 4/5] adding installation tips to README --- README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..71b1457 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +To install checkout this repository in to your CodeIgniter root folder. + +Orif you cloned CodeIgniter from Github you can set up this repository as an upstream in your CodeIgniter root folder. From 418e30a0c1365ac6efd1e30eae57131411cab25a Mon Sep 17 00:00:00 2001 From: w3prodigy Date: Fri, 29 Jul 2016 18:08:40 -0400 Subject: [PATCH 5/5] Adding link to Github's fork a repo page --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 71b1457..8d1461e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ -To install checkout this repository in to your CodeIgniter root folder. +To install clone this repository in to your CodeIgniter root folder. -Orif you cloned CodeIgniter from Github you can set up this repository as an upstream in your CodeIgniter root folder. +Or if you cloned CodeIgniter from Github, or your CodeIgniter root folder is already a working repository, you can set up this repository as an upstream in your CodeIgniter root folder. + +See: https://help.github.com/articles/fork-a-repo/