RouterRewrite.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2008 by Sergey S. Sergeev                               *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00012     final class RouterRewrite extends Singleton implements Router, Instantiatable
00013     {
00014         protected $routes       = array();
00015         protected $currentRoute = null;
00016         
00020         protected $request      = null;
00021         
00025         protected $baseUrl      = null;
00026         
00027         protected function __construct()
00028         {
00029             $this->baseUrl = new HttpUrl();
00030         }
00031         
00035         public static function me()
00036         {
00037             return self::getInstance(__CLASS__);
00038         }
00039         
00043         public function setRequest(HttpRequest $request)
00044         {
00045             $this->request = $request;
00046             
00047             return $this;
00048         }
00049         
00053         public function getRequest()
00054         {
00055             return $this->request;
00056         }
00057         
00061         public function resetRequest()
00062         {
00063             $this->request = null;
00064             
00065             return $this;
00066         }
00067         
00071         public function setBaseUrl(HttpUrl $url)
00072         {
00073             $this->baseUrl = $url;
00074             
00075             return $this;
00076         }
00077         
00081         public function getBaseUrl()
00082         {
00083             return $this->baseUrl;
00084         }
00085         
00089         public function resetBaseUrl()
00090         {
00091             $this->baseUrl = null;
00092             
00093             return $this;
00094         }
00095         
00099         public function addRoute($name, RouterRule $route)
00100         {
00101             if ($this->hasRoute($name))
00102                 throw new RouterException(
00103                     "Route with name '{$name}' is already defined"
00104                 );
00105             
00106             $this->routes[$name] = $route;
00107             
00108             return $this;
00109         }
00110         
00114         public function addRoutes(array $routes)
00115         {
00116             foreach ($routes as $name => $route)
00117                 $this->addRoute($name, $route);
00118             
00119             return $this;
00120         }
00121         
00126         public function removeRoute($name)
00127         {
00128             if (!$this->hasRoute($name))
00129                 throw new RouterException(
00130                     "Route '{$name}' is not defined"
00131                 );
00132             
00133             unset($this->routes[$name]);
00134             
00135             return $this;
00136         }
00137         
00141         public function hasRoute($name)
00142         {
00143             return isset($this->routes[$name]);
00144         }
00145         
00150         public function getRoute($name)
00151         {
00152             if (!$this->hasRoute($name))
00153                 throw new RouterException(
00154                     "Route '{$name}' is not defined"
00155                 );
00156             
00157             return $this->routes[$name];
00158         }
00159         
00164         public function getCurrentRoute()
00165         {
00166             if (!isset($this->currentRoute))
00167                 throw new RouterException(
00168                     "Current route is not defined"
00169                 );
00170             
00171             return $this->getRoute($this->currentRoute);
00172         }
00173         
00178         public function getCurrentRouteName()
00179         {
00180             if (!isset($this->currentRoute))
00181                 throw new RouterException(
00182                     "Current route is not defined"
00183                 );
00184             
00185             return $this->currentRoute;
00186         }
00187         
00191         public function getRoutes()
00192         {
00193             return $this->routes;
00194         }
00195         
00199         public function resetRoutes()
00200         {
00201             $this->currentRoute = null;
00202             $this->routes = array();
00203             
00204             return $this;
00205         }
00206         
00213         public function route(HttpRequest $request)
00214         {
00215             $this->setRequest($request);
00216             
00217             foreach (array_reverse($this->routes) as $name => $route) {
00218                 if ($params = $route->match($request)) {
00219                     $this->setRequestParams($request, $params);
00220                     $this->currentRoute = $name;
00221                     
00222                     break;
00223                 }
00224             }
00225             
00226             return $request;
00227         }
00228         
00233         public function assembly(
00234             array $userParams = array(),
00235             $name = null,
00236             $reset = false,
00237             $encode = true
00238         )
00239         {
00240             if ($name === null)
00241                 $name = $this->getCurrentRouteName();
00242             
00243             $route = $this->getRoute($name);
00244             $url = $route->assembly($userParams, $reset, $encode);
00245             
00246             if (!preg_match('|^[a-z]+://|', $url)) {
00247                 if ($this->getBaseUrl())
00248                     $url = rtrim($this->getBaseUrl()->toString(), '/').'/'.$url;
00249                 else
00250                     $url = '/'.$url;
00251             }
00252             
00253             return $url;
00254         }
00255 
00259         public function resetAll()
00260         {
00261             return $this->
00262                 resetBaseUrl()->
00263                 resetRequest()->
00264                 resetRoutes();
00265             
00266             return $this;
00267         }
00268         
00272         protected function setRequestParams(HttpRequest $request, array $params)
00273         {
00274             foreach ($params as $param => $value)
00275                 $request->setAttachedVar($param, $value);
00276             
00277             return $this;
00278         }
00279     }
00280 ?>