Ejemplo n.º 1
0
 public function testResponse()
 {
     $homePage = new HomePageAction($this->router->reveal(), null);
     $response = $homePage(new ServerRequest(['/']), new Response(), function () {
     });
     $this->assertTrue($response instanceof Response);
 }
Ejemplo n.º 2
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  *
  * @return HtmlResponse|RedirectResponse
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     if ($request->getMethod() === 'POST') {
         $this->albumService->addAlbum($request->getParsedBody());
         return new RedirectResponse($this->router->generateUri('album.index'));
     }
     return new HtmlResponse($this->template->render('album::add', ['form' => $this->form]));
 }
Ejemplo n.º 3
0
 private function seedRoutes(RouterInterface $router) : RouterInterface
 {
     $middleware = function () {
     };
     foreach ($this->routes as $name => $path) {
         $router->addRoute(new Route($path, $middleware, ['GET'], $name));
     }
     return $router;
 }
Ejemplo n.º 4
0
 /**
  * @param array $params
  * @return string
  * @throws Exception\RenderingException if current result is a routing
  *     failure.
  */
 private function generateUriFromResult(array $params)
 {
     if ($this->result->isFailure()) {
         throw new Exception\RenderingException('Attempting to use matched result when routing failed; aborting');
     }
     $name = $this->result->getMatchedRouteName();
     $params = array_merge($this->result->getMatchedParams(), $params);
     return $this->router->generateUri($name, $params);
 }
 /**
  * Process an incoming request and/or response.
  *
  * Accepts a server-side request and a response instance, and does
  * something with them.
  *
  * If the response is not complete and/or further processing would not
  * interfere with the work done in the middleware, or if the middleware
  * wants to delegate to another process, it can use the `$out` callable
  * if present.
  *
  * If the middleware does not return a value, execution of the current
  * request is considered complete, and the response instance provided will
  * be considered the response to return.
  *
  * Alternately, the middleware may return a response instance.
  *
  * Often, middleware will `return $out();`, with the assumption that a
  * later middleware will return a response.
  *
  * @param Request $request
  * @param Response $response
  * @param null|callable $out
  * @return null|Response
  */
 public function __invoke(Request $request, Response $response, callable $out = null)
 {
     $matchedRoute = $this->router->match($request);
     $params = $matchedRoute->getMatchedParams();
     // Determine the language to use based on the lang parameter
     $lang = isset($params['lang']) ? $params['lang'] : 'en';
     $this->translator->setLocale($lang);
     return $out($request, $response);
 }
 /**
  * @param Request $request
  * @return array []
  */
 public function getOptionsForRequest(ServerRequestInterface $request)
 {
     /**
      * @var RouteResult $routeMatch
      */
     $routePath = $this->router->match($request)->getMatchedRouteName();
     foreach ($this->config as $route) {
         if ($route['path'] === $routePath) {
             return isset($route['options']) ? $route['options'] : [];
         }
     }
     return [];
 }
 /**
  * Process an incoming request and/or response.
  *
  * Accepts a server-side request and a response instance, and does
  * something with them.
  *
  * If the response is not complete and/or further processing would not
  * interfere with the work done in the middleware, or if the middleware
  * wants to delegate to another process, it can use the `$out` callable
  * if present.
  *
  * If the middleware does not return a value, execution of the current
  * request is considered complete, and the response instance provided will
  * be considered the response to return.
  *
  * Alternately, the middleware may return a response instance.
  *
  * Often, middleware will `return $out();`, with the assumption that a
  * later middleware will return a response.
  *
  * @param Request $request
  * @param Response $response
  * @param null|callable $out
  * @return null|Response
  */
 public function __invoke(Request $request, Response $response, callable $out = null) : Response
 {
     $currentRoute = $this->router->match($request);
     $currentRoutePath = $request->getUri()->getPath();
     // If current route is a success route and it has been previously cached, write cached content and retur
     if ($currentRoute->isSuccess() && $this->cache->contains($currentRoutePath)) {
         $response->getBody()->write($this->cache->fetch($currentRoutePath));
         return $response;
     }
     // If the response is not cached, process the next middleware and get its response, then cache it
     $resp = $out($request, $response);
     if ($resp instanceof Response && $this->isResponseCacheable($resp, $currentRoute->getMatchedParams())) {
         $this->cache->save($currentRoutePath, $resp->getBody()->__toString());
     }
     return $resp;
 }
 public function testInvalidIdRequest()
 {
     $request = $this->request->withUri(new Uri('http://localhost/api/ping/one'))->withMethod('GET');
     /** @var \Zend\Expressive\Router\RouteResult $result */
     $result = $this->router->match($request);
     $this->assertTrue($result->isFailure());
 }
Ejemplo n.º 9
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  *
  * @return HtmlResponse|RedirectResponse
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     try {
         $album = (array) $this->albumService->getAlbum($request->getAttribute('id'));
     } catch (\Exception $e) {
         return new HtmlResponse($this->template->render('error::404'), 404);
     }
     if ($request->getMethod() === 'POST') {
         $body = new Parameters($request->getParsedBody());
         $del = $body->get('del', 'No');
         if (strtolower($del) === 'yes') {
             $this->albumService->deleteAlbum($album);
         }
         return new RedirectResponse($this->router->generateUri('album.index'));
     }
     return new HtmlResponse($this->template->render('album::delete', ['album' => $album]));
 }
