Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.93 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  *
  5.  *
  6.  *  //test URLs
  7.  *  $urlCnt[0]  =   "/information/location";
  8.  *  controller => information, action => location
  9.  *  $urlCnt[1]  =   "/webdesign/show/1";
  10.  *  $urlCnt[2]  =   "/information/show/3/why-you-should-choose-us";
  11.  *  $urlCnt[3]  =   "/information/delete/3/becauseItsOld";
  12.  *  $urlCnt[4]  =   "/information";
  13.  *  controller => information, action => $standartAction
  14.  * @author bs
  15.  *
  16.  */
  17. class Router
  18. {
  19.     static private $_instance;
  20.     private $routes =   array();
  21.     private $url;
  22.     private $standardAction     =   "index";
  23.     private $standardController =   "index";
  24.    
  25.     public function __construct($url)
  26.     {
  27.         $this->url  =   "/" . $url;
  28.     }
  29.    
  30.     public static function getInstance($url = "")
  31.     {
  32.         if ( !(self::$_instance instanceof self) )
  33.         {
  34.             self::$_instance = new self($url);
  35.         }
  36.         return self::$_instance;
  37.     }
  38.        
  39.     /**
  40.      *
  41.      * returns all routes in the router
  42.      */
  43.     public function getRoutes()
  44.     {
  45.         return $this->routes;
  46.     }
  47.    
  48.     /*
  49.      * returns the request
  50.      */
  51.     public function getRequest()
  52.     {
  53.         return $this->generateOutcome();
  54.     }
  55.    
  56.    
  57.     /**
  58.      *
  59.      * sets the standart controller
  60.      * //standard = index
  61.      * @param unknown_type $controller
  62.      */
  63.     public function setStandardController($controller)
  64.     {
  65.         $this->standardController   =   $controller;
  66.     }
  67.    
  68.     /**
  69.      *
  70.      * sets the standart action
  71.      * //standard = index
  72.      * @param unknown_type $action
  73.      */
  74.     public function setStandardAction($action)
  75.     {
  76.         $this->standardAction   =   $action;
  77.     }
  78.    
  79.     /**
  80.      *
  81.      * adds routes to the router
  82.      * @param unknown_type $route
  83.      */
  84.     public function addRoute($route = array() )
  85.     {
  86.         $this->routes[] =   $route;
  87.     }
  88.    
  89.     /**
  90.      *
  91.      * creates an array that will output something like:
  92.      */
  93.     private function generateOutcome()
  94.     {      
  95.         //check if the domain is just the index...
  96.         if ($this->url != "/")
  97.         {
  98.             //some empty stuff for later
  99.             $params         =   array();
  100.             $regex          =   "";
  101.             $routesCNT      =   array();
  102.            
  103.             //prepare the regex to parse the urls
  104.             foreach($this->routes as $route)
  105.             {  
  106.                 $ga =   preg_split("/\//", $route[0] );
  107.                 foreach($ga as $expr)
  108.                 {
  109.                     //check if the name starts with a : - if so than it will be a variable that is needed as an array index
  110.                     //important for later stuff, as controller, action, params always be there as an index
  111.                     if( strpos($expr, ":") === 0 )
  112.                     {
  113.                         //:controller or :action found or whatever -> a regex need to be used
  114.                         $regex  .= "\/[a-zA-Z-0-9]+";
  115.                     }
  116.                     else
  117.                     {
  118.                         //not found therefore what is written is the requirement
  119.                         $regex  .= "\/" . $expr;               
  120.                     }
  121.                                
  122.                 }  
  123.                
  124.                 //put the route back together but this time with the regular expression
  125.                 $rPattern[0]    =   $route[0];
  126.                 $rPattern[1]    =   $route[1];
  127.                 $rPattern[2]    =   $regex;
  128.        
  129.                 $routesCNT[]    =   $rPattern;
  130.                 $regex          =   null;
  131.             }  
  132.            
  133.             //build the final array for the url to usefull stuff
  134.        
  135.             $processor  =   array();
  136.             $finalArray =   array();
  137.             $i          =   1;
  138.             foreach($routesCNT as $route)
  139.             {  
  140.                 if ( preg_match("/^" . $route[2] . "\z/i", $this->url ) == 1  )
  141.                 {
  142.                     //split the url into pieces for the final array
  143.                     $urlValues      =   explode("/", $this->url);  
  144.                     $routeLength    =   count($route[1]);
  145.                    
  146.                     foreach( $route[1] as $name => $val )
  147.                     {          
  148.                         if ( $routeLength > 2 && $routeLength == (count($urlValues) - 1))
  149.                         {
  150.                             //make sure not to override the index
  151.                             if( $val == "")
  152.                             {
  153.                                 $processor[$name]   =   $urlValues[$i];
  154.                             }  
  155.                             else
  156.                             {      
  157.                                 $processor[$name]   =   $val;
  158.                             }
  159.                         }
  160.                         else
  161.                         {
  162.                             $processor['controller']    =   $urlValues[1];
  163.                             $processor['action']        =   $urlValues[2];
  164.                         }
  165.                         $finalArray =   $processor;
  166.                         $i++;
  167.                     }          
  168.                    
  169.                 }
  170.                 else
  171.                 {
  172.                     //here is the $route[1]['params'] regquired!!!
  173.                     //split the url into pieces for the final array
  174.                     $urlValues      =   explode("/", $this->url);  
  175.                     $processor['controller']    =   $urlValues[1];
  176.                     //check if there is no action set
  177.                     if ( count($urlValues) <= 2)
  178.                     {
  179.                         $processor['action']        =   $this->standardAction;                 
  180.                     }
  181.                     else if(empty($urlValues))
  182.                     {
  183.                         $processor['controller']    =   $this->standardController;
  184.                         $processor['action']        =   $this->standardAction;                     
  185.                     }
  186.                     else
  187.                     {
  188.                         $processor['action']        =   $urlValues[2]; 
  189.                         $baseLength     =   strlen($urlValues[1] . "/" . $urlValues[2] );
  190.                         $newUrl         =   substr($this->url, $baseLength + 1);
  191.                         $params     =   explode("/", $newUrl); 
  192.                         array_shift( $params );
  193.                         $processor['params']    =   $params;
  194.                     }
  195.                     $finalArray =   $processor;        
  196.                 }
  197.             }
  198.         }
  199.         else
  200.         {
  201.             //if the requested path is just / - it means basically its the index file therefore show the standard
  202.             $processor['controller']    =   $this->standardController;
  203.             $processor['action']        =   $this->standardAction; 
  204.             $finalArray                 =   $processor;    
  205.         }
  206.         return new ArrayObject( $finalArray, 2);
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement