Login   Register  
PHP Classes
elePHPant
Icontem

File: routeclass.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Roman Krivosheev  >  Simple Route  >  routeclass.php  >  Download  
File: routeclass.php
Role: Class source
Content type: text/plain
Description: Router class
Class: Simple Route
Parse and build URIs from routing rules
Author: By
Last change: New class version, new search algorithm
Date: 2 years ago
Size: 2,097 bytes
 

Contents

Class file image Download
<?php

    
Class Route
    
{
        
//TRAP REQUESTS ARRAY:
        
public static $request;
        private static 
$param;


        public static function 
matchURI($uri null) {
            
$uri = (!$uri) ? $_SERVER['PATH_INFO'] : $uri;
            
$uri = (!$uri) ? '/' rtrim($uri,"\/");
            if(!empty(
self::$request)) {
                
$count=count(self::$request);
                for(
$i=0$i<$count; ++$i) {
                    foreach(
self::$request[$i] as $k => $v) {
                        if (
is_array($v) and $k !== 'param') {
                            
self::$param self::$request[$i]['param'];
                            
$v['request'] = preg_replace_callback("/\<(?<key>[0-9a-z_]+)\>/",
                                
'Route::_replacer',
                                
str_replace(")",")?"$v['request'])
                            );
                            
$rulleTemp array_merge((array)self::$request[$i], (array)$v);
                            if((
$t self::_reportRulle($rulleTemp$uri)))
                                return 
$t;
                        }
                    }
                }

            } else return array();
        }

        private static function 
_replacer($matches) {
            if(isset(
self::$param[$matches['key']])) {
                return 
"(?<".$matches['key'].">".self::$param[$matches['key']].")";
            } else return 
"(?<".$matches['key'].">"."([^/]+)".")";
        }

        private static function 
_reportRulle($ini_array$uri) {
            if(
is_array($ini_array) and $uri) {
                if(
preg_match("#^".$ini_array['request']."$#"$uri$match)){
                    
$r array_merge((array)$ini_array, (array)$match);
                    foreach(
$r as $k => $v)
                        if((int)
$k OR $k == 'param' OR $k == 'request')
                            unset(
$r[$k]);
                    return 
$r;
                }
            }
        }
        
/** =================================================================== **/
    
}