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 Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException; |
15
|
|
|
use Liip\ImagineBundle\Exception\InvalidArgumentException; |
16
|
|
|
use Liip\ImagineBundle\Model\FileBinary; |
17
|
|
|
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface; |
18
|
|
|
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; |
19
|
|
|
|
20
|
|
|
class FileSystemLoader implements LoaderInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var MimeTypeGuesserInterface |
24
|
|
|
*/ |
25
|
|
|
protected $mimeTypeGuesser; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ExtensionGuesserInterface |
29
|
|
|
*/ |
30
|
|
|
protected $extensionGuesser; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $dataRoots; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param MimeTypeGuesserInterface $mimeTypeGuesser |
39
|
|
|
* @param ExtensionGuesserInterface $extensionGuesser |
40
|
|
|
* @param string[] $dataRoots |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
MimeTypeGuesserInterface $mimeTypeGuesser, |
44
|
|
|
ExtensionGuesserInterface $extensionGuesser, |
45
|
|
|
$dataRoots |
46
|
|
|
) { |
47
|
|
|
$this->mimeTypeGuesser = $mimeTypeGuesser; |
48
|
|
|
$this->extensionGuesser = $extensionGuesser; |
49
|
|
|
|
50
|
|
|
$this->dataRoots = array_map(function ($root) { |
|
|
|
|
51
|
|
|
if (!empty($root) && false !== $realRoot = realpath($root)) { |
52
|
|
|
return $realRoot; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
throw new InvalidArgumentException(sprintf('Root image path not resolvable "%s"', $root)); |
56
|
|
|
}, (array) $dataRoots); |
57
|
|
|
|
58
|
|
|
if (count($this->dataRoots) === 0) { |
59
|
|
|
throw new InvalidArgumentException('One or more data root paths must be specified.'); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function find($path) |
67
|
|
|
{ |
68
|
|
|
$path = $this->absolutePathRestrict($this->absolutePathLocate($path)); |
69
|
|
|
$mime = $this->mimeTypeGuesser->guess($path); |
70
|
|
|
|
71
|
|
|
return new FileBinary($path, $mime, $this->extensionGuesser->guess($mime)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $path |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
private function absolutePathLocate($path) |
80
|
|
|
{ |
81
|
|
|
foreach ($this->dataRoots as $root) { |
|
|
|
|
82
|
|
|
if (false !== $realPath = realpath($root.DIRECTORY_SEPARATOR.$path)) { |
83
|
|
|
return $realPath; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
throw new NotLoadableException(sprintf('Source image not resolvable "%s" in root path(s) "%s"', |
88
|
|
|
$path, implode(':', $this->dataRoots))); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $path |
93
|
|
|
* |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
private function absolutePathRestrict($path) |
97
|
|
|
{ |
98
|
|
|
foreach ($this->dataRoots as $root) { |
|
|
|
|
99
|
|
|
if (0 === strpos($path, $root)) { |
100
|
|
|
return $path; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
throw new NotLoadableException(sprintf('Source image invalid "%s" as it is outside of the defined root path(s) "%s"', |
105
|
|
|
$path, implode(':', $this->dataRoots))); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..