/halogy/application/libraries/MY_Router.php
https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 131 lines · 59 code · 24 blank · 48 comment · 12 complexity · 998514bebee46904625e379981a6531b MD5 · raw file
- <?php (defined('BASEPATH')) OR exit('No direct script access allowed');
- /* load the modules class */
- require_once 'Modules'.EXT;
- /* define the module locations and offset */
- Modules::$locations = array(
- APPPATH.'modules/' => '../modules/',
- );
- /**
- * Modular Extensions - PHP5
- *
- * Adapted from the CodeIgniter Core Classes
- * @link http://codeigniter.com
- *
- * Description:
- * This library extends the CodeIgniter router class.
- *
- * Install this file as application/libraries/MY_Router.php
- *
- * @copyright Copyright (c) Wiredesignz 2011-01-18
- * @version 5.2.31
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- **/
- class MY_Router extends CI_Router
- {
- private $module;
-
- public function fetch_module() {
- return $this->module;
- }
-
- public function _validate_request($segments) {
-
- /* locate module controller */
- if ($located = $this->locate($segments)) return $located;
-
- /* use a default 404 controller */
- if (isset($this->routes['404']) AND $segments = explode('/', $this->routes['404'])) {
- if ($located = $this->locate($segments)) return $located;
- }
-
- /* no controller found */
- show_404();
- }
-
- /** Locate the controller **/
- public function locate($segments) {
-
- $this->module = '';
- $this->directory = '';
- $ext = $this->config->item('controller_suffix').EXT;
-
- /* use module route if available */
- if (isset($segments[0]) AND $routes = Modules::parse_routes($segments[0], implode('/', $segments))) {
- $segments = $routes;
- }
-
- /* get the segments array elements */
- list($module, $directory, $controller) = array_pad($segments, 3, NULL);
- foreach (Modules::$locations as $location => $offset) {
-
- /* module exists? */
- if (is_dir($source = $location.$module.'/controllers/')) {
-
- $this->module = $module;
- $this->directory = $offset.$module.'/controllers/';
-
- /* module sub-controller exists? */
- if($directory AND is_file($source.$directory.$ext)) {
- return array_slice($segments, 1);
- }
-
- /* module sub-directory exists? */
- if($directory AND is_dir($module_subdir = $source.$directory.'/')) {
-
- $this->directory .= $directory.'/';
- /* module sub-directory controller exists? */
- if(is_file($module_subdir.$directory.$ext)) {
- return array_slice($segments, 1);
- }
-
- /* module sub-directory sub-controller exists? */
- if($controller AND is_file($module_subdir.$controller.$ext)) {
- return array_slice($segments, 2);
- }
- }
-
- /* module controller exists? */
- if(is_file($source.$module.$ext)) {
- return $segments;
- }
- }
- }
-
- /* application controller exists? */
- if(is_file(APPPATH.'controllers/'.$module.$ext)) {
- return $segments;
- }
-
- /* application sub-directory controller exists? */
- if(is_file(APPPATH.'controllers/'.$module.'/'.$directory.$ext)) {
- $this->directory = $module.'/';
- return array_slice($segments, 1);
- }
- }
-
- public function set_class($class) {
- $this->class = $class.$this->config->item('controller_suffix');
- }
- }