vendor/liip/imagine-bundle/Binary/Loader/FileSystemLoader.php line 74

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\Binary\Loader;
  11. use Liip\ImagineBundle\Binary\Locator\LocatorInterface;
  12. use Liip\ImagineBundle\Exception\InvalidArgumentException;
  13. use Liip\ImagineBundle\Model\FileBinary;
  14. use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface as DeprecatedExtensionGuesserInterface;
  15. use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface as DeprecatedMimeTypeGuesserInterface;
  16. use Symfony\Component\Mime\MimeTypeGuesserInterface;
  17. use Symfony\Component\Mime\MimeTypesInterface;
  18. class FileSystemLoader implements LoaderInterface
  19. {
  20.     /**
  21.      * @var MimeTypeGuesserInterface|DeprecatedMimeTypeGuesserInterface
  22.      */
  23.     protected $mimeTypeGuesser;
  24.     /**
  25.      * @var MimeTypesInterface|DeprecatedExtensionGuesserInterface
  26.      */
  27.     protected $extensionGuesser;
  28.     /**
  29.      * @var LocatorInterface
  30.      */
  31.     protected $locator;
  32.     /**
  33.      * @param MimeTypeGuesserInterface|DeprecatedMimeTypeGuesserInterface $mimeGuesser
  34.      * @param MimeTypesInterface|DeprecatedExtensionGuesserInterface      $extensionGuesser
  35.      */
  36.     public function __construct(
  37.         $mimeGuesser,
  38.         $extensionGuesser,
  39.         LocatorInterface $locator
  40.     ) {
  41.         if (!$mimeGuesser instanceof MimeTypeGuesserInterface && !$mimeGuesser instanceof DeprecatedMimeTypeGuesserInterface) {
  42.             throw new InvalidArgumentException('$mimeGuesser must be an instance of Symfony\Component\Mime\MimeTypeGuesserInterface or Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface');
  43.         }
  44.         if (!$extensionGuesser instanceof MimeTypesInterface && !$extensionGuesser instanceof DeprecatedExtensionGuesserInterface) {
  45.             throw new InvalidArgumentException('$extensionGuesser must be an instance of Symfony\Component\Mime\MimeTypesInterface or Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface');
  46.         }
  47.         if (interface_exists(MimeTypeGuesserInterface::class) && $mimeGuesser instanceof DeprecatedMimeTypeGuesserInterface) {
  48.             @trigger_error(sprintf('Passing a %s to "%s()" is deprecated since Symfony 4.3, pass a "%s" instead.'DeprecatedMimeTypeGuesserInterface::class, __METHOD__MimeTypeGuesserInterface::class), E_USER_DEPRECATED);
  49.         }
  50.         if (interface_exists(MimeTypesInterface::class) && $extensionGuesser instanceof DeprecatedExtensionGuesserInterface) {
  51.             @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);
  52.         }
  53.         $this->mimeTypeGuesser $mimeGuesser;
  54.         $this->extensionGuesser $extensionGuesser;
  55.         $this->locator $locator;
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      */
  60.     public function find($path)
  61.     {
  62.         $path $this->locator->locate($path);
  63.         $mimeType $this->mimeTypeGuesser instanceof DeprecatedMimeTypeGuesserInterface $this->mimeTypeGuesser->guess($path) : $this->mimeTypeGuesser->guessMimeType($path);
  64.         $extension $this->getExtension($mimeType);
  65.         return new FileBinary($path$mimeType$extension);
  66.     }
  67.     private function getExtension(?string $mimeType): ?string
  68.     {
  69.         if ($this->extensionGuesser instanceof DeprecatedExtensionGuesserInterface) {
  70.             return $this->extensionGuesser->guess($mimeType);
  71.         }
  72.         if (null === $mimeType) {
  73.             return null;
  74.         }
  75.         return $this->extensionGuesser->getExtensions($mimeType)[0] ?? null;
  76.     }
  77. }