/** * Invoke the application. * * - Convert the PSR request/response into CakePHP equivalents. * - Create the controller that will handle this request. * - Invoke the controller. * * @param \Psr\Http\Message\ServerRequestInterface $request The request * @param \Psr\Http\Message\ResponseInterface $response The response * @param callable $next The next middleware * @return \Psr\Http\Message\ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next) { // Convert the request/response to CakePHP equivalents. $cakeRequest = RequestTransformer::toCake($request); $cakeResponse = ResponseTransformer::toCake($response); // Dispatch the request/response to CakePHP $cakeResponse = $this->getDispatcher()->dispatch($cakeRequest, $cakeResponse); // Convert the response back into a PSR7 object. return ResponseTransformer::toPsr($cakeResponse); }
/** * {@inheritDoc} * * Will render the view and use the content as the body of the response. * It will also set the specified HTTP code and optional additional headers. */ public function process(ServerRequestInterface $request, ResponseInterface $response) { $className = $this->_config['view']['className']; if (empty($className)) { $className = 'App\\View\\AppView'; } $viewConfig = $this->_config['view'] ?: []; $view = new $className(RequestTransformer::toCake($request), ResponseTransformer::toCake($response), null, $viewConfig); $stream = new Stream(fopen('php://memory', 'r+')); $stream->write($view->render()); $response = $response->withBody($stream); $response = $response->withStatus($this->_config['code']); $response = $this->addHeaders($response); return $response; }
/** * Run a request and get the response. * * @param \Cake\Network\Request $request The request to execute. * @return \Cake\Network\Response The generated response. */ public function execute($request) { try { $reflect = new ReflectionClass($this->_class); $app = $reflect->newInstanceArgs($this->_constructorArgs); } catch (ReflectionException $e) { throw new LogicException(sprintf('Cannot load "%s" for use in integration testing.', $this->_class)); } // Spy on the controller using the initialize hook instead // of the dispatcher hooks as those will be going away one day. EventManager::instance()->on('Controller.initialize', [$this->_test, 'controllerSpy']); $server = new Server($app); $psrRequest = $this->_createRequest($request); $response = $server->run($psrRequest); return ResponseTransformer::toCake($response); }
/** * Handle an exception and generate an error response * * @param \Exception $exception The exception to handle. * @param \Psr\Http\Message\ServerRequestInterface $request The request. * @param \Psr\Http\Message\ResponseInterface $response The response. * @return \Psr\Http\Message\ResponseInterface A response */ public function handleException($exception, $request, $response) { $renderer = $this->getRenderer($exception); try { $res = $renderer->render(); $this->logException($request, $exception); return ResponseTransformer::toPsr($res); } catch (\Exception $e) { $this->logException($request, $e); $body = $response->getBody(); $body->write('An Internal Server Error Occurred'); $response = $response->withStatus(500)->withBody($body); } return $response; }