/** * @param Closure $callback * @param InterventionRequest $interventionRequest * @return Response */ public function getResponse(Closure $callback, InterventionRequest $interventionRequest) { try { $this->cacheFile = new File($this->cacheFilePath); $response = new Response(file_get_contents($this->cacheFile->getPathname()), Response::HTTP_OK, ['Content-Type' => $this->cacheFile->getMimeType(), 'Content-Disposition' => 'filename="' . $this->realImage->getFilename() . '"', 'X-Generator-Cached' => true]); $response->setLastModified(new \DateTime(date("Y-m-d H:i:s", $this->cacheFile->getMTime()))); } catch (FileNotFoundException $e) { if (is_callable($callback)) { $image = $callback($interventionRequest); if ($image instanceof Image) { $this->saveImage($image); $this->cacheFile = new File($this->cacheFilePath); if (null !== $this->dispatcher) { // create the ImageSavedEvent and dispatch it $event = new ImageSavedEvent($image, $this->cacheFile); $this->dispatcher->dispatch(ImageSavedEvent::NAME, $event); } // send HTTP header and output image data $response = new Response(file_get_contents($this->cacheFile->getPathname()), Response::HTTP_OK, ['Content-Type' => $image->mime(), 'Content-Disposition' => 'filename="' . $this->realImage->getFilename() . '"', 'X-Generator-First-Render' => true]); $response->setLastModified(new \DateTime('now')); } else { throw new \RuntimeException("Image is not a valid InterventionImage instance.", 1); } } else { throw new \RuntimeException("No image handle closure defined", 1); } } $this->initializeGarbageCollection(); return $response; }
/** * Automatically sets the Last-Modified header according the file modification date. */ public function setAutoLastModified() { $this->setLastModified(\DateTime::createFromFormat('U', $this->file->getMTime())); return $this; }
/** * Creates the response object. * * @return Response */ public function getResponse() { $requestPath = parse_url($this->request->server->get('REQUEST_URI'), PHP_URL_PATH); foreach ($this->from as $from) { $filePath = $from . substr($requestPath, strlen($this->to)); if (!$this->isAllowedExtension(pathinfo($filePath, PATHINFO_EXTENSION))) { continue; } if (is_file($filePath)) { $file = new File($filePath); $response = Response::create()->setExpires(new DateTime('+1 week'))->setLastModified(DateTime::createFromFormat('U', $file->getMTime())); if ($response->isNotModified($this->request)) { return $response; } $this->setContentType($file, $response); return $response->setContent(file_get_contents($file->getPathname())); } } return $this->getNotFoundResponse(); }
} $css = file_get_contents(APP_FRONT_RESOURCE_PATH . $request->getPathInfo(), false, null, -1); $response = new Response($css); $response->headers->set('Content-Type', 'text/css'); //200 OK(BFCache) $response->setExpires(new \DateTime('2019-01-01')); return $response; }), array('_method' => 'GET', '_format' => 'css|min.css', 'path' => 'dist/css|assets/css|app/css'))); #.js config $routes->add('front_js', new Route('/{path}/{file}.{_format}', array('file' => null, '_controller' => function (Request $request) { $js = new File(APP_FRONT_RESOURCE_PATH . $request->getPathInfo(), true); # check file exists $jsContent = file_get_contents(APP_FRONT_RESOURCE_PATH . $request->getPathInfo(), false, null, -1); $response = new Response($jsContent); $response->headers->set('Content-Type', 'text/javascript'); $jsLastModified = $js->getMTime(); $date = new \DateTime('now'); //UTC $date->setTimestamp($jsLastModified); $response->setLastModified($date); $response->isNotModified($request); return $response; }), array('_method' => 'GET', '_format' => 'js|min.js|11.3.min.js', 'path' => 'dist/js|assets/js|assets/js/vendor|app/js'))); #.jpeg config $routes->add('img', new Route('/img/{params}', array('params' => null, '_controller' => function (Request $request) { if (!file_exists(APP_FRONT_RESOURCE_PATH . $request->getPathInfo())) { throw new ResourceNotFoundException(APP_FRONT_RESOURCE_PATH . $request->getPathInfo()); } $img = file_get_contents(APP_FRONT_RESOURCE_PATH . $request->getPathInfo(), false, null, -1); $response = new Response($img); $response->headers->set('Content-Type', 'image/jpeg');