ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Router.php
Go to the documentation of this file.
1 <?php
45 class Slim_Router implements IteratorAggregate {
46 
50  protected $request;
51 
55  protected $routes;
56 
60  protected $namedRoutes;
61 
65  protected $matchedRoutes;
66 
70  protected $notFound;
71 
75  protected $error;
76 
81  public function __construct( Slim_Http_Request $request ) {
82  $this->request = $request;
83  $this->routes = array();
84  }
85 
90  public function getIterator() {
91  return new ArrayIterator($this->getMatchedRoutes());
92  }
93 
98  public function getRequest() {
99  return $this->request;
100  }
101 
107  public function setRequest( Slim_Http_Request $req ) {
108  $this->request = $req;
109  }
110 
115  public function getMatchedRoutes( $reload = false ) {
116  if ( $reload || is_null($this->matchedRoutes) ) {
117  $this->matchedRoutes = array();
118  foreach ( $this->routes as $route ) {
119  if ( $route->matches($this->request->getResourceUri()) ) {
120  $this->matchedRoutes[] = $route;
121  }
122  }
123  }
124  return $this->matchedRoutes;
125  }
126 
133  public function map( $pattern, $callable ) {
134  $route = new Slim_Route($pattern, $callable);
135  $route->setRouter($this);
136  $this->routes[] = $route;
137  return $route;
138  }
139 
147  public function cacheNamedRoute( $name, Slim_Route $route ) {
148  if ( isset($this->namedRoutes[(string)$name]) ) {
149  throw new RuntimeException('Named route already exists with name: ' . $name);
150  }
151  $this->namedRoutes[$name] = $route;
152  }
153 
161  public function urlFor( $name, $params = array() ) {
162  if ( !isset($this->namedRoutes[(string)$name]) ) {
163  throw new RuntimeException('Named route not found for name: ' . $name);
164  }
165  $pattern = $this->namedRoutes[(string)$name]->getPattern();
166  $search = $replace = array();
167  foreach ( $params as $key => $value ) {
168  $search[] = ':' . $key;
169  $replace[] = $value;
170  }
171  $pattern = str_replace($search, $replace, $pattern);
172  //Remove remnants of unpopulated, trailing optional pattern segments
173  return preg_replace(array(
174  '@\(\/?:.+\/??\)\??@',
175  '@\?|\(|\)@'
176  ), '', $this->request->getRootUri() . $pattern);
177  }
178 
184  public function notFound( $callable = null ) {
185  if ( is_callable($callable) ) {
186  $this->notFound = $callable;
187  }
188  return $this->notFound;
189  }
190 
196  public function error( $callable = null ) {
197  if ( is_callable($callable) ) {
198  $this->error = $callable;
199  }
200  return $this->error;
201  }
202 
203 }
urlFor( $name, $params=array())
Get URL for named route.
Definition: Router.php:161
Add rich text string
The name of the decorator.
error( $callable=null)
Register a 500 Error callback.
Definition: Router.php:196
__construct(Slim_Http_Request $request)
Constructor.
Definition: Router.php:81
getRequest()
Get Request.
Definition: Router.php:98
$matchedRoutes
Definition: Router.php:65
notFound( $callable=null)
Register a 404 Not Found callback.
Definition: Router.php:184
Create styles array
The data for the language used.
getMatchedRoutes( $reload=false)
Return routes that match the current request.
Definition: Router.php:115
setRequest(Slim_Http_Request $req)
Set Request.
Definition: Router.php:107
map( $pattern, $callable)
Map a route to a callback function.
Definition: Router.php:133
getIterator()
Get Iterator.
Definition: Router.php:90
$params
Definition: example_049.php:96
cacheNamedRoute( $name, Slim_Route $route)
Cache named route.
Definition: Router.php:147