|
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
|
|
|
* Default URL where to read the specification from. |
|
14
|
|
|
* |
|
15
|
|
|
* The apache httpd project contains the most complete list of file |
|
16
|
|
|
* extension to mime type mapping on the planet. We use it to update our |
|
17
|
|
|
* own list. |
|
18
|
|
|
*/ |
|
19
|
|
|
const DEFAULT_SOURCE_FILE = 'http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=co'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Returns the default file with override commands to be executed. |
|
23
|
|
|
* |
|
24
|
|
|
* The YAML file provides an array of calls to MapHandler methods to be |
|
25
|
|
|
* executed sequentially. Each entry indicates the method to be invoked and |
|
26
|
|
|
* the arguments to be passed in. |
|
27
|
|
|
* |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public static function getDefaultOverrideFile() |
|
31
|
|
|
{ |
|
32
|
1 |
|
return __DIR__ . '/../resources/apache_overrides.yml'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Creates a new type-to-extension map reading from a file. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $source_file |
|
39
|
|
|
* (Optional) the source file. Defaults to the Apache source bind_textdomain_codeset |
|
40
|
|
|
* repository file where MIME types and file extensions are associated. |
|
41
|
|
|
* |
|
42
|
|
|
* @throws \RuntimeException if file I/O error occurs. |
|
43
|
|
|
* |
|
44
|
|
|
* @return AbstractMap |
|
45
|
|
|
* The new map. |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function createMapFromSourceFile($source_file = MapUpdater::DEFAULT_SOURCE_FILE) |
|
48
|
|
|
{ |
|
49
|
3 |
|
$map = MapHandler::map('\FileEye\MimeMap\Map\EmptyMap'); |
|
50
|
3 |
|
$lines = file($source_file); |
|
51
|
3 |
|
foreach ($lines as $line) { |
|
52
|
2 |
|
if ($line{0} == '#') { |
|
53
|
2 |
|
continue; |
|
54
|
|
|
} |
|
55
|
2 |
|
$line = preg_replace("#\\s+#", ' ', trim($line)); |
|
56
|
2 |
|
$parts = explode(' ', $line); |
|
57
|
2 |
|
$type = array_shift($parts); |
|
58
|
2 |
|
foreach ($parts as $extension) { |
|
59
|
2 |
|
$map->addMapping($type, $extension); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
3 |
|
$map_array = $map->getMapArray(); |
|
63
|
3 |
|
if (empty($map_array)) { |
|
64
|
1 |
|
throw new \RuntimeException('No data found in file ' . $source_file); |
|
65
|
|
|
} |
|
66
|
2 |
|
return $map; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Applies to the map an array of overrides. |
|
71
|
|
|
* |
|
72
|
|
|
* @param AbstractMap $map |
|
73
|
|
|
* The map. |
|
74
|
|
|
* @param array $overrides |
|
75
|
|
|
* The overrides to be applied. |
|
76
|
|
|
* |
|
77
|
|
|
* @return void |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public function applyOverrides(AbstractMap $map, array $overrides) |
|
80
|
|
|
{ |
|
81
|
1 |
|
foreach ($overrides as $command) { |
|
82
|
1 |
|
call_user_func_array([$map, $command[0]], $command[1]); |
|
83
|
|
|
} |
|
84
|
1 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Updates the map at a destination PHP file. |
|
88
|
|
|
* |
|
89
|
|
|
* @param AbstractMap $map |
|
90
|
|
|
* The map. |
|
91
|
|
|
* |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function writeMapToPhpClassFile(AbstractMap $map, $file) |
|
95
|
|
|
{ |
|
96
|
1 |
|
$content = preg_replace( |
|
97
|
1 |
|
'#protected static \$map = (.+?);#s', |
|
98
|
1 |
|
"protected static \$map = " . var_export($map->getMapArray(), true) . ";", |
|
99
|
1 |
|
file_get_contents($file) |
|
100
|
|
|
); |
|
101
|
1 |
|
file_put_contents($file, $content); |
|
102
|
1 |
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|