Skip to content

Commit 57d7c42

Browse files
1.3.0
* [+] New generator and new internal storage format * [*] new template of `MimeTypes` class * [*] new tests
1 parent 17f7db6 commit 57d7c42

File tree

8 files changed

+3785
-2141
lines changed

8 files changed

+3785
-2141
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ $ext = MimeTypes::getExtension('application/json');
8585
$type = MimeTypes::fromExtension('.json');
8686

8787
// 'application/json'
88-
// or (use alias)
89-
90-
$type = MimeTypes::getMimeType('.json');
9188
```
9289

9390
NB:

makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ build: ##@build Generate new mimetypes.json and src\Mimetypes.php
1010
@php ./tools/generate.php
1111

1212
test: ##@test PHPUnit tests
13-
@php ./vendor/bin/phpunit
13+
@php ./vendor/bin/phpunit --bootstrap vendor/autoload.php --testdox tests
1414

1515
# ------------------------------------------------
1616
# Add the following 'help' target to your makefile, add help text after each target name starting with '\#\#'

src/MimeTypes.php

Lines changed: 1837 additions & 1038 deletions
Large diffs are not rendered by default.

src/MimeTypesInterface.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,44 @@
44

55
interface MimeTypesInterface
66
{
7+
78
/**
8-
* Get all available mime types
9+
* Get all available mimetypes as array keys
910
*
10-
* @return string[]
11+
* @return array
1112
*/
12-
public static function getMimeTypes():array;
13+
public static function getAllMimeTypes():array;
1314

1415
/**
15-
* Get EXTENSION from MIMETYPE string
16+
* Get all available extensios (as array keys)
1617
*
17-
* @param string $mime_type
18-
* @return string
18+
* @return array
1919
*/
20-
public static function getExtension(string $mime_type):string;
20+
public static function getAllExtensions():array;
2121

2222
/**
23-
* Get mimetype from extension (extension may contain dot)
23+
* Get EXTENSION from MIMETYPE string
2424
*
25-
* @param string $extension
25+
* @param string $mime_type
2626
* @return string
2727
*/
28-
public static function fromExtension(string $extension):string;
28+
public static function getExtension(string $mime_type):string;
2929

3030
/**
31-
* Alias of fromExtension()
31+
* Get MIMETYPE from EXTENSION (extension may contain dot)
3232
*
3333
* @param string $extension
3434
* @return string
3535
*/
36-
public static function getMimeType(string $extension):string;
36+
public static function fromExtension(string $extension):string;
3737

3838
/**
39-
* Get mimetype from filename with extension
39+
* get MIMETYPE from filename with extension
4040
*
4141
* @param string $filename
4242
* @return string
4343
*/
4444
public static function fromFilename(string $filename):string;
4545

4646

47-
48-
49-
50-
5147
}

tests/MimeTypesTest.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,58 @@
55

66
class MimeTypesTest extends TestCase {
77

8+
/**
9+
* @return void
10+
* @testdox get mimetype for unknown (undefined extension)
11+
*/
812
public function testGetMimeTypeUndefined()
913
{
1014
$this->assertEquals('', MimeTypes::fromExtension('undefined'));
1115
}
1216

17+
/**
18+
* @return void
19+
* @testdox get extension for undefined mimetype
20+
*/
1321
public function testGetExtensionUndefined()
1422
{
1523
$this->assertEquals('', MimeTypes::getExtension('undefined'));
1624
}
1725

18-
public function testVaidExtensions()
26+
/**
27+
* @return void
28+
* @testdox get mimetype for valid extension (.json)
29+
*/
30+
public function testGetMimeType()
1931
{
20-
$this->assertEquals('json', MimeTypes::getExtension('application/json'));
2132
$this->assertEquals('application/json', MimeTypes::fromExtension('.json'));
2233
}
2334

35+
/**
36+
* @return void
37+
* @testdox get Extension for valid mimetype 'application/json'
38+
*/
39+
public function getGetExtension()
40+
{
41+
$this->assertEquals('json', MimeTypes::getExtension('application/json'));
42+
}
43+
44+
/**
45+
* @return void
46+
* @testdox get mimetype for valid extension (.jpg)
47+
*/
48+
public function testGetMimeTypeJPEG()
49+
{
50+
$this->assertEquals('image/jpeg', MimeTypes::fromExtension('.jpg'));
51+
}
52+
53+
/**
54+
* @return void
55+
* @testdox get Extension for valid mimetype 'image/jpeg'
56+
*/
57+
public function getGetExtensionJPEG()
58+
{
59+
$this->assertEquals('jpg', MimeTypes::getExtension('image/jpeg'));
60+
}
61+
2462
}

tools/generate.php

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#! /usr/bin/env php
21
<?php
32

43
function var_export_short($expression, $return = false): ?string
@@ -21,87 +20,102 @@ function var_export_short($expression, $return = false): ?string
2120

2221
$template_generate_date = date('j M Y, g:ia T');
2322

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';
2525

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');
2828
}
29+
$mime_types_default_text = file($fn_source_types, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
2930

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+
}
3135

32-
$source_mimes = array_merge($custom_mimes, $downloaded_mimes);
36+
$source_mimes = array_merge($mime_types_custom_text, $mime_types_default_text);
3337

34-
$lines = [];
38+
$defs = [];
3539

36-
// clear comments
3740
foreach ($source_mimes as $line) {
38-
if (strpos($line, '#') !== 0) {
41+
$line = trim(preg_replace('~\\#.*~', '', $line));
42+
43+
if (!empty($line)) {
3944
preg_match_all('/^((\w|\/|\.|-|\+)+)(\s+)([^\n]*)$/im', $line, $match);
45+
4046
$type = $match[1][0];
4147
$extensions = explode(' ', $match[4][0]);
4248

43-
if (!array_key_exists($type, $lines)) {
44-
$lines[$type] = $extensions;
49+
if (!array_key_exists($type, $defs)) {
50+
$defs[$type] = $extensions;
4551
}
4652
}
4753
}
4854

49-
$mimes = [];
55+
ksort($defs);
5056

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]);
5673
}
74+
$mapping['extensions'][$mime] = $exts[0];
5775
}
58-
$customize_json = __DIR__ . '/customize.json';
5976

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);
6279

63-
foreach ($entries as $extensions => $type) {
80+
foreach ($entries as $extensions => $mime) {
6481
$extensions = explode(' ', $extensions);
6582

6683
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];
6889
}
6990
}
7091
}
92+
ksort($mapping['mimes']);
93+
ksort($mapping['extensions']);
7194

72-
// WRITE JSON DOCUMENT
73-
// ksort($mimes);
95+
var_dump($mapping);
7496

75-
$mimetypes_json = __DIR__ . '/mimetypes.json';
97+
/**
98+
* Output
99+
*/
76100

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)
79104
. PHP_EOL
80105
. "\033[01;32mSuccessfully wrote {$mimetypes_json}\033[00m"
81106
. PHP_EOL;
82107
} else {
83108
echo "Failed to write {$mimetypes_json}. Please ensure that this file system location is writable." . PHP_EOL;
84109
}
85110

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+
*/
90114

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
101115
$content = file_get_contents(__DIR__ . '/template.txt');
102116
$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);
105119

106120
//$content = str_replace('%%array_mime_types%%', $text, $content); // преформатированный красивый вывод
107121

@@ -115,3 +129,4 @@ function var_export_short($expression, $return = false): ?string
115129
// Done.
116130
echo PHP_EOL;
117131

132+

0 commit comments

Comments
 (0)