vendor/liip/imagine-bundle/Binary/Loader/FlysystemV2Loader.php line 56

  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 League\Flysystem\FilesystemException;
  12. use League\Flysystem\FilesystemOperator;
  13. use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
  14. use Liip\ImagineBundle\Model\Binary;
  15. use Symfony\Component\Mime\MimeTypesInterface;
  16. class FlysystemV2Loader implements LoaderInterface
  17. {
  18.     /**
  19.      * @var FilesystemOperator
  20.      */
  21.     protected $filesystem;
  22.     /**
  23.      * @var MimeTypesInterface
  24.      */
  25.     protected $extensionGuesser;
  26.     public function __construct(
  27.         MimeTypesInterface $extensionGuesser,
  28.         FilesystemOperator $filesystem
  29.     ) {
  30.         $this->extensionGuesser $extensionGuesser;
  31.         $this->filesystem $filesystem;
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public function find($path)
  37.     {
  38.         try {
  39.             $mimeType $this->filesystem->mimeType($path);
  40.             $extension $this->getExtension($mimeType);
  41.             return new Binary(
  42.                 $this->filesystem->read($path),
  43.                 $mimeType,
  44.                 $extension
  45.             );
  46.         } catch (FilesystemException $exception) {
  47.             throw new NotLoadableException(sprintf('Source image "%s" not found.'$path), 0$exception);
  48.         }
  49.     }
  50.     private function getExtension(?string $mimeType): ?string
  51.     {
  52.         return $this->extensionGuesser->getExtensions($mimeType)[0] ?? null;
  53.     }
  54. }