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

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