vendor/liip/imagine-bundle/Controller/ImagineController.php line 138

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the `liip/LiipImagineBundle` project.
  4.  *
  5.  * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE.md
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Liip\ImagineBundle\Controller;
  11. use Imagine\Exception\RuntimeException;
  12. use Liip\ImagineBundle\Config\Controller\ControllerConfig;
  13. use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
  14. use Liip\ImagineBundle\Exception\Imagine\Filter\NonExistingFilterException;
  15. use Liip\ImagineBundle\Imagine\Cache\Helper\PathHelper;
  16. use Liip\ImagineBundle\Imagine\Cache\SignerInterface;
  17. use Liip\ImagineBundle\Imagine\Data\DataManager;
  18. use Liip\ImagineBundle\Service\FilterService;
  19. use Symfony\Component\HttpFoundation\RedirectResponse;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  22. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  23. class ImagineController
  24. {
  25.     /**
  26.      * @var FilterService
  27.      */
  28.     private $filterService;
  29.     /**
  30.      * @var DataManager
  31.      */
  32.     private $dataManager;
  33.     /**
  34.      * @var SignerInterface
  35.      */
  36.     private $signer;
  37.     /**
  38.      * @var ControllerConfig
  39.      */
  40.     private $controllerConfig;
  41.     /**
  42.      * @param FilterService         $filterService
  43.      * @param DataManager           $dataManager
  44.      * @param SignerInterface       $signer
  45.      * @param ControllerConfig|null $controllerConfig
  46.      */
  47.     public function __construct(FilterService $filterServiceDataManager $dataManagerSignerInterface $signer, ?ControllerConfig $controllerConfig null)
  48.     {
  49.         $this->filterService $filterService;
  50.         $this->dataManager $dataManager;
  51.         $this->signer $signer;
  52.         if (null === $controllerConfig) {
  53.             @trigger_error(sprintf(
  54.                 'Instantiating "%s" without a forth argument of type "%s" is deprecated since 2.2.0 and will be required in 3.0.'self::class, ControllerConfig::class
  55.             ), E_USER_DEPRECATED);
  56.         }
  57.         $this->controllerConfig $controllerConfig ?? new ControllerConfig(301);
  58.     }
  59.     /**
  60.      * This action applies a given filter to a given image, saves the image and redirects the browser to the stored
  61.      * image.
  62.      *
  63.      * The resulting image is cached so subsequent requests will redirect to the cached image instead applying the
  64.      * filter and storing the image again.
  65.      *
  66.      * @param Request $request
  67.      * @param string  $path
  68.      * @param string  $filter
  69.      *
  70.      * @throws RuntimeException
  71.      * @throws NotFoundHttpException
  72.      *
  73.      * @return RedirectResponse
  74.      */
  75.     public function filterAction(Request $request$path$filter)
  76.     {
  77.         $path PathHelper::urlPathToFilePath($path);
  78.         $resolver $request->get('resolver');
  79.         return $this->createRedirectResponse(function () use ($path$filter$resolver) {
  80.             return $this->filterService->getUrlOfFilteredImage($path$filter$resolver);
  81.         }, $path$filter);
  82.     }
  83.     /**
  84.      * This action applies a given filter -merged with additional runtime filters- to a given image, saves the image and
  85.      * redirects the browser to the stored image.
  86.      *
  87.      * The resulting image is cached so subsequent requests will redirect to the cached image instead applying the
  88.      * filter and storing the image again.
  89.      *
  90.      * @param Request $request
  91.      * @param string  $hash
  92.      * @param string  $path
  93.      * @param string  $filter
  94.      *
  95.      * @throws RuntimeException
  96.      * @throws BadRequestHttpException
  97.      * @throws NotFoundHttpException
  98.      *
  99.      * @return RedirectResponse
  100.      */
  101.     public function filterRuntimeAction(Request $request$hash$path$filter)
  102.     {
  103.         $resolver $request->get('resolver');
  104.         $path PathHelper::urlPathToFilePath($path);
  105.         $runtimeConfig $request->query->get('filters', []);
  106.         if (!\is_array($runtimeConfig)) {
  107.             throw new NotFoundHttpException(sprintf('Filters must be an array. Value was "%s"'$runtimeConfig));
  108.         }
  109.         if (true !== $this->signer->check($hash$path$runtimeConfig)) {
  110.             throw new BadRequestHttpException(sprintf(
  111.                 'Signed url does not pass the sign check for path "%s" and filter "%s" and runtime config %s',
  112.                 $path,
  113.                 $filter,
  114.                 json_encode($runtimeConfig)
  115.             ));
  116.         }
  117.         return $this->createRedirectResponse(function () use ($path$filter$runtimeConfig$resolver) {
  118.             return $this->filterService->getUrlOfFilteredImageWithRuntimeFilters($path$filter$runtimeConfig$resolver);
  119.         }, $path$filter$hash);
  120.     }
  121.     private function createRedirectResponse(\Closure $urlstring $pathstring $filter, ?string $hash null): RedirectResponse
  122.     {
  123.         try {
  124.             return new RedirectResponse($url(), $this->controllerConfig->getRedirectResponseCode());
  125.         } catch (NotLoadableException $exception) {
  126.             if (null !== $this->dataManager->getDefaultImageUrl($filter)) {
  127.                 return new RedirectResponse($this->dataManager->getDefaultImageUrl($filter));
  128.             }
  129.             throw new NotFoundHttpException(sprintf('Source image for path "%s" could not be found'$path));
  130.         } catch (NonExistingFilterException $exception) {
  131.             throw new NotFoundHttpException(sprintf('Requested non-existing filter "%s"'$filter));
  132.         } catch (RuntimeException $exception) {
  133.             throw new \RuntimeException(vsprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"', [
  134.                 $hash sprintf('%s/%s'$hash$path) : $path,
  135.                 $filter,
  136.                 $exception->getMessage(),
  137.             ]), 0$exception);
  138.         }
  139.     }
  140. }