1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of Esi\Mimey. |
7
|
|
|
* |
8
|
|
|
* (c) Eric Sizemore <[email protected]> |
9
|
|
|
* (c) Ricardo Boss <[email protected]> |
10
|
|
|
* (c) Ralph Khattar <[email protected]> |
11
|
|
|
* |
12
|
|
|
* This source file is subject to the MIT license. For the full copyright, |
13
|
|
|
* license information, and credits/acknowledgements, please view the LICENSE |
14
|
|
|
* and README files that were distributed with this source code. |
15
|
|
|
*/ |
16
|
|
|
/** |
17
|
|
|
* Esi\Mimey is a fork of Elephox\Mimey (https://github.com/elephox-dev/mimey) which is: |
18
|
|
|
* Copyright (c) 2022 Ricardo Boss |
19
|
|
|
* Elephox\Mimey is a fork of ralouphie/mimey (https://github.com/ralouphie/mimey) which is: |
20
|
|
|
* Copyright (c) 2016 Ralph Khattar |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Esi\Mimey\Mapping; |
24
|
|
|
|
25
|
|
|
use Esi\Mimey\Interfaces\BuilderInterface; |
26
|
|
|
use RuntimeException; |
27
|
|
|
use Throwable; |
28
|
|
|
|
29
|
|
|
use function array_unique; |
30
|
|
|
use function array_unshift; |
31
|
|
|
use function file_get_contents; |
32
|
|
|
use function file_put_contents; |
33
|
|
|
use function json_decode; |
34
|
|
|
use function json_encode; |
35
|
|
|
|
36
|
|
|
use const JSON_PRETTY_PRINT; |
37
|
|
|
use const JSON_THROW_ON_ERROR; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Class for converting MIME types to file extensions and vice versa. |
41
|
|
|
* |
42
|
|
|
* @phpstan-import-type MimeTypeMap from \Esi\Mimey\MimeTypes |
43
|
|
|
*/ |
44
|
|
|
class Builder implements BuilderInterface |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* Create a new mapping builder. |
48
|
|
|
* |
49
|
|
|
* @param MimeTypeMap $mapping An associative array containing two entries. |
50
|
|
|
* See `MimeTypes` constructor for details. |
51
|
|
|
*/ |
52
|
6 |
|
private function __construct(protected array $mapping) {} |
53
|
|
|
|
54
|
6 |
|
#[\Override] |
55
|
|
|
public function add(string $mime, string $extension, bool $prependExtension = true, bool $prependMime = true): void |
56
|
|
|
{ |
57
|
6 |
|
$existingExtensions = $this->mapping['extensions'][$mime] ?? []; |
58
|
6 |
|
$existingMimes = $this->mapping['mimes'][$extension] ?? []; |
59
|
|
|
|
60
|
6 |
|
if ($prependExtension) { |
61
|
6 |
|
array_unshift($existingExtensions, $extension); |
62
|
|
|
} else { |
63
|
1 |
|
$existingExtensions[] = $extension; |
64
|
|
|
} |
65
|
|
|
|
66
|
6 |
|
if ($prependMime) { |
67
|
6 |
|
array_unshift($existingMimes, $mime); |
68
|
|
|
} else { |
69
|
1 |
|
$existingMimes[] = $mime; |
70
|
|
|
} |
71
|
|
|
|
72
|
6 |
|
$this->mapping['extensions'][$mime] = array_unique($existingExtensions); |
73
|
6 |
|
$this->mapping['mimes'][$extension] = array_unique($existingMimes); |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
#[\Override] |
77
|
|
|
public function compile(bool $pretty = false): string |
78
|
|
|
{ |
79
|
2 |
|
return json_encode($this->getMapping(), flags: JSON_THROW_ON_ERROR | ($pretty ? JSON_PRETTY_PRINT : 0)); |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
#[\Override] |
83
|
|
|
public function getMapping(): array |
84
|
|
|
{ |
85
|
6 |
|
return $this->mapping; |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
#[\Override] |
89
|
|
|
public function save(string $file, int $flags = 0, mixed $context = null): false|int |
90
|
|
|
{ |
91
|
2 |
|
if (\is_resource($context)) { |
92
|
1 |
|
return file_put_contents($file, $this->compile(), $flags, $context); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return file_put_contents($file, $this->compile(), $flags); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Create a new mapping builder that has no types defined. |
100
|
|
|
* |
101
|
|
|
* @return Builder A mapping builder with no types defined. |
102
|
|
|
*/ |
103
|
5 |
|
public static function blank(): Builder |
104
|
|
|
{ |
105
|
5 |
|
return new self([ |
106
|
5 |
|
'mimes' => [], |
107
|
5 |
|
'extensions' => [], |
108
|
5 |
|
]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Create a new mapping builder based on the built-in types. |
113
|
|
|
* |
114
|
|
|
* @return Builder A mapping builder with built-in types loaded. |
115
|
|
|
*/ |
116
|
1 |
|
public static function create(): Builder |
117
|
|
|
{ |
118
|
1 |
|
return self::load(\dirname(__DIR__, 2) . '/dist/mime.types.min.json'); |
119
|
|
|
} |
120
|
|
|
|
121
|
4 |
|
#[\Override] |
122
|
|
|
public static function load(string $file): Builder |
123
|
|
|
{ |
124
|
|
|
try { |
125
|
|
|
/** @var MimeTypeMap $json * */ |
126
|
4 |
|
$json = json_decode((string) file_get_contents($file), true, flags: JSON_THROW_ON_ERROR); |
127
|
|
|
|
128
|
3 |
|
return new self($json); |
129
|
1 |
|
} catch (Throwable $throwable) { |
130
|
1 |
|
throw new RuntimeException(\sprintf('Unable to parse built-in types at %s', $file), 0, $throwable); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|