-
Notifications
You must be signed in to change notification settings - Fork 380
/
FileSystemLoader.php
57 lines (46 loc) · 1.55 KB
/
FileSystemLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Liip\ImagineBundle\Binary\Loader;
use Liip\ImagineBundle\Binary\Locator\LocatorInterface;
use Liip\ImagineBundle\Model\FileBinary;
use Symfony\Component\Mime\MimeTypeGuesserInterface;
use Symfony\Component\Mime\MimeTypesInterface;
class FileSystemLoader implements LoaderInterface
{
protected MimeTypeGuesserInterface $mimeTypeGuesser;
protected MimeTypesInterface $extensionGuesser;
protected LocatorInterface $locator;
public function __construct(
MimeTypeGuesserInterface $mimeGuesser,
MimeTypesInterface $extensionGuesser,
LocatorInterface $locator
) {
$this->mimeTypeGuesser = $mimeGuesser;
$this->extensionGuesser = $extensionGuesser;
$this->locator = $locator;
}
/**
* {@inheritdoc}
*/
public function find($path): FileBinary
{
$path = $this->locator->locate($path);
$mimeType = $this->mimeTypeGuesser->guessMimeType($path);
$extension = $this->getExtension($mimeType);
return new FileBinary($path, $mimeType, $extension);
}
private function getExtension(?string $mimeType): ?string
{
if (null === $mimeType) {
return null;
}
return $this->extensionGuesser->getExtensions($mimeType)[0] ?? null;
}
}