Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function __invoke(Request $request, Response $response, callable $next)
 {
     $this->initSession($request);
     $scope = $this->container->replace(SessionInterface::class, $this->session);
     try {
         $response = $next($request->withAttribute('session', $this->session), $response);
     } finally {
         $this->container->restore($scope);
     }
     return $this->commitSession($request, $response);
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function __invoke(Request $request, Response $response, callable $next)
 {
     $context = $this->createContext($request);
     $scope = $this->container->replace(ContextInterface::class, $context);
     try {
         $response = $next($request->withAttribute('auth', $context), $response);
     } finally {
         $this->container->restore($scope);
     }
     if ($context->hasToken()) {
         $response = $this->updateToken($request, $response, $context);
     } elseif ($context->hasUser()) {
         $response = $this->createToken($request, $response, $context);
     }
     return $response;
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     $scope = $this->container->replace(RouterInterface::class, $this);
     try {
         $route = $this->findRoute($request);
         if (empty($route)) {
             //Unable to locate route
             throw new ClientException(ClientException::NOT_FOUND);
         }
         //IoC container context
         $response = $route->withContainer($this->container)->perform($request->withAttribute('route', $route), $response);
     } finally {
         $this->container->restore($scope);
     }
     return $response;
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function __invoke(Request $request, Response $response, callable $next = null)
 {
     $queue = $this->container->make(CookieQueue::class, ['request' => $request, 'httpConfig' => $this->httpConfig]);
     //Opening cookie scope
     $scope = $this->container->replace(CookieQueue::class, $queue);
     try {
         /**
          * Debug: middleware creates scope for [CookieQueue].
          */
         $response = $next($this->unpackCookies($request)->withAttribute('cookieQueue', $queue), $response);
         //New cookies
         return $this->packCookies($response, $queue);
     } finally {
         $this->container->restore($scope);
     }
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, \Closure $next = null)
 {
     //Opening cookie scope
     $outerCookies = $this->container->replace(self::class, $this);
     $this->request = $this->decodeCookies($request);
     $response = $next($this->request->withAttribute('cookieDomain', $this->cookieDomain()));
     //New cookies
     $response = $this->mountCookies($response);
     //Restoring scope
     $this->container->restore($outerCookies);
     return $response;
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     //Open router scope
     $outerRouter = $this->container->replace(self::class, $this);
     if (empty($this->activeRoute = $this->findRoute($request, $this->basePath))) {
         throw new ClientException(ClientException::NOT_FOUND);
     }
     //Default routes will understand about keepOutput
     $response = $this->activeRoute->perform($request->withAttribute('route', $this->activeRoute), $response, $this->container);
     //Close router scope
     $this->container->restore($outerRouter);
     return $response;
 }
Esempio n. 7
0
 /**
  * Execute console command using it's name.
  *
  * @param string               $command
  * @param array|InputInterface $parameters
  * @param OutputInterface      $output
  * @return CommandOutput
  * @throws ConsoleException
  */
 public function command($command, $parameters = [], OutputInterface $output = null)
 {
     $input = is_object($parameters) ? $parameters : new ArrayInput(compact('command') + $parameters);
     $output = !empty($output) ? $output : new BufferedOutput();
     $outerOutput = $this->container->replace(OutputInterface::class, $output);
     $outerInput = $this->container->replace(InputInterface::class, $input);
     //Go
     $code = self::CODE_UNDEFINED;
     try {
         $code = $this->application()->find($command)->run($input, $output);
     } catch (\Exception $exception) {
         $this->application->renderException($exception, $output);
     } finally {
         $this->container->restore($outerInput);
         $this->container->restore($outerOutput);
     }
     return new CommandOutput($code, $output);
 }
Esempio n. 8
0
 /**
  * @param string $controller
  * @param string $action
  * @param array  $parameters
  * @return mixed
  * @throws ControllerException
  */
 protected function execute($controller, $action, array $parameters)
 {
     $benchmark = $this->benchmark('callAction', $controller . '::' . ($action ?: '~default~'));
     $scope = $this->container->replace(Vault::class, $this);
     $this->controller = $controller;
     try {
         //Initiating controller with all required dependencies
         $object = $this->container->make($this->config->controllers()[$controller]);
         if (!$object instanceof ControllerInterface) {
             throw new ControllerException("Invalid '{$controller}', ControllerInterface not implemented.", ControllerException::NOT_FOUND);
         }
         return $object->callAction($action, $parameters);
     } finally {
         $this->benchmark($benchmark);
         $this->container->restore($scope);
         $this->controller = '';
     }
 }
Esempio n. 9
0
 /**
  * Run pipeline target and return generated response.
  *
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  * @return ResponseInterface
  */
 protected function createResponse(ServerRequestInterface $request, ResponseInterface $response)
 {
     //Request scope
     $outerRequest = $this->container->replace(ServerRequestInterface::class, $request);
     $outerResponse = $this->container->replace(ResponseInterface::class, $response);
     $outputLevel = ob_get_level();
     $output = '';
     $result = null;
     try {
         ob_start();
         $result = $this->execute($request, $response);
     } finally {
         while (ob_get_level() > $outputLevel + 1) {
             $output = ob_get_clean() . $output;
         }
         //Closing request/response scope
         $this->container->restore($outerRequest);
         $this->container->restore($outerResponse);
     }
     return $this->wrapResponse($response, $result, ob_get_clean() . $output);
 }
Esempio n. 10
0
 /**
  * Restore initial (pre pipeline) request and response.
  */
 private function restoreScope()
 {
     $this->container->restore($this->requestScope);
     $this->container->restore($this->responseScope);
 }
Esempio n. 11
0
 /**
  * Restoring input and output scopes.
  */
 private function restoreScope()
 {
     $this->container->restore($this->inputScope);
     $this->container->restore($this->outputScope);
 }