1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
5
|
|
|
* |
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\Binary\Loader; |
13
|
|
|
|
14
|
|
|
use League\Flysystem\FilesystemInterface; |
15
|
|
|
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException; |
16
|
|
|
use Liip\ImagineBundle\Exception\InvalidArgumentException; |
17
|
|
|
use Liip\ImagineBundle\Model\Binary; |
18
|
|
|
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface as DeprecatedExtensionGuesserInterface; |
19
|
|
|
use Symfony\Component\Mime\MimeTypesInterface; |
20
|
|
|
|
21
|
|
|
class FlysystemLoader implements LoaderInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var FilesystemInterface |
25
|
|
|
*/ |
26
|
|
|
protected $filesystem; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var MimeTypesInterface|DeprecatedExtensionGuesserInterface |
30
|
|
|
*/ |
31
|
|
|
protected $extensionGuesser; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
$extensionGuesser, |
35
|
|
|
FilesystemInterface $filesystem) |
36
|
|
|
{ |
37
|
|
|
if (!$extensionGuesser instanceof MimeTypesInterface && !$extensionGuesser instanceof DeprecatedExtensionGuesserInterface) { |
38
|
|
|
throw new InvalidArgumentException('$extensionGuesser must be an instance of Symfony\Component\Mime\MimeTypesInterface or Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (interface_exists(MimeTypesInterface::class) && $extensionGuesser instanceof DeprecatedExtensionGuesserInterface) { |
42
|
|
|
@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); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->extensionGuesser = $extensionGuesser; |
46
|
|
|
$this->filesystem = $filesystem; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function find($path) |
53
|
|
|
{ |
54
|
|
|
if (false === $this->filesystem->has($path)) { |
55
|
|
|
throw new NotLoadableException(sprintf('Source image "%s" not found.', $path)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$mimeType = $this->filesystem->getMimetype($path); |
59
|
|
|
|
60
|
|
|
$extension = $this->getExtension($mimeType); |
61
|
|
|
|
62
|
|
|
return new Binary( |
63
|
|
|
$this->filesystem->read($path), |
|
|
|
|
64
|
|
|
$mimeType, |
|
|
|
|
65
|
|
|
$extension |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function getExtension(?string $mimeType): ?string |
70
|
|
|
{ |
71
|
|
|
if ($this->extensionGuesser instanceof DeprecatedExtensionGuesserInterface) { |
72
|
|
|
return $this->extensionGuesser->guess($mimeType); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (null === $mimeType) { |
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->extensionGuesser->getExtensions($mimeType)[0] ?? null; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: