Example #1
0
 public function testInvoke()
 {
     $map = new RouteMap();
     $map->add('/foo/{id}', function () {
     })->name('foo');
     $url = new UrlFunction($map);
     $this->assertSame('/foo/1234', $url('foo', ['id' => 1234]));
 }
Example #2
0
 /**
  * @param string|string[] $methods
  * @param string          $path
  * @param callable        $handler
  * @return Route
  */
 public function route($methods, $path, $handler)
 {
     $route = $this->routeMap->add($path, $handler);
     $this->collector->addRoute($methods, $path, $route);
     $this->middleware[] = $route;
     return $route;
 }
Example #3
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  * @param callable               $next
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
 {
     $request = $this->decorateRequest($request);
     $response = $this->decorateResponse($response);
     $relay = $this->relayBuilder->newInstance($this->middleware);
     $error = $this->errorHandler;
     $notFound = $this->notFoundHandler;
     try {
         $response = $relay($request, $response);
     } catch (\Exception $ex) {
         $response = $error($request, $response, $ex);
     }
     if (!$response instanceof ResponseInterface) {
         throw new Exception\InvalidResponse();
     }
     if (null === $this->routeMap->getRouteMatch()) {
         $response = $notFound($request, $response);
     }
     return $next ? $next($request, $response) : $response;
 }
Example #4
0
 /**
  * Generates a url for the route specified if it exists.
  *
  * @param string $name
  * @param array $params
  * @return string
  */
 public function __invoke($name, array $params = [])
 {
     return $this->routeMap->assemble($name, $params);
 }