vendor/liip/imagine-bundle/Imagine/Data/DataManager.php line 135

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\Imagine\Data;
  11. use Liip\ImagineBundle\Binary\BinaryInterface;
  12. use Liip\ImagineBundle\Binary\Loader\LoaderInterface;
  13. use Liip\ImagineBundle\Binary\MimeTypeGuesserInterface;
  14. use Liip\ImagineBundle\Exception\InvalidArgumentException;
  15. use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
  16. use Liip\ImagineBundle\Model\Binary;
  17. use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface as DeprecatedExtensionGuesserInterface;
  18. use Symfony\Component\Mime\MimeTypesInterface;
  19. class DataManager
  20. {
  21.     /**
  22.      * @var MimeTypeGuesserInterface
  23.      */
  24.     protected $mimeTypeGuesser;
  25.     /**
  26.      * @var DeprecatedExtensionGuesserInterface|MimeTypesInterface
  27.      */
  28.     protected $extensionGuesser;
  29.     /**
  30.      * @var FilterConfiguration
  31.      */
  32.     protected $filterConfig;
  33.     /**
  34.      * @var string|null
  35.      */
  36.     protected $defaultLoader;
  37.     /**
  38.      * @var string|null
  39.      */
  40.     protected $globalDefaultImage;
  41.     /**
  42.      * @var LoaderInterface[]
  43.      */
  44.     protected $loaders = [];
  45.     /**
  46.      * @param MimeTypeGuesserInterface                               $mimeTypeGuesser
  47.      * @param DeprecatedExtensionGuesserInterface|MimeTypesInterface $extensionGuesser
  48.      * @param FilterConfiguration                                    $filterConfig
  49.      * @param string                                                 $defaultLoader
  50.      * @param string                                                 $globalDefaultImage
  51.      */
  52.     public function __construct(
  53.         MimeTypeGuesserInterface $mimeTypeGuesser,
  54.         $extensionGuesser,
  55.         FilterConfiguration $filterConfig,
  56.         $defaultLoader null,
  57.         $globalDefaultImage null
  58.     ) {
  59.         if (!$extensionGuesser instanceof MimeTypesInterface && !$extensionGuesser instanceof DeprecatedExtensionGuesserInterface) {
  60.             throw new InvalidArgumentException('$extensionGuesser must be an instance of Symfony\Component\Mime\MimeTypesInterface or Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface');
  61.         }
  62.         if (interface_exists(MimeTypesInterface::class) && $extensionGuesser instanceof DeprecatedExtensionGuesserInterface) {
  63.             @trigger_error(sprintf('Passing a %s to "%s()" is deprecated since Symfony 4.3, pass a "%s" instead.'DeprecatedExtensionGuesserInterface::class, __METHOD__MimeTypesInterface::class), E_USER_DEPRECATED);
  64.         }
  65.         $this->mimeTypeGuesser $mimeTypeGuesser;
  66.         $this->filterConfig $filterConfig;
  67.         $this->defaultLoader $defaultLoader;
  68.         $this->extensionGuesser $extensionGuesser;
  69.         $this->globalDefaultImage $globalDefaultImage;
  70.     }
  71.     /**
  72.      * Adds a loader to retrieve images for the given filter.
  73.      *
  74.      * @param string          $filter
  75.      * @param LoaderInterface $loader
  76.      */
  77.     public function addLoader($filterLoaderInterface $loader)
  78.     {
  79.         $this->loaders[$filter] = $loader;
  80.     }
  81.     /**
  82.      * Returns a loader previously attached to the given filter.
  83.      *
  84.      * @param string $filter
  85.      *
  86.      * @throws \InvalidArgumentException
  87.      *
  88.      * @return LoaderInterface
  89.      */
  90.     public function getLoader($filter)
  91.     {
  92.         $config $this->filterConfig->get($filter);
  93.         $loaderName = empty($config['data_loader']) ? $this->defaultLoader $config['data_loader'];
  94.         if (!isset($this->loaders[$loaderName])) {
  95.             throw new \InvalidArgumentException(sprintf(
  96.                 'Could not find data loader "%s" for "%s" filter type',
  97.                 $loaderName,
  98.                 $filter
  99.             ));
  100.         }
  101.         return $this->loaders[$loaderName];
  102.     }
  103.     /**
  104.      * Retrieves an image with the given filter applied.
  105.      *
  106.      * @param string $filter
  107.      * @param string $path
  108.      *
  109.      * @throws \LogicException
  110.      *
  111.      * @return BinaryInterface
  112.      */
  113.     public function find($filter$path)
  114.     {
  115.         $loader $this->getLoader($filter);
  116.         $binary $loader->find($path);
  117.         if (!$binary instanceof BinaryInterface) {
  118.             $mimeType $this->mimeTypeGuesser->guess($binary);
  119.             $extension $this->getExtension($mimeType);
  120.             $binary = new Binary(
  121.                 $binary,
  122.                 $mimeType,
  123.                 $extension
  124.             );
  125.         }
  126.         if (null === $binary->getMimeType()) {
  127.             throw new \LogicException(sprintf('The mime type of image %s was not guessed.'$path));
  128.         }
  129.         if (!== mb_strpos($binary->getMimeType(), 'image/')) {
  130.             throw new \LogicException(sprintf('The mime type of image %s must be image/xxx got %s.'$path$binary->getMimeType()));
  131.         }
  132.         return $binary;
  133.     }
  134.     /**
  135.      * Get default image url with the given filter applied.
  136.      *
  137.      * @param string $filter
  138.      *
  139.      * @return string|null
  140.      */
  141.     public function getDefaultImageUrl($filter)
  142.     {
  143.         $config $this->filterConfig->get($filter);
  144.         $defaultImage null;
  145.         if (false === empty($config['default_image'])) {
  146.             $defaultImage $config['default_image'];
  147.         } elseif (!empty($this->globalDefaultImage)) {
  148.             $defaultImage $this->globalDefaultImage;
  149.         }
  150.         return $defaultImage;
  151.     }
  152.     private function getExtension(?string $mimeType): ?string
  153.     {
  154.         if ($this->extensionGuesser instanceof DeprecatedExtensionGuesserInterface) {
  155.             return $this->extensionGuesser->guess($mimeType);
  156.         }
  157.         if (null === $mimeType) {
  158.             return null;
  159.         }
  160.         return $this->extensionGuesser->getExtensions($mimeType)[0] ?? null;
  161.     }
  162. }