PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
  2
  3/* load the modules class */
  4require_once 'Modules'.EXT;
  5
  6/* define the module locations and offset */
  7Modules::$locations = array(
  8	APPPATH.'modules/' => '../modules/',
  9);
 10
 11/**
 12 * Modular Extensions - PHP5
 13 *
 14 * Adapted from the CodeIgniter Core Classes
 15 * @link	http://codeigniter.com
 16 *
 17 * Description:
 18 * This library extends the CodeIgniter router class.
 19 *
 20 * Install this file as application/libraries/MY_Router.php
 21 *
 22 * @copyright	Copyright (c) Wiredesignz 2011-01-18
 23 * @version 	5.2.31
 24 * 
 25 * Permission is hereby granted, free of charge, to any person obtaining a copy
 26 * of this software and associated documentation files (the "Software"), to deal
 27 * in the Software without restriction, including without limitation the rights
 28 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 29 * copies of the Software, and to permit persons to whom the Software is
 30 * furnished to do so, subject to the following conditions:
 31 * 
 32 * The above copyright notice and this permission notice shall be included in
 33 * all copies or substantial portions of the Software.
 34 * 
 35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 38 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 39 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 40 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 41 * THE SOFTWARE.
 42 **/
 43class MY_Router extends CI_Router
 44{
 45	private $module;
 46	
 47	public function fetch_module() {
 48		return $this->module;
 49	}
 50	
 51	public function _validate_request($segments) {		
 52		
 53		/* locate module controller */
 54		if ($located = $this->locate($segments)) return $located;
 55		
 56		/* use a default 404 controller */
 57		if (isset($this->routes['404']) AND $segments = explode('/', $this->routes['404'])) {
 58			if ($located = $this->locate($segments)) return $located;
 59		}
 60		
 61		/* no controller found */
 62		show_404();
 63	}
 64	
 65	/** Locate the controller **/
 66	public function locate($segments) {		
 67		
 68		$this->module = '';
 69		$this->directory = '';
 70		$ext = $this->config->item('controller_suffix').EXT;
 71		
 72		/* use module route if available */
 73		if (isset($segments[0]) AND $routes = Modules::parse_routes($segments[0], implode('/', $segments))) {
 74			$segments = $routes;
 75		}
 76	
 77		/* get the segments array elements */
 78		list($module, $directory, $controller) = array_pad($segments, 3, NULL);
 79
 80		foreach (Modules::$locations as $location => $offset) {
 81		
 82			/* module exists? */
 83			if (is_dir($source = $location.$module.'/controllers/')) {
 84				
 85				$this->module = $module;
 86				$this->directory = $offset.$module.'/controllers/';
 87				
 88				/* module sub-controller exists? */
 89				if($directory AND is_file($source.$directory.$ext)) {
 90					return array_slice($segments, 1);
 91				}
 92					
 93				/* module sub-directory exists? */
 94				if($directory AND is_dir($module_subdir = $source.$directory.'/')) {
 95							
 96					$this->directory .= $directory.'/';
 97
 98					/* module sub-directory controller exists? */
 99					if(is_file($module_subdir.$directory.$ext)) {
100						return array_slice($segments, 1);
101					}
102				
103					/* module sub-directory sub-controller exists? */
104					if($controller AND is_file($module_subdir.$controller.$ext))	{
105						return array_slice($segments, 2);
106					}
107				}
108			
109				/* module controller exists? */			
110				if(is_file($source.$module.$ext)) {
111					return $segments;
112				}
113			}
114		}
115		
116		/* application controller exists? */			
117		if(is_file(APPPATH.'controllers/'.$module.$ext)) {
118			return $segments;
119		}
120		
121		/* application sub-directory controller exists? */
122		if(is_file(APPPATH.'controllers/'.$module.'/'.$directory.$ext)) {
123			$this->directory = $module.'/';
124			return array_slice($segments, 1);
125		}
126	}
127	
128	public function set_class($class) {
129		$this->class = $class.$this->config->item('controller_suffix');
130	}
131}