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

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