1
- #! /usr/bin/env php
2
1
<?php
3
2
4
3
function var_export_short ($ expression , $ return = false ): ?string
@@ -21,87 +20,102 @@ function var_export_short($expression, $return = false): ?string
21
20
22
21
$ template_generate_date = date ('j M Y, g:ia T ' );
23
22
24
- $ downloaded_mimes = file (__DIR__ . '/mime.types ' , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
23
+ $ fn_source_types = __DIR__ . '/mime.types ' ;
24
+ $ fn_source_types_custom = __DIR__ . '/mime.types.custom ' ;
25
25
26
- if (empty ( $ downloaded_mimes )) {
27
- die ('mime.types is empty or not downloded ' );
26
+ if (! is_readable ( $ fn_source_types )) {
27
+ die ('mime.types is not downloaded or not readable ' );
28
28
}
29
+ $ mime_types_default_text = file ($ fn_source_types , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
29
30
30
- $ custom_mimes = file (__DIR__ . '/mime.types.custom ' , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
31
+ $ mime_types_custom_text = [];
32
+ if (is_readable ($ fn_source_types_custom )) {
33
+ $ mime_types_custom_text = file ($ fn_source_types_custom , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
34
+ }
31
35
32
- $ source_mimes = array_merge ($ custom_mimes , $ downloaded_mimes );
36
+ $ source_mimes = array_merge ($ mime_types_custom_text , $ mime_types_default_text );
33
37
34
- $ lines = [];
38
+ $ defs = [];
35
39
36
- // clear comments
37
40
foreach ($ source_mimes as $ line ) {
38
- if (strpos ($ line , '# ' ) !== 0 ) {
41
+ $ line = trim (preg_replace ('~ \\#.*~ ' , '' , $ line ));
42
+
43
+ if (!empty ($ line )) {
39
44
preg_match_all ('/^((\w|\/|\.|-|\+)+)(\s+)([^\n]*)$/im ' , $ line , $ match );
45
+
40
46
$ type = $ match [1 ][0 ];
41
47
$ extensions = explode (' ' , $ match [4 ][0 ]);
42
48
43
- if (!array_key_exists ($ type , $ lines )) {
44
- $ lines [$ type ] = $ extensions ;
49
+ if (!array_key_exists ($ type , $ defs )) {
50
+ $ defs [$ type ] = $ extensions ;
45
51
}
46
52
}
47
53
}
48
54
49
- $ mimes = [] ;
55
+ ksort ( $ defs ) ;
50
56
51
- foreach ($ lines as $ type => $ extensions ) {
52
- foreach ($ extensions as $ extension ) {
53
- if (!isset ($ lines [$ extension ])) {
54
- $ mimes [$ extension ] = $ type ;
55
- }
57
+ $ mapping = [
58
+ // ext -> mime
59
+ 'mimes ' => [],
60
+
61
+ // mime -> ext
62
+ 'extensions ' => []
63
+ ];
64
+
65
+ foreach ($ defs as $ mime => $ exts ) {
66
+ foreach ($ exts as $ extension ) {
67
+ // $mapping['mimes'][$extension][] = $mime;
68
+ // $mapping['mimes'][$extension] = array_unique($mapping['mimes'][$extension]);
69
+ $ mapping ['mimes ' ][$ extension ] = $ mime ;
70
+
71
+ // $mapping['extensions'][$mime][] = $extension;
72
+ // $mapping['extensions'][$mime] = array_unique($mapping['extensions'][$mime]);
56
73
}
74
+ $ mapping ['extensions ' ][$ mime ] = $ exts [0 ];
57
75
}
58
- $ customize_json = __DIR__ . '/customize.json ' ;
59
76
60
- if (file_exists ( $ customize_json )) {
61
- $ entries = json_decode (file_get_contents ($ customize_json ), true );
77
+ if (is_readable ( __DIR__ . ' /customize.json ' )) {
78
+ $ entries = json_decode (file_get_contents (__DIR__ . ' /customize.json ' ), true );
62
79
63
- foreach ($ entries as $ extensions => $ type ) {
80
+ foreach ($ entries as $ extensions => $ mime ) {
64
81
$ extensions = explode (' ' , $ extensions );
65
82
66
83
foreach ($ extensions as $ extension ) {
67
- $ mimes [$ extension ] = $ type ;
84
+ $ mapping ['mimes ' ][$ extension ] = $ mime ;
85
+ }
86
+
87
+ if (!array_key_exists ($ mime , $ mapping ['extensions ' ])) {
88
+ $ mapping ['extensions ' ][$ mime ] = $ extensions [0 ];
68
89
}
69
90
}
70
91
}
92
+ ksort ($ mapping ['mimes ' ]);
93
+ ksort ($ mapping ['extensions ' ]);
71
94
72
- // WRITE JSON DOCUMENT
73
- // ksort($mimes);
95
+ var_dump ($ mapping );
74
96
75
- $ mimetypes_json = __DIR__ . '/mimetypes.json ' ;
97
+ /**
98
+ * Output
99
+ */
76
100
77
- if (file_put_contents ($ mimetypes_json , json_encode ($ mimes , JSON_PRETTY_PRINT ))) {
78
- echo print_r ($ mimes , true )
101
+ $ mimetypes_json = __DIR__ . '/mimetypes.json ' ;
102
+ if (file_put_contents ($ mimetypes_json , json_encode ($ mapping , JSON_PRETTY_PRINT ))) {
103
+ echo print_r ($ mapping , true )
79
104
. PHP_EOL
80
105
. "\033[01;32mSuccessfully wrote {$ mimetypes_json }\033[00m "
81
106
. PHP_EOL ;
82
107
} else {
83
108
echo "Failed to write {$ mimetypes_json }. Please ensure that this file system location is writable. " . PHP_EOL ;
84
109
}
85
110
86
- /*$max_ext_length = 0;
87
- foreach ($mimes as $ext => $type) {
88
- $max_ext_length = max($max_ext_length, strlen($ext));
89
- }
111
+ /*
112
+ * WRITE PHP CLASS
113
+ */
90
114
91
- $text = '';
92
- $text .= "return [" . PHP_EOL;
93
- foreach ($mimes as $ext => $type) {
94
- $text .= " '{$ext}' ";
95
- // $text .= str_repeat(' ', $max_ext_length + 2 - strlen($ext));
96
- $text .= "=> '{$type}'," . PHP_EOL;
97
- }
98
- $text .= " ];";*/
99
-
100
- # WRITE PHP CLASS
101
115
$ content = file_get_contents (__DIR__ . '/template.txt ' );
102
116
$ content = str_replace ('%%generate_datetime%% ' , $ template_generate_date , $ content );
103
- $ content = str_replace ('%%return_array_mime_types%% ' , "return " . var_export_short ($ mimes , true ) . "; " , $ content );
104
- $ content = str_replace ('%%array_mime_types%% ' , var_export_short ($ mimes , true ), $ content );
117
+ // $content = str_replace('%%return_array_mime_types%%', "return " . var_export_short($mapping , true) . ";", $content);
118
+ $ content = str_replace ('%%array_mime_types%% ' , var_export_short ($ mapping , true ), $ content );
105
119
106
120
//$content = str_replace('%%array_mime_types%%', $text, $content); // преформатированный красивый вывод
107
121
@@ -115,3 +129,4 @@ function var_export_short($expression, $return = false): ?string
115
129
// Done.
116
130
echo PHP_EOL ;
117
131
132
+
0 commit comments