1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FileEye\MimeMap; |
4
|
|
|
|
5
|
|
|
use FileEye\MimeMap\Map\AbstractMap; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Compiles the MIME type to file extension map. |
9
|
|
|
*/ |
10
|
|
|
class MapUpdater |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The AbstractMap object to update. |
14
|
|
|
* |
15
|
|
|
* @var AbstractMap |
16
|
|
|
*/ |
17
|
|
|
protected $map; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Returns the default file with override commands to be executed. |
21
|
|
|
* |
22
|
|
|
* The YAML file provides an array of calls to MapHandler methods to be |
23
|
|
|
* executed sequentially. Each entry indicates the method to be invoked and |
24
|
|
|
* the arguments to be passed in. |
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
1 |
|
public static function getDefaultMapBuildFile() |
29
|
|
|
{ |
30
|
1 |
|
return __DIR__ . '/../resources/default_map_build.yml'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor. |
35
|
|
|
* |
36
|
|
|
* @param AbstractMap $map |
37
|
|
|
* The map. |
38
|
|
|
*/ |
39
|
7 |
|
public function __construct(AbstractMap $map) |
40
|
|
|
{ |
41
|
7 |
|
$this->map = $map; |
42
|
7 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Loads a new type-to-extension map reading from a file in Apache format. |
46
|
|
|
* |
47
|
|
|
* @param string $source_file |
48
|
|
|
* The source file. The file must conform to the format in the Apache |
49
|
|
|
* source code repository file where MIME types and file extensions are |
50
|
|
|
* associated. |
51
|
|
|
* |
52
|
|
|
* @return string[] |
53
|
|
|
* An array of error messages. |
54
|
|
|
*/ |
55
|
3 |
|
public function loadMapFromApacheFile($source_file) |
56
|
|
|
{ |
57
|
3 |
|
$errors = []; |
58
|
|
|
|
59
|
3 |
|
$lines = file($source_file); |
60
|
3 |
|
foreach ($lines as $line) { |
61
|
2 |
|
if ($line{0} == '#') { |
62
|
2 |
|
continue; |
63
|
|
|
} |
64
|
2 |
|
$line = preg_replace("#\\s+#", ' ', trim($line)); |
65
|
2 |
|
$parts = explode(' ', $line); |
66
|
2 |
|
$type = array_shift($parts); |
67
|
2 |
|
foreach ($parts as $extension) { |
68
|
2 |
|
$this->map->addTypeExtensionMapping($type, $extension); |
69
|
|
|
} |
70
|
|
|
} |
71
|
3 |
|
$this->map->sort(); |
72
|
|
|
|
73
|
3 |
|
return $errors; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Loads a new type-to-extension map reading from a Freedesktop.org file. |
78
|
|
|
* |
79
|
|
|
* @param string $source_file |
80
|
|
|
* The source file. The file must conform to the format in the |
81
|
|
|
* Freedesktop.org database. |
82
|
|
|
* |
83
|
|
|
* @return string[] |
84
|
|
|
* An array of error messages. |
85
|
|
|
*/ |
86
|
2 |
|
public function loadMapFromFreedesktopFile($source_file) |
87
|
|
|
{ |
88
|
2 |
|
$errors = []; |
89
|
2 |
|
$xml = simplexml_load_string(file_get_contents($source_file)); |
90
|
2 |
|
$aliases = []; |
91
|
|
|
|
92
|
2 |
|
foreach ($xml as $node) { |
93
|
1 |
|
$exts = []; |
94
|
1 |
|
foreach ($node->glob as $glob) { |
95
|
1 |
|
$pattern = (string) $glob['pattern']; |
96
|
1 |
|
if ('*' != $pattern[0] || '.' != $pattern[1]) { |
97
|
1 |
|
continue; |
98
|
|
|
} |
99
|
1 |
|
$exts[] = substr($pattern, 2); |
100
|
|
|
} |
101
|
1 |
|
if (empty($exts)) { |
102
|
1 |
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
$type = (string) $node['type']; |
106
|
|
|
|
107
|
|
|
// Add description. |
108
|
1 |
|
if (isset($node->comment)) { |
109
|
1 |
|
$this->map->addTypeDescription($type, (string) $node->comment[0]); |
110
|
|
|
} |
111
|
1 |
|
if (isset($node->acronym)) { |
112
|
1 |
|
$acronym = (string) $node->acronym; |
113
|
1 |
|
if (isset($node->{'expanded-acronym'})) { |
114
|
1 |
|
$acronym .= ': ' . (string) $node->{'expanded-acronym'}; |
115
|
|
|
} |
116
|
1 |
|
$this->map->addTypeDescription($type, $acronym); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Add extensions. |
120
|
1 |
|
foreach ($exts as $ext) { |
121
|
1 |
|
$this->map->addTypeExtensionMapping($type, $ext); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// All aliases are accumulated and processed at the end of the |
125
|
|
|
// cycle to allow proper consistency checking on the completely |
126
|
|
|
// developed list of types. |
127
|
1 |
|
foreach ($node->alias as $alias) { |
128
|
1 |
|
$aliases[$type][] = (string) $alias['type']; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Add all the aliases, provide logging of errors. |
133
|
2 |
|
foreach ($aliases as $type => $a) { |
134
|
1 |
|
foreach ($a as $alias) { |
135
|
|
|
try { |
136
|
1 |
|
$this->map->addTypeAlias($type, $alias); |
137
|
1 |
|
} catch (MappingException $e) { |
138
|
1 |
|
$errors[] = $e->getMessage(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
2 |
|
$this->map->sort(); |
143
|
|
|
|
144
|
2 |
|
return $errors; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Applies to the map an array of overrides. |
149
|
|
|
* |
150
|
|
|
* @param array $overrides |
151
|
|
|
* The overrides to be applied. |
152
|
|
|
* |
153
|
|
|
* @return string[] |
154
|
|
|
* An array of error messages. |
155
|
|
|
*/ |
156
|
2 |
|
public function applyOverrides(array $overrides) |
157
|
|
|
{ |
158
|
2 |
|
$errors = []; |
159
|
|
|
|
160
|
2 |
|
foreach ($overrides as $command) { |
161
|
|
|
try { |
162
|
2 |
|
call_user_func_array([$this->map, $command[0]], $command[1]); |
163
|
|
|
} catch (MappingException $e) { |
164
|
2 |
|
$errors[] = $e->getMessage(); |
165
|
|
|
} |
166
|
|
|
} |
167
|
2 |
|
$this->map->sort(); |
168
|
|
|
|
169
|
2 |
|
return $errors; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Updates the map at a destination PHP file. |
174
|
|
|
* |
175
|
|
|
* @return $this |
176
|
|
|
*/ |
177
|
1 |
|
public function writeMapToPhpClassFile($file) |
178
|
|
|
{ |
179
|
1 |
|
$content = preg_replace( |
180
|
1 |
|
'#protected static \$map = (.+?);#s', |
181
|
1 |
|
"protected static \$map = " . var_export($this->map->getMapArray(), true) . ";", |
182
|
1 |
|
file_get_contents($file) |
183
|
|
|
); |
184
|
1 |
|
file_put_contents($file, $content); |
185
|
1 |
|
return $this; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|