Example #1
0
 public function testApp()
 {
     $app = new App();
     $sRequest = new StratigilityRequest(new DiactorosRequest());
     $req = new Request($app, $sRequest);
     $this->assertSame($app, $req->app());
 }
Example #2
0
 /**
  * @param Request  $request
  * @param Response $response
  * @param callable $next
  * @return Response
  */
 public function __invoke(Request $request, Response $response, callable $next)
 {
     $dispatcher = new Dispatcher($this->collector->getData());
     $result = $dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
     $code = $result[0];
     if ($code != Dispatcher::FOUND) {
         return $next($request, $response);
     }
     /** @var Route $route */
     $route = $result[1];
     $params = $result[2];
     $request = $request->withAttribute('params', $params);
     foreach ($params as $key => $value) {
         $request[$key] = $value;
     }
     // Custom middleware handler.
     // We have to iterate through middleware as it was added so that they retain the proper ordering.
     // We have to check if the middleware is a route and if it was the route matched during dispatching.
     // If so, we can call the route handler, otherwise, we need to skip over it.
     $middleware = $this->middleware;
     $callable = function ($request, $response) use(&$callable, &$middleware, $route, $next) {
         /** @var callable $layer */
         $layer = array_shift($middleware);
         if ($layer instanceof Route) {
             if ($layer !== $route) {
                 return $callable($request, $response, $next);
             }
             $layer = $this->resolver->resolve($route->getHandler());
             return $layer($request, $response);
         }
         return $layer($request, $response, $callable);
     };
     return $callable($request, $response);
 }
Example #3
0
 /**
  * @param Request           $request
  * @param ResponseInterface $response
  * @param callable          $next
  * @return ResponseInterface
  */
 public function __invoke(Request $request, ResponseInterface $response, callable $next)
 {
     $container = $request->app()->getContainer();
     if (!$container instanceof Container) {
         throw new Exception\InvalidContainer('This package only works with the default Container');
     }
     $container->addServiceProvider(new DoctrineProvider($this->config));
     return $next($request, $response);
 }
Example #4
0
 /**
  * @param Request $request
  * @param Response $response
  * @param callable $next
  * @return Response
  */
 public function __invoke(Request $request, Response $response, callable $next = null)
 {
     $app = $request->app();
     $result = $this->configure($app);
     if ($result instanceof Router) {
         return $result($request, $response, $next);
     }
     return $next ? $next($request, $response) : $response;
 }
Example #5
0
 /**
  * {@inheritDoc}
  */
 public function getValue(Request $request, Response $response)
 {
     $query = $request->getQueryParams();
     $values = [];
     $params = $this->params();
     foreach ($params as $param) {
         $values[$param] = $query[$param];
     }
     return count($values) > 1 ? $values : current($values);
 }
Example #6
0
 /**
  * @internal
  * @param Request $request
  * @param Response $response
  * @param callable $next
  * @return Response
  */
 public function dispatchHandler(Request $request, Response $response, callable $next)
 {
     $dispatcher = new Dispatcher($this->collector->getData());
     $result = $dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
     $code = $result[0];
     $handler = isset($result[1]) ? $result[1] : null;
     $params = isset($result[2]) ? $result[2] : [];
     if ($code != Dispatcher::FOUND) {
         return $next ? $next($request, $response) : $response;
     }
     foreach ($params as $key => $value) {
         $request[$key] = $value;
     }
     return $this->paramHandler($request, $response, $next, $handler, $params);
 }