Public paste
MVC-Router
By: fruffl | Date: Dec 15 2009 20:32 | Format: None | Expires: never | Size: 7.58 KB | Hits: 907

  1. <?PHP
  2.  
  3.         //if(!defined('ILLI_ACCESS')) die("ILLI_ACCESS isn't defined.");
  4.  
  5.  
  6.  
  7.  
  8.         /**
  9.          * ILLI PACKAGE
  10.          *
  11.          * @category    ILLI
  12.          * @package     ILLI_API
  13.          * @subpackage  ILLI_CORE_API
  14.          * @license     New BSD License (IMP FTW!)
  15.          */
  16.  
  17.  
  18.  
  19.  
  20.         /**
  21.          * MAP-managemant
  22.          *
  23.          * despatcher: ./ILLI/SOURCE/includes/ILLI.core_api.class.php
  24.          *
  25.          * location:   ./ILLI/SOURCE/includes/core/ILLI.map.class.php
  26.          *
  27.          * DEMO-Class!
  28.          * exceptions disabled!
  29.          *
  30.          *
  31.          *
  32.          * @category    ILLI
  33.          * @package     ILLI_API
  34.          * @subpackage  ILLI_CORE_API
  35.          * @license     New BSD License (IMP FTW!)
  36.          * @see ILLI_Core_Api
  37.          * @see ILLI_InstanceApiScope
  38.          * @see ILLI_InstanceRouting
  39.          */    
  40.         class CORE_____ILLI_MAP
  41.         {
  42.                
  43.                
  44.                 /**
  45.                  * extract controller, action, module from env_path
  46.                  *
  47.                  * @param string $env_path              env_path
  48.                  * @param array $routing_table          your routing-table
  49.                  * @param array $request_default_name_table     default names for controller, module and action
  50.                  * @return array extracted map
  51.                  */
  52.                 public function extract($env_path, array $routing_table, array $request_default_name_table, $use_wildcard = false)
  53.                 {
  54.                        
  55.                
  56.                         // env_path is the extracted request from uri.
  57.                        
  58.                         // set the defaults for controller, module and action; defined in ILLI_Configuration
  59.                         // key names reserved
  60.                         $extracted = $request_default_name_table;
  61.                        
  62.                         // remove last slash, if exists
  63.                         $env_path = strtolower(rtrim($env_path, '/'));
  64.                        
  65.                         // set first slash in env_path, if not exists
  66.                         if(substr($env_path, 0, 1) != '/')
  67.                                 $env_path = '/'.$env_path;
  68.                                
  69.                                
  70.                         // extract routing-values from env_path
  71.                         $path = $env_path;
  72.                         // kick first slash in path for clean explode
  73.                         if(substr($path, 0, 1) == '/')
  74.                                 $path = substr($path, 1, (strlen($path)));
  75.  
  76.                         // create an array from given path-vars
  77.                         // path to array and clean-up names
  78.                         $path = (!empty($path))
  79.                                 //? explode('/', preg_replace('/[^a-z0-9_/]/i', '_', $path))
  80.                                 ? explode('/', $path)
  81.                                 : array();
  82.                                
  83.                        
  84.                        
  85.                         // first, make sure, the routing-keys are fairly valid...
  86.                         foreach($routing_table as $key => $value)
  87.                         {
  88.                                 if(substr($key, 0, 1) != '/' && strlen($key) > 1)
  89.                                         die('first char for <b>'.$key.'</b> in routing-table must be a "/"!'); 
  90.                                 if(substr($key, -1) == '/' && strlen($key) > 1)
  91.                                         die('last char for <b>'.$key.'</b> in routing-table can't be a "/"!');                         
  92.                         }
  93.                        
  94.                         $routing_rules = '';
  95.                        
  96.                         // reverse-search of routing-rules in routing-table by path
  97.                         // get next height in path and find the next valid existing routing-path
  98.                
  99.                         // copy of path-array
  100.                         $_path = $path;
  101.                         // each path as val
  102.                         foreach($path as $val)
  103.                         {
  104.                                 // create router-key from copy
  105.                                 $_pathstr = '/'.implode('/', $_path);
  106.                                
  107.                                 // router exists?
  108.                                 if(array_key_exists($_pathstr, $routing_table))
  109.                                 {
  110.                                         // save routing-rules
  111.                                         $routing_rules = $routing_table[$_pathstr];
  112.                                         break;
  113.                                 }
  114.                                        
  115.                                 // unset last entry in copy and return
  116.                                 unset($_path[(count($_path)-1)]);
  117.                         }
  118.                        
  119.                        
  120.                        
  121.                         // '/' is root of install-location
  122.                         if($env_path != '/' && $env_path != '/index.php')
  123.                         {
  124.                                 if(!is_array($routing_rules))
  125.                                         // well, in that case the given url is invalid - or your routing-table is crappy configured
  126.                                         die('no array found for '.$env_path.' in routing-table: 404');
  127.                                        
  128.                                 if(!isset($routing_rules['config']))
  129.                                         die('missing array-key config for key '.$env_path.' in routing-table');
  130.                                        
  131.                                 if(!is_string($routing_rules['config']))
  132.                                         die('array-key config for '.$env_path.' in routing-table must contain a string.');
  133.                                        
  134.                                 if(strlen($routing_rules['config']) === 0)
  135.                                         die('array-key config for '.$env_path.' in routing-table is empty.');  
  136.                         }
  137.                                
  138.                                
  139.                         // rules-config into array
  140.                         $rules_config = $routing_rules['config'];
  141.                         if(substr($rules_config, 0, 1) == '/')
  142.                                 $rules_config = substr($rules_config, 1, (strlen($rules_config)));
  143.                         $rules_config = rtrim($rules_config, '/');
  144.                         $rules_config = (!empty($rules_config))
  145.                                 ? explode('/', $rules_config)
  146.                                 : array();
  147.                                
  148.                        
  149.                         // set the rules for controller, module and action
  150.                         if($rules_config[0] == 'module')
  151.                                 $extracted['module'] = preg_replace('/[^a-z0-9_/]/i', '_', array_shift($path));
  152.                                
  153.                         if($rules_config[1] == 'controller')
  154.                                 $extracted['controller'] = preg_replace('/[^a-z0-9_/]/i', '_', array_shift($path));
  155.                                
  156.                         if($rules_config[2] == 'action')
  157.                                 $extracted['action'] = preg_replace('/[^a-z0-9_/]/i', '_', array_shift($path));
  158.                                
  159.                        
  160.                         // key-name from params to key-name in extraced; values from path
  161.                         if(is_array($routing_rules) && array_key_exists('params', $routing_rules))
  162.                         {
  163.                                 // we need a copy of path to remove the orig-key
  164.                                 $_path = $path;
  165.  
  166.                                 $rules_params = $routing_rules['params'];
  167.                                 if(substr($rules_params, 0, 1) == '/')
  168.                                         $rules_params = substr($rules_params, 1, (strlen($rules_params)));
  169.                                 $rules_params = rtrim($rules_params, '/');
  170.                                 $rules_params = (!empty($rules_params))
  171.                                         ? explode('/', $rules_params)
  172.                                         : array();
  173.                                        
  174.                                 $_p = count($rules_params);
  175.                                 for($i = 0; $i < $_p; $i++)
  176.                                 {
  177.                                         $rule = strtolower($rules_params[$i]);
  178.                                        
  179.                                         switch($rule)
  180.                                         {
  181.                                                 case 'module':
  182.                                                 case 'controller':
  183.                                                 case 'action':
  184.                                                                 die('Error in Routing-Table=>params '.$_path[$i].':
  185.                                                                 Using rule-name <b>'.$rule.'</b> is forbidden! Rename it.');
  186.                                                         break;
  187.                                                 default:                                       
  188.                                                                 $extracted['params'][$rule] = $_path[$i];
  189.                                                        
  190.                                                                 // unset current from original
  191.                                                                 array_shift($path);
  192.                                                         break;
  193.                                         }
  194.                                 }
  195.                         }
  196.                        
  197.                        
  198.                         // parse the unregistered path-vars                    
  199.                        
  200.                         // i've added this bool to fix a law-problem in some countries
  201.                         // if true, all remaining values will ignored.
  202.                         // otherwise, the remaining values will be parsed as key/value;
  203.                         if($use_wildcard)
  204.                         {
  205.                                 $i = 0;
  206.                                 foreach($path as $value)
  207.                                 {
  208.                                         if($i % 2)
  209.                                                 // ignore all existing keys
  210.                                                 if(!array_key_exists($path[$i], $extracted))
  211.                                                         $extracted['query'][$path[$i-1]] = $value;
  212.                                         $i++;
  213.                                 }
  214.                         }
  215.                        
  216.                         return $extracted;
  217.                 }
  218.                
  219.                
  220.                
  221.                 public function class_help()
  222.                 {
  223.                 }
  224.                
  225.                
  226.                
  227.                 public function class_config_help()
  228.                 {
  229.                 }
  230.                
  231.                
  232.                
  233.                 /**
  234.                  * class-demonstration
  235.                  *
  236.                  * @return string extracted maps
  237.                  */
  238.                 public final function demo()
  239.                 {
  240.                         $defaults = array(
  241.                                 'module' => 'default',
  242.                                 'controller' => 'default',
  243.                                 'action' => 'default',
  244.                         );
  245.                        
  246.                         $table = array
  247.                         (
  248.                                 '/error'                => array('config' => '/module', 'params' => '/error_code'),
  249.                                 '/site'                 => array('config' => '/module'),
  250.                                 '/site/blog'            => array('config' => '/module/controller', 'params' => '/page'),
  251.                                 '/site/blog/by-day'     => array('config' => '/module/controller/action', 'params' => '/year/month/day/page')
  252.                         );
  253.                        
  254.                         $URIs = array
  255.                         (
  256.                                 '/site/blog/5/irgendein_key/irgendein_value',
  257.                                 '/site/blog/5/',
  258.                                 '/site/blog/by-day/2009/12/14',
  259.                                 '/site/blog/by-anything/2009/12/14',
  260.                                 '/site/blubberblase',
  261.                         );
  262.  
  263.  
  264.                         $result = '<h1>'.__CLASS__.'::exctract()</h1>';
  265.                         $i = 1;
  266.                         foreach($URIs as $uri)
  267.                         {
  268.                                 $result .=      
  269.                                                 '<h2>Sample #'.$i.'</h2>'
  270.                                                 .'<h4>called uri</h4>'
  271.                                                 .$uri
  272.                                                 .'<h4>given routing-table</h4>'
  273.                                                 .'<pre>'
  274.                                                 .print_r($table, true)
  275.                                                 .'</pre>'
  276.                                                 .'<h4>the extracted path</h4>'
  277.                                                 .'<pre>'
  278.                                                 .print_r($this->extract($uri, $table, $defaults, true), true)
  279.                                                 .'</pre><hr />';
  280.                                                 $i++;
  281.                         }
  282.                        
  283.                         return $result;
  284.                        
  285.                 }
  286.         }
  287.        
  288.        
  289.        
  290.  
  291.  
  292.  
  293.  
  294.  
  295.        
  296. $map = new CORE_____ILLI_MAP;
  297. print $map->demo();