Public paste
regexp-routing-table
By: fruffl | Date: Jan 10 2010 00:14 | Format: PHP | Expires: never | Size: 7.33 KB | Hits: 1057

  1. [ILLI_ROUTING_TABLE]
  2.          => array (0015)
  3.                 [/]
  4.                  => array (0002)
  5.                         [controller]
  6.                          => string (9) Frontpage
  7.                         [regexp]
  8.                          => string (1) /
  9.                 [site/]
  10.                  => array (0003)
  11.                         [controller]
  12.                          => string (7) default
  13.                         [module]
  14.                          => string (4) site
  15.                         [regexp]
  16.                          => string (5) site/
  17.                 [site/blog(/d{1,})?]
  18.                  => array (0004)
  19.                         [controller]
  20.                          => string (4) blog
  21.                         [module]
  22.                          => string (4) site
  23.                         [params]
  24.                          => string (8) int∴page
  25.                         [regexp]
  26.                          => string (19) site/blog(/d{1,})?
  27.                 [site/blog/add-entry/]
  28.                  => array (0004)
  29.                         [action]
  30.                          => string (9) add_entry
  31.                         [controller]
  32.                          => string (4) blog
  33.                         [module]
  34.                          => string (4) site
  35.                         [regexp]
  36.                          => string (20) site/blog/add-entry/
  37.                 [site/blog/by-day(/d{4}/d{2}/d{2}(/d{1,})?)?]
  38.                  => array (0005)
  39.                         [action]
  40.                          => string (14) article_by_day
  41.                         [controller]
  42.                          => string (4) blog
  43.                         [module]
  44.                          => string (4) site
  45.                         [params]
  46.                          => string (38) int∴year,∴int∴month,∴int∴day,∴int∴page
  47.                         [regexp]
  48.                          => string (47) site/blog/by-day(/d{4}/d{2}/d{2}(/d{1,})?)?
  49.  
  50.  
  51. <?php
  52.  
  53.         FINAL CLASS FRUFFI_Dispatcher extends FRUFFI_InstancieOpziener
  54.         {      
  55.                
  56.                 private static $valid_routing_args = array('controller', 'module', 'regexp', 'action', 'view', 'params');
  57.                 private static $routing = array();
  58.                
  59.                 public static function init()
  60.                 {
  61.                         return self::instance(__CLASS__);
  62.                 }
  63.                        
  64.                 public function run()
  65.                 {
  66.                         $this->activate(__CLASS__);
  67.                         $this->execute();
  68.                         return $this;
  69.                 }
  70.                        
  71.                 protected function execute()
  72.                 {
  73.                         FRUFFI::registryAdd
  74.                         (
  75.                                 'FRUFFI_ENVIRONMENT_REMOTE_IP',
  76.                                 $_SERVER['REMOTE_ADDR']
  77.                         );
  78.                        
  79.                         FRUFFI::registryAdd
  80.                         (
  81.                                 'FRUFFI_ENVIRONMENT',
  82.                                 (
  83.                                         FRUFFI::registryGet('FRUFFI_ENVIRONMENT_DEVELOPMENT_IP') == $_SERVER['REMOTE_ADDR']
  84.                                                 ? 'DEVELOPMENT'
  85.                                                 : 'PRODUCTION'
  86.                                 )
  87.                         );
  88.                        
  89.                         FRUFFI::registryAdd
  90.                         (
  91.                                 'FRUFFI_ROUTING_TABLE',
  92.                                 self::$routing
  93.                         );
  94.                        
  95.                        
  96.                         /*
  97.                         FRUFFI::registryAdd
  98.                         (
  99.                                 'FRUFFI_APPLICATION_ROUTING_INI',
  100.                                 parse_ini_file(FRUFFI::registryGet('FRUFFI_FILE_SYSTEM_APP_ROUTING'), TRUE)
  101.                         );
  102.                         */
  103.                        
  104.                         require_once FRUFFI::registryGet('FRUFFI_FILE_SYSTEM_APP_ROUTING');
  105.                        
  106.                         FRUFFI::registryAdd
  107.                         (
  108.                                 'FRUFFI_ENVIRONMENT_URI',
  109.                                 $this->getUri()
  110.                         );
  111.                                                
  112.                         FRUFFI::registryAdd
  113.                         (
  114.                                 'FRUFFI_ENVIRONMENT_URIBASE',
  115.                                 $this->getBase
  116.                                 (
  117.                                         FRUFFI::registryGet('FRUFFI_ENVIRONMENT_URI'),
  118.                                         FRUFFI::registryGet('FRUFFI_ENVIRONMENT_REWRITE_BASE_'.FRUFFI::registryGet('FRUFFI_ENVIRONMENT'))
  119.                                 )
  120.                         );
  121.                        
  122.                         FRUFFI::registryAdd
  123.                         (
  124.                                 'FRUFFI_ENVIRONMENT_URIPATH',
  125.                                 $this->getPath
  126.                                 (
  127.                                         FRUFFI::registryGet('FRUFFI_ENVIRONMENT_URI'),
  128.                                         FRUFFI::registryGet('FRUFFI_ENVIRONMENT_URIBASE')
  129.                                 )
  130.                         );
  131.                        
  132.                         $this->dispatch(FRUFFI::registryGet('FRUFFI_ENVIRONMENT_URIPATH'));
  133.                 }
  134.                
  135.                
  136.                 private function addRouting(array $route)
  137.                 {
  138.                
  139.                         try
  140.                         {
  141.                                 if(!isset($route['regexp']))
  142.                                         throw new FRUFFI_ExceptionDispatcher
  143.                                                 ('No Expression for Routing-Argument found.');
  144.                                                
  145.                                 if(array_key_exists($route['regexp'], self::$routing))
  146.                                         throw new FRUFFI_ExceptionDispatcher
  147.                                                 ('Route '.$parsed['regexp'].' already defined.');
  148.                                
  149.                                 foreach($route as $arg => $value)
  150.                                 {
  151.                                         if(!in_array(strtolower($arg), self::$valid_routing_args))
  152.                                                 throw new FRUFFI_ExceptionDispatcher
  153.                                                         ('Unknown classification for Routing-Argument: '.$arg);
  154.                                        
  155.                                         if(!is_string($value))
  156.                                                 throw new FRUFFI_ExceptionDispatcher
  157.                                                         ('Value for argument '.$arg.' is not a string');
  158.                                                        
  159.                                         if($arg != 'params')
  160.                                                 $value = preg_replace(array('/t/', '/s/', '/r/', '/n/'), NULL, $value);
  161.                                                
  162.                                         if($arg == 'params')
  163.                                         {
  164.                                                 $value = array_map('trim', explode(',', preg_replace(array('/t/', '/r/', '/n/'), NULL, $value)));
  165.                                                 foreach($value as $index => $var)
  166.                                                         $value[$index] = explode(' ', $var);
  167.                                         }
  168.                                        
  169.                                         $parsed[$arg] = $value;
  170.                                        
  171.                                 }
  172.                                
  173.                                 self::$routing[$parsed['regexp']] = $route;
  174.                                 FRUFFI::registryUpdate('FRUFFI_ROUTING_TABLE', self::$routing);
  175.                                        
  176.                         }
  177.                         catch(FRUFFI_Exception $e){FRUFFI_Exception::w($e);}
  178.                        
  179.                         return $this;
  180.                 }
  181.                
  182.                
  183.                 private function dispatch($path)
  184.                 {
  185.                
  186.                         if($path == '/' && array_key_exists('/', FRUFFI::registryGet('FRUFFI_ROUTING_TABLE')))
  187.                         {
  188.                                 $this->invoke('/', FRUFFI::registryGet('FRUFFI_ROUTING_TABLE', '/'));
  189.                                 return;
  190.                         }
  191.                        
  192.                        
  193.                         foreach(FRUFFI::registryGet('FRUFFI_ROUTING_TABLE') as $application => $config)
  194.                         {
  195.                                 $regex = "|^/?". str_replace('*', '?.*', $application) . "$|";
  196.                                
  197.                                 if(substr($path, 0, 1) != '/')
  198.                                         $path = '/'.$path;
  199.                                 if(substr($path, -1, 1) == '/')
  200.                                         $path = rtrim($path, '/');
  201.                                
  202.                                 if(substr($application, 0, 1) != '/')
  203.                                         $application = '/'.$application;
  204.                                 if(substr($application, -1, 1) == '/')
  205.                                         $application = rtrim($application, '/');
  206.                                        
  207.                                
  208.                                
  209.                                 if($path == $application)
  210.                                 {
  211.                                         $this->invoke($application, $config);
  212.                                         return;
  213.                                 }
  214.                                
  215.                                 if (preg_match($regex, $path))
  216.                                 {
  217.                                         $this->invoke($application, $config);
  218.                                         return;
  219.                                 }
  220.                         }
  221.                        
  222.                         $this->routingNotFound();
  223.                        
  224.                 }
  225.                
  226.                 private function routingNotFound()
  227.                 {
  228.                         FRUFFI::registryUpdate('FRUFFI_PUBLIC_ERROR_ROUTING', TRUE);
  229.                 }
  230.                
  231.                 private function invoke($application, $config)
  232.                 {
  233.                         if(!array_key_exists('module', $config))
  234.                                 $config['module'] = 'default';
  235.                         if(!array_key_exists('controller', $config))
  236.                                 $config['controller'] = 'default';
  237.                         if(!array_key_exists('action', $config))
  238.                                 $config['action'] = 'default';
  239.                                
  240.                         v($application, $config);
  241.                 }
  242.         }
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249. /*================
  250. config-file
  251. */
  252.  
  253.  
  254.         $this  
  255.         ->addRouting
  256.         (
  257.                 array
  258.                 (
  259.                         'regexp' => '/',
  260.                         'controller' => 'Frontpage'
  261.                 )
  262.         )
  263.         ->addRouting
  264.         (
  265.                 array
  266.                 (
  267.                         'regexp' => 'site/',
  268.                         'controller' => 'default',
  269.                         'module' => 'site'
  270.                 )
  271.         )
  272.         ->addRouting
  273.         (
  274.                 array
  275.                 (
  276.                         'regexp' => 'site/blog(/d{1,})?',
  277.                         'controller' => 'blog',
  278.                         'module' => 'site',
  279.                         'params' => 'int page'
  280.                 )
  281.         )
  282.         ->addRouting
  283.         (
  284.                 array
  285.                 (
  286.                         'regexp' => 'site/blog/by-year(/d{4}(/d{1,})?)?',
  287.                         'controller' => 'blog',
  288.                         'module' => 'site',
  289.                         'action' => 'article_by_year',
  290.                         'params' => 'int year, int page'
  291.                 )
  292.         )
  293.         ->addRouting
  294.         (
  295.                 array
  296.                 (
  297.                         'regexp' => 'site/blog/list-by-year(/d{4}(/d{1,})?)?',
  298.                         'controller' => 'blog',
  299.                         'module' => 'site',
  300.                         'action' => 'articlelist_by_year',
  301.                         'params' => 'int year, int page'
  302.                 )
  303.         )
  304.         ->addRouting
  305.         (
  306.                 array
  307.                 (
  308.                         'regexp' => 'site/blog/by-month(/d{4}/d{2}(/d{1,})?)?',
  309.                         'controller' => 'blog',
  310.                         'module' => 'site',
  311.                         'action' => 'article_by_month',
  312.                         'params' => 'int year, int month, int page'
  313.                 )
  314.         )
  315.         ->addRouting
  316.         (
  317.                 array
  318.                 (
  319.                         'regexp' => 'site/blog/list-by-month(/d{4}/d{2}(/d{1,})?)?',
  320.                         'controller' => 'blog',
  321.                         'module' => 'site',
  322.                         'action' => 'articlelist_by_month',
  323.                         'params' => 'int year, int month, int page'
  324.                 )
  325.         )
  326.         ->addRouting
  327.         (
  328.                 array
  329.                 (
  330.                         'regexp' => 'site/blog/by-day(/d{4}/d{2}/d{2}(/d{1,})?)?',
  331.                         'controller' => 'blog',
  332.                         'module' => 'site',
  333.                         'action' => 'article_by_day',
  334.                         'params' => 'int year, int month, int day, int page'
  335.                 )
  336.         );