/** * Return admin assets * @param Response $response * @param $asset * @return Response|static * @throws FileNotFoundException */ public function assets(Response $response, $asset) { $filesystem = new Filesystem(new Adapter(FS2ADMIN)); $expires = 8640000; try { // generate cache data $timestamp_string = gmdate('D, d M Y H:i:s ', $filesystem->getTimestamp($asset)) . 'GMT'; $etag = md5($filesystem->read($asset)); $mime_type = $filesystem->getMimetype($asset); if (0 !== strpos($mime_type, 'image')) { $mime_type = 'text/css'; } $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false; $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] : false; if (($if_none_match && $if_none_match == "\"{$etag}\"" || !$if_none_match) && ($if_modified_since && $if_modified_since == $timestamp_string)) { return $response->withStatus('304'); } else { $response = $response->withHeader('Last-Modified', $timestamp_string)->withHeader('ETag', "\"{$etag}\""); } // send out content type, expire time and the file $response->getBody()->write($filesystem->read($asset)); return $response->withHeader('Expires', gmdate('D, d M Y H:i:s ', time() + $expires) . 'GMT')->withHeader('Content-Type', $mime_type)->withHeader('Pragma', 'cache')->withHeader('Cache-Control', 'cache'); } catch (FileNotFoundException $e) { throw $e; } }
/** * Executes a http request. * * @param ServerRequestInterface $request * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, Response $response) { $runner = new Runner(array_merge($this->queue, $this->sources, [function (ServerRequestInterface $request, Response $response, callable $next) { return $response->withStatus(404); }])); return $runner($request, $response); }
/** * Wrap the remaining middleware with error handling. * * @param Request $request The request. * @param Response $response The response. * @param callable $next Callback to invoke the next middleware. * @return Response A response */ public function __invoke(Request $request, Response $response, callable $next) { $uri = $this->getBaseUri($request); $route = $this->router->dispatch($request->getMethod(), $uri); if ($route[0] === Dispatcher::NOT_FOUND) { $stream = new Stream('php://temp', 'wb+'); $stream->write('Not found'); return $response->withStatus(404)->withBody($stream); } if ($route[0] === Dispatcher::METHOD_NOT_ALLOWED) { $stream = new Stream('php://temp', 'wb+'); $stream->write('Not allowed'); return $response->withStatus(405)->withBody($stream); } $request = $request->withAttribute('vars', $route[2]); $response = $this->executeCallable($route[1], $request, $response, $next); return $next($request, $response); }
/** * */ public function run() { $route = $this->getRoute(); if ($route !== null) { $queue = array_merge($this->_middleware->getArrayCopy(), $route->getMiddleware()); $queue[] = $route->getRequestHandler(); $relayBuilder = new RelayBuilder(); $relay = $relayBuilder->newInstance($queue); (new SapiEmitter())->emit($relay($this->_request, $this->_response)); return true; } (new SapiEmitter())->emit($this->_response->withStatus(404)); }
public function failed(Request $request, Response $response) { return $response->withStatus(502); }