- <?PHP
- //if(!defined('ILLI_ACCESS')) die("ILLI_ACCESS isn't defined.");
- /**
- * ILLI PACKAGE
- *
- * @category ILLI
- * @package ILLI_API
- * @subpackage ILLI_CORE_API
- * @license New BSD License (IMP FTW!)
- */
- /**
- * MAP-managemant
- *
- * despatcher: ./ILLI/SOURCE/includes/ILLI.core_api.class.php
- *
- * location: ./ILLI/SOURCE/includes/core/ILLI.map.class.php
- *
- * DEMO-Class!
- * exceptions disabled!
- *
- *
- *
- * @category ILLI
- * @package ILLI_API
- * @subpackage ILLI_CORE_API
- * @license New BSD License (IMP FTW!)
- * @see ILLI_Core_Api
- * @see ILLI_InstanceApiScope
- * @see ILLI_InstanceRouting
- */
- class CORE_____ILLI_MAP
- {
- /**
- * extract controller, action, module from env_path
- *
- * @param string $env_path env_path
- * @param array $routing_table your routing-table
- * @param array $request_default_name_table default names for controller, module and action
- * @return array extracted map
- */
- public function extract($env_path, array $routing_table, array $request_default_name_table, $use_wildcard = false)
- {
- // env_path is the extracted request from uri.
- // set the defaults for controller, module and action; defined in ILLI_Configuration
- // key names reserved
- $extracted = $request_default_name_table;
- // remove last slash, if exists
- $env_path = strtolower(rtrim($env_path, '/'));
- // set first slash in env_path, if not exists
- if(substr($env_path, 0, 1) != '/')
- $env_path = '/'.$env_path;
- // extract routing-values from env_path
- $path = $env_path;
- // kick first slash in path for clean explode
- if(substr($path, 0, 1) == '/')
- $path = substr($path, 1, (strlen($path)));
- // create an array from given path-vars
- // path to array and clean-up names
- $path = (!empty($path))
- //? explode('/', preg_replace('/[^a-z0-9_/]/i', '_', $path))
- ? explode('/', $path)
- : array();
- // first, make sure, the routing-keys are fairly valid...
- foreach($routing_table as $key => $value)
- {
- if(substr($key, 0, 1) != '/' && strlen($key) > 1)
- die('first char for <b>'.$key.'</b> in routing-table must be a "/"!');
- if(substr($key, -1) == '/' && strlen($key) > 1)
- die('last char for <b>'.$key.'</b> in routing-table can't be a "/"!');
- }
- $routing_rules = '';
- // reverse-search of routing-rules in routing-table by path
- // get next height in path and find the next valid existing routing-path
- // copy of path-array
- $_path = $path;
- // each path as val
- foreach($path as $val)
- {
- // create router-key from copy
- $_pathstr = '/'.implode('/', $_path);
- // router exists?
- if(array_key_exists($_pathstr, $routing_table))
- {
- // save routing-rules
- $routing_rules = $routing_table[$_pathstr];
- break;
- }
- // unset last entry in copy and return
- unset($_path[(count($_path)-1)]);
- }
- // '/' is root of install-location
- if($env_path != '/' && $env_path != '/index.php')
- {
- if(!is_array($routing_rules))
- // well, in that case the given url is invalid - or your routing-table is crappy configured
- die('no array found for '.$env_path.' in routing-table: 404');
- if(!isset($routing_rules['config']))
- die('missing array-key config for key '.$env_path.' in routing-table');
- if(!is_string($routing_rules['config']))
- die('array-key config for '.$env_path.' in routing-table must contain a string.');
- if(strlen($routing_rules['config']) === 0)
- die('array-key config for '.$env_path.' in routing-table is empty.');
- }
- // rules-config into array
- $rules_config = $routing_rules['config'];
- if(substr($rules_config, 0, 1) == '/')
- $rules_config = substr($rules_config, 1, (strlen($rules_config)));
- $rules_config = rtrim($rules_config, '/');
- $rules_config = (!empty($rules_config))
- ? explode('/', $rules_config)
- : array();
- // set the rules for controller, module and action
- if($rules_config[0] == 'module')
- $extracted['module'] = preg_replace('/[^a-z0-9_/]/i', '_', array_shift($path));
- if($rules_config[1] == 'controller')
- $extracted['controller'] = preg_replace('/[^a-z0-9_/]/i', '_', array_shift($path));
- if($rules_config[2] == 'action')
- $extracted['action'] = preg_replace('/[^a-z0-9_/]/i', '_', array_shift($path));
- // key-name from params to key-name in extraced; values from path
- if(is_array($routing_rules) && array_key_exists('params', $routing_rules))
- {
- // we need a copy of path to remove the orig-key
- $_path = $path;
- $rules_params = $routing_rules['params'];
- if(substr($rules_params, 0, 1) == '/')
- $rules_params = substr($rules_params, 1, (strlen($rules_params)));
- $rules_params = rtrim($rules_params, '/');
- $rules_params = (!empty($rules_params))
- ? explode('/', $rules_params)
- : array();
- $_p = count($rules_params);
- for($i = 0; $i < $_p; $i++)
- {
- $rule = strtolower($rules_params[$i]);
- switch($rule)
- {
- case 'module':
- case 'controller':
- case 'action':
- die('Error in Routing-Table=>params '.$_path[$i].':
- Using rule-name <b>'.$rule.'</b> is forbidden! Rename it.');
- break;
- default:
- $extracted['params'][$rule] = $_path[$i];
- // unset current from original
- array_shift($path);
- break;
- }
- }
- }
- // parse the unregistered path-vars
- // i've added this bool to fix a law-problem in some countries
- // if true, all remaining values will ignored.
- // otherwise, the remaining values will be parsed as key/value;
- if($use_wildcard)
- {
- $i = 0;
- foreach($path as $value)
- {
- if($i % 2)
- // ignore all existing keys
- if(!array_key_exists($path[$i], $extracted))
- $extracted['query'][$path[$i-1]] = $value;
- $i++;
- }
- }
- return $extracted;
- }
- public function class_help()
- {
- }
- public function class_config_help()
- {
- }
- /**
- * class-demonstration
- *
- * @return string extracted maps
- */
- public final function demo()
- {
- $defaults = array(
- 'module' => 'default',
- 'controller' => 'default',
- 'action' => 'default',
- );
- $table = array
- (
- '/error' => array('config' => '/module', 'params' => '/error_code'),
- '/site' => array('config' => '/module'),
- '/site/blog' => array('config' => '/module/controller', 'params' => '/page'),
- '/site/blog/by-day' => array('config' => '/module/controller/action', 'params' => '/year/month/day/page')
- );
- $URIs = array
- (
- '/site/blog/5/irgendein_key/irgendein_value',
- '/site/blog/5/',
- '/site/blog/by-day/2009/12/14',
- '/site/blog/by-anything/2009/12/14',
- '/site/blubberblase',
- );
- $result = '<h1>'.__CLASS__.'::exctract()</h1>';
- $i = 1;
- foreach($URIs as $uri)
- {
- $result .=
- '<h2>Sample #'.$i.'</h2>'
- .'<h4>called uri</h4>'
- .$uri
- .'<h4>given routing-table</h4>'
- .'<pre>'
- .print_r($table, true)
- .'</pre>'
- .'<h4>the extracted path</h4>'
- .'<pre>'
- .print_r($this->extract($uri, $table, $defaults, true), true)
- .'</pre><hr />';
- $i++;
- }
- return $result;
- }
- }
- $map = new CORE_____ILLI_MAP;
- print $map->demo();
MVC-Router
By: fruffl | Date: Dec 15 2009 20:32 | Format: None | Expires: never | Size: 7.58 KB | Hits: 1010
Latest pastes
1 hours ago
11 hours ago
1 days ago
2 days ago
2 days ago