|
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\File\Attributes\Resolver; |
|
13
|
|
|
|
|
14
|
|
|
use Liip\ImagineBundle\File\Attributes\Attributes; |
|
15
|
|
|
use Liip\ImagineBundle\File\Attributes\ContentTypeAttribute; |
|
16
|
|
|
use Liip\ImagineBundle\File\Attributes\ExtensionAttribute; |
|
17
|
|
|
use Liip\ImagineBundle\File\Attributes\Guesser\ContentTypeGuesserInterface; |
|
18
|
|
|
use Liip\ImagineBundle\File\Attributes\Guesser\ExtensionGuesserInterface; |
|
19
|
|
|
use Liip\ImagineBundle\File\FileBlobInterface; |
|
20
|
|
|
use Liip\ImagineBundle\File\FileInterface; |
|
21
|
|
|
use Liip\ImagineBundle\File\FilePathInterface; |
|
22
|
|
|
use Liip\ImagineBundle\File\FileTemp; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @author Rob Frawley 2nd <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
final class FileAttributesResolver |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var ContentTypeGuesserInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $contentTypeGuesser; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var ExtensionGuesserInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $extensionGuesser; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param ContentTypeGuesserInterface $contentTypeGuesser |
|
41
|
|
|
* @param ExtensionGuesserInterface $extensionGuesser |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(ContentTypeGuesserInterface $contentTypeGuesser, ExtensionGuesserInterface $extensionGuesser) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->contentTypeGuesser = $contentTypeGuesser; |
|
46
|
|
|
$this->extensionGuesser = $extensionGuesser; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param FileInterface|string $file |
|
51
|
|
|
* |
|
52
|
|
|
* @return Attributes |
|
53
|
|
|
*/ |
|
54
|
|
|
public function resolve($file): Attributes |
|
55
|
|
|
{ |
|
56
|
|
|
if ($file instanceof FileBlobInterface) { |
|
57
|
|
|
return $this->resolveFileBlob($file); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ($file instanceof FilePathInterface) { |
|
61
|
|
|
return $this->resolveFilePath($file); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $this->guess($file); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param FileInterface $file |
|
69
|
|
|
* |
|
70
|
|
|
* @return Attributes |
|
71
|
|
|
*/ |
|
72
|
|
|
public function resolveFileBlob(FileInterface $file): Attributes |
|
73
|
|
|
{ |
|
74
|
|
|
$temporary = new FileTemp('file-attributes-resolver'); |
|
75
|
|
|
$temporary->acquire(); |
|
76
|
|
|
|
|
77
|
|
|
if ($file->hasContents()) { |
|
78
|
|
|
$temporary->setContents($file->getContents()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
try { |
|
82
|
|
|
return $this->resolveFilePath($temporary); |
|
83
|
|
|
} finally { |
|
84
|
|
|
$temporary->release(); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param FilePathInterface $file |
|
90
|
|
|
* |
|
91
|
|
|
* @return Attributes |
|
92
|
|
|
*/ |
|
93
|
|
|
public function resolveFilePath(FilePathInterface $file): Attributes |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->guess($file->getFile()->getPathname()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string $path |
|
100
|
|
|
* |
|
101
|
|
|
* @return Attributes |
|
102
|
|
|
*/ |
|
103
|
|
|
private function guess(string $path): Attributes |
|
104
|
|
|
{ |
|
105
|
|
|
$c = ContentTypeAttribute::create( |
|
106
|
|
|
$this->contentTypeGuesser->guess($path) |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
$e = ExtensionAttribute::create( |
|
110
|
|
|
$this->extensionGuesser->guess($c->stringify()) |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
return new Attributes($c, $e); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.