Passed
Pull Request — master (#2)
by mon
03:19
created

MapUpdater::loadMapFromUrl()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 24
cp 0
rs 9.2248
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 30
1
<?php
2
3
namespace FileEye\MimeMap;
4
5
use SebastianBergmann\Comparator\Factory;
6
use SebastianBergmann\Comparator\ComparisonFailure;
7
use SebastianBergmann\Diff\Differ;
8
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
9
10
/**
11
 * Compiles the MIME type to file extension map.
12
 */
13
class MapUpdater
14
{
15
    /**
16
     * Default URL where to read the specification from.
17
     *
18
     * The apache httpd project contains the most complete list of file
19
     * extension to mime type mapping on the planet. We use it to update our
20
     * own list.
21
     */
22
    const DEFAULT_URL = 'http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=co';
23
24
    /**
25
     * The defualt file where to write the map as PHP code.
26
     */
27
    const DEFAULT_CODE_FILE_NAME = 'TypeExtensionMap.php';
28
29
    public static function getDefaultCodeFilePath()
30
    {
31
        return dirname(__FILE__) . '/' . static::DEFAULT_CODE_FILE_NAME;
32
    }
33
34
    public function loadMapFromUrl($url = MapUpdater::DEFAULT_URL)
35
    {
36
        $map = [];
37
        $lines = file($url);
38
        if ($lines === false) {
39
            throw new \RuntimeException('Error loading URL: ' . $url);
40
        }
41
        foreach ($lines as $line) {
42
            if ($line{0} == '#') {
43
                continue;
44
            }
45
            $line = preg_replace("#\\s+#", ' ', trim($line));
46
            $parts = explode(' ', $line);
47
            $type = array_shift($parts);
48
            $extensions = $parts;
49
            foreach ($extensions as $ext) {
50
                $map['types'][$type][] = $ext;
51
                $map['extensions'][$ext][] = $type;
52
            }
53
        }
54
        ksort($map['types']);
55
        ksort($map['extensions']);
56
        return $map;
57
    }
58
59
    public function compareMaps($current_map, $new_map)
60
    {
61
        $factory = new Factory;
62
        $comparator = $factory->getComparatorFor($current_map['types'], $new_map['types']);
63
        try {
64
            $comparator->assertEquals($current_map['types'], $new_map['types']);
65
            return true;
66
        } catch (ComparisonFailure $failure) {
67
            $current_map_string = var_export($current_map['types'], true);
68
            $new_map_string = var_export($new_map['types'], true);
69
            $differ = new Differ(new UnifiedDiffOutputBuilder("--- Removed\n+++ Added\n"));
70
            throw new \RuntimeException($differ->diff($current_map_string, $new_map_string));
71
        }
72
    }
73
74
    public function writeMapToCodeFile($map, $file)
75
    {
76
        $new = preg_replace(
77
            '#protected static \$extensionToType = (.+?);#s',
78
            "protected static \$extensionToType = " . var_export($map, true) . ";",
79
            file_get_contents($file)
80
        );
81
        file_put_contents($file, $new);
82
    }
83
}
84