ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 }