vendor/liip/imagine-bundle/Service/FilterService.php line 101

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\Service;
  11. use Liip\ImagineBundle\Binary\BinaryInterface;
  12. use Liip\ImagineBundle\Exception\Imagine\Filter\NonExistingFilterException;
  13. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  14. use Liip\ImagineBundle\Imagine\Data\DataManager;
  15. use Liip\ImagineBundle\Imagine\Filter\FilterManager;
  16. use Psr\Log\LoggerInterface;
  17. use Psr\Log\NullLogger;
  18. class FilterService
  19. {
  20.     /**
  21.      * @var DataManager
  22.      */
  23.     private $dataManager;
  24.     /**
  25.      * @var FilterManager
  26.      */
  27.     private $filterManager;
  28.     /**
  29.      * @var CacheManager
  30.      */
  31.     private $cacheManager;
  32.     /**
  33.      * @var LoggerInterface
  34.      */
  35.     private $logger;
  36.     /**
  37.      * @var bool
  38.      */
  39.     private $webpGenerate;
  40.     /**
  41.      * @var array
  42.      */
  43.     private $webpOptions;
  44.     public function __construct(
  45.         DataManager $dataManager,
  46.         FilterManager $filterManager,
  47.         CacheManager $cacheManager,
  48.         bool $webpGenerate,
  49.         array $webpOptions,
  50.         ?LoggerInterface $logger null
  51.     ) {
  52.         $this->dataManager $dataManager;
  53.         $this->filterManager $filterManager;
  54.         $this->cacheManager $cacheManager;
  55.         $this->webpGenerate $webpGenerate;
  56.         $this->webpOptions $webpOptions;
  57.         $this->logger $logger ?: new NullLogger();
  58.     }
  59.     /**
  60.      * @param string $path
  61.      * @param string $filter
  62.      */
  63.     public function bustCache($path$filter)
  64.     {
  65.         $basePathContainer = new FilterPathContainer($path);
  66.         $filterPathContainers = [$basePathContainer];
  67.         if ($this->webpGenerate) {
  68.             $filterPathContainers[] = $basePathContainer->createWebp($this->webpOptions);
  69.         }
  70.         foreach ($filterPathContainers as $filterPathContainer) {
  71.             if ($this->cacheManager->isStored($filterPathContainer->getTarget(), $filter)) {
  72.                 $this->cacheManager->remove($filterPathContainer->getTarget(), $filter);
  73.             }
  74.         }
  75.     }
  76.     /**
  77.      * @param string      $path
  78.      * @param string      $filter
  79.      * @param string|null $resolver
  80.      *
  81.      * @return string
  82.      */
  83.     public function getUrlOfFilteredImage($path$filter$resolver nullbool $webpSupported false)
  84.     {
  85.         $basePathContainer = new FilterPathContainer($path);
  86.         return $this->getUrlOfFilteredImageByContainer($basePathContainer$filter$resolver$webpSupported);
  87.     }
  88.     /**
  89.      * @param string      $path
  90.      * @param string      $filter
  91.      * @param string|null $resolver
  92.      *
  93.      * @return string
  94.      */
  95.     public function getUrlOfFilteredImageWithRuntimeFilters(
  96.         $path,
  97.         $filter,
  98.         array $runtimeFilters = [],
  99.         $resolver null,
  100.         bool $webpSupported false
  101.     ) {
  102.         $runtimePath $this->cacheManager->getRuntimePath($path$runtimeFilters);
  103.         $basePathContainer = new FilterPathContainer($path$runtimePath, [
  104.             'filters' => $runtimeFilters,
  105.         ]);
  106.         return $this->getUrlOfFilteredImageByContainer($basePathContainer$filter$resolver$webpSupported);
  107.     }
  108.     private function getUrlOfFilteredImageByContainer(
  109.         FilterPathContainer $basePathContainer,
  110.         string $filter,
  111.         ?string $resolver null,
  112.         bool $webpSupported false
  113.     ): string {
  114.         $filterPathContainers = [$basePathContainer];
  115.         if ($this->webpGenerate) {
  116.             $webpPathContainer $basePathContainer->createWebp($this->webpOptions);
  117.             $filterPathContainers[] = $webpPathContainer;
  118.         }
  119.         foreach ($filterPathContainers as $filterPathContainer) {
  120.             if (!$this->cacheManager->isStored($filterPathContainer->getTarget(), $filter$resolver)) {
  121.                 $this->cacheManager->store(
  122.                     $this->createFilteredBinary($filterPathContainer$filter),
  123.                     $filterPathContainer->getTarget(),
  124.                     $filter,
  125.                     $resolver
  126.                 );
  127.             }
  128.         }
  129.         if ($webpSupported && isset($webpPathContainer)) {
  130.             return $this->cacheManager->resolve($webpPathContainer->getTarget(), $filter$resolver);
  131.         }
  132.         return $this->cacheManager->resolve($basePathContainer->getTarget(), $filter$resolver);
  133.     }
  134.     /**
  135.      * @throws NonExistingFilterException
  136.      */
  137.     private function createFilteredBinary(FilterPathContainer $filterPathContainerstring $filter): BinaryInterface
  138.     {
  139.         $binary $this->dataManager->find($filter$filterPathContainer->getSource());
  140.         try {
  141.             return $this->filterManager->applyFilter($binary$filter$filterPathContainer->getOptions());
  142.         } catch (NonExistingFilterException $e) {
  143.             $this->logger->debug(sprintf(
  144.                 'Could not locate filter "%s" for path "%s". Message was "%s"',
  145.                 $filter,
  146.                 $filterPathContainer->getSource(),
  147.                 $e->getMessage()
  148.             ));
  149.             throw $e;
  150.         }
  151.     }
  152. }