Ejemplo n.º 10
0
 /**
  * Process an incoming request and/or response.
  *
  * Accepts a server-side request and a response instance, and does
  * something with them.
  *
  * If the response is not complete and/or further processing would not
  * interfere with the work done in the middleware, or if the middleware
  * wants to delegate to another process, it can use the `$out` callable
  * if present.
  *
  * If the middleware does not return a value, execution of the current
  * request is considered complete, and the response instance provided will
  * be considered the response to return.
  *
  * Alternately, the middleware may return a response instance.
  *
  * Often, middleware will `return $out();`, with the assumption that a
  * later middleware will return a response.
  *
  * @param Request $request
  * @param Response $response
  * @param null|callable $out
  * @return null|Response
  */
 public function __invoke(Request $request, Response $response, callable $out = null)
 {
     // Make sure the short URL exists for this short code
     $shortCode = $request->getAttribute('shortCode');
     try {
         $shortUrl = $this->urlShortener->shortCodeToUrl($shortCode);
         if (!isset($shortUrl)) {
             return $out($request, $response->withStatus(404), 'Not Found');
         }
     } catch (InvalidShortCodeException $e) {
         $this->logger->warning('Tried to create a QR code with an invalid short code' . PHP_EOL . $e);
         return $out($request, $response->withStatus(404), 'Not Found');
     }
     $path = $this->router->generateUri('long-url-redirect', ['shortCode' => $shortCode]);
     $size = $this->getSizeParam($request);
     $qrCode = new QrCode($request->getUri()->withPath($path)->withQuery(''));
     $qrCode->setSize($size)->setPadding(0);
     return new QrCodeResponse($qrCode);
 }
Ejemplo n.º 11
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     try {
         /**
          * @var Session $session
          */
         $session = $request->getAttribute('session');
         $album = new Album();
         $this->form->bind($album);
         $this->form->get('submit')->setValue('Add');
         if ($request->getMethod() === 'POST') {
             $this->albumService->addAlbum($request->getParsedBody());
             $session->getSegment('App\\Album')->setFlash('flash', ['type' => 'success', 'message' => sprintf('Successfully added album %s (%s)', $album->getTitle(), $album->getArtist())]);
             return new RedirectResponse($this->router->generateUri('album.index'));
         }
     } catch (\Exception $e) {
         // perhaps log an error and display a message to the user
     }
     return new HtmlResponse($this->template->render('album::add', ['form' => $this->form]));
 }
Ejemplo n.º 12
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     try {
         $album = $this->albumService->getAlbum($request->getAttribute('id'));
         if ($request->getMethod() === 'POST') {
             $body = new Parameters($request->getParsedBody());
             $del = $body->get('del', 'No');
             if (strtolower($del) === 'yes') {
                 $this->albumService->deleteAlbum($album);
                 /**
                  * @var Session $session
                  */
                 $session = $request->getAttribute('session');
                 $session->getSegment('App\\Album')->setFlash('flash', ['type' => 'success', 'message' => sprintf('Successfully deleted album %s (%s)', $album->getTitle(), $album->getArtist())]);
             }
             // Redirect to list of albums
             return new RedirectResponse($this->router->generateUri('album.index'));
         }
     } catch (\Exception $e) {
         // do something useful
     }
     return new HtmlResponse($this->template->render('album::delete', compact('album')));
 }
Ejemplo n.º 13
0
 /**
  * Add a route for the route middleware to match.
  *
  * Accepts either a Router\Route instance, or a combination of a path and
  * middleware, and optionally the HTTP methods allowed.
  *
  * On first invocation, pipes the route middleware to the middleware
  * pipeline.
  *
  * @param string|Router\Route $path
  * @param callable|string|array $middleware Middleware (or middleware service name) to associate with route.
  * @param null|array $methods HTTP method to accept; null indicates any.
  * @param null|string $name the name of the route
  * @return Router\Route
  * @throws Exception\InvalidArgumentException if $path is not a Router\Route AND middleware is null.
  */
 public function route($path, $middleware = null, array $methods = null, $name = null)
 {
     if (!$path instanceof Router\Route && null === $middleware) {
         throw new Exception\InvalidArgumentException(sprintf('%s expects either a route argument, or a combination of a path and middleware arguments', __METHOD__));
     }
     if ($path instanceof Router\Route) {
         $route = $path;
         $path = $route->getPath();
         $methods = $route->getAllowedMethods();
         $name = $route->getName();
     }
     $this->checkForDuplicateRoute($path, $methods);
     if (!isset($route)) {
         $methods = null === $methods ? Router\Route::HTTP_METHOD_ANY : $methods;
         $route = new Router\Route($path, $middleware, $methods, $name);
     }
     $this->routes[] = $route;
     $this->router->addRoute($route);
     return $route;
 }
 /**
  * @param ServerRequestInterface $request
  * @param RouterInterface $router
  * @return array
  */
 private function getParsedAttributes(ServerRequestInterface $request, RouterInterface $router)
 {
     $routeResult = $router->match($request);
     return $routeResult->getMatchedParams();
 }
Ejemplo n.º 15
0
 /**
  * {@inheritDoc}
  */
 public function collectParams(ServerRequestInterface $request)
 {
     return $this->router->match($request)->getMatchedParams();
 }
 /**
  * Usage: {{ path('name', parameters) }}
  *
  * @param $name
  * @param array $parameters
  * @param bool $relative
  * @return string
  */
 public function renderUri($name, $parameters = [], $relative = false)
 {
     return $this->router->generateUri($name, $parameters);
 }
Ejemplo n.º 17
0
 /**
  * @return RouteResult
  */
 public function getCurrentRouteResult()
 {
     return $this->router->match($this->request);
 }
Ejemplo n.º 18
0
 /**
  * @param string $routeName
  * @param array $options
  * @return string
  */
 public function __invoke($routeName, $options = [])
 {
     return $this->router->generateUri($routeName, $options);
 }