|
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\Binary\Locator\FileSystemLocator; |
|
15
|
|
|
use Liip\ImagineBundle\Binary\Locator\LocatorInterface; |
|
16
|
|
|
use Liip\ImagineBundle\Exception\InvalidArgumentException; |
|
17
|
|
|
use Liip\ImagineBundle\Model\FileBinary; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; |
|
20
|
|
|
|
|
21
|
|
|
class FileSystemLoader implements LoaderInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var MimeTypeGuesserInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $mimeTypeGuesser; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ExtensionGuesserInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $extensionGuesser; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var LocatorInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $locator; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* This method will continue to support two prior, deprecated signitures for the duration of the 1.x |
|
40
|
|
|
* release. The currently documented signiture will be the only valid usage once 2.0 is release. You |
|
41
|
|
|
* can reference PR-963 {@see https://github.com/liip/LiipImagineBundle/pull/963} for more information. |
|
42
|
|
|
* |
|
43
|
|
|
* @param MimeTypeGuesserInterface $mimeGuesser |
|
44
|
|
|
* @param ExtensionGuesserInterface $extensionGuesser |
|
45
|
|
|
* @param LocatorInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(MimeTypeGuesserInterface $mimeGuesser, ExtensionGuesserInterface $extensionGuesser, $locator) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->mimeTypeGuesser = $mimeGuesser; |
|
50
|
|
|
$this->extensionGuesser = $extensionGuesser; |
|
51
|
|
|
|
|
52
|
|
|
if ($locator instanceof LocatorInterface) { // post-1.9.0 behavior |
|
53
|
|
|
$this->locator = $locator; |
|
54
|
|
|
} elseif (is_array($locator) || is_string($locator)) { // pre-1.9.0 behaviour |
|
55
|
|
|
if (count((array) $locator) === 0) { |
|
56
|
|
|
throw new InvalidArgumentException('One or more data root paths must be specified.'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (func_num_args() >= 4) { |
|
60
|
|
|
if (func_get_arg(3) instanceof LocatorInterface) { |
|
61
|
|
|
@trigger_error(sprintf( |
|
|
|
|
|
|
62
|
|
|
'Passing a LocatorInterface as fourth parameter to %s() is deprecated. It needs to be the '. |
|
63
|
|
|
'third parameter. The previous third parameter (data roots) is removed and the data roots must '. |
|
64
|
|
|
'now be passed as a constructor argument to the LocatorInterface passed to this method.', __METHOD__ |
|
65
|
|
|
), E_USER_DEPRECATED); |
|
66
|
|
|
|
|
67
|
|
|
$this->locator = func_get_arg(3); |
|
68
|
|
|
$this->locator->setOptions(array('roots' => (array) $locator)); |
|
69
|
|
|
} else { |
|
70
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
71
|
|
|
'Unknown call to %s(). Please check the method signature.', __METHOD__ |
|
72
|
|
|
)); |
|
73
|
|
|
} |
|
74
|
|
|
} else { |
|
75
|
|
|
@trigger_error(sprintf( |
|
|
|
|
|
|
76
|
|
|
'Method %s() will expect the third parameter to be a LocatorInterface in version 2.0. Defining '. |
|
77
|
|
|
'data roots instead is deprecated since version 1.9.0', __METHOD__ |
|
78
|
|
|
), E_USER_DEPRECATED); |
|
79
|
|
|
|
|
80
|
|
|
$this->locator = new FileSystemLocator((array) $locator); |
|
81
|
|
|
} |
|
82
|
|
|
} else { // invalid behavior |
|
83
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
84
|
|
|
'Method %s() expects a LocatorInterface for the third argument.', __METHOD__ |
|
85
|
|
|
)); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritdoc} |
|
91
|
|
|
*/ |
|
92
|
|
|
public function find($path) |
|
93
|
|
|
{ |
|
94
|
|
|
$path = $this->locator->locate($path); |
|
95
|
|
|
$mime = $this->mimeTypeGuesser->guess($path); |
|
96
|
|
|
|
|
97
|
|
|
return new FileBinary($path, $mime, $this->extensionGuesser->guess($mime)); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: