Passed
Pull Request — master (#7)
by mon
02:04
created

MapUpdater::getDefaultOverrideFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace FileEye\MimeMap;
4
5
use SebastianBergmann\Comparator\ComparisonFailure;
6
use SebastianBergmann\Comparator\Factory;
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_SOURCE_FILE = 'http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=co';
23
24
    /**
25
     * Returns the default file with override commands to be executed.
26
     *
27
     * The YAML file provides an array of calls to MapHandler methods to be
28
     * executed sequentially. Each entry indicates the method to be invoked and
29
     * the arguments to be passed in.
30
     *
31
     * @return string
32
     */
33 1
    public static function getDefaultOverrideFile()
34
    {
35 1
        return __DIR__ . '/../resources/overrides.yml';
36
    }
37
38
    /**
39
     * Creates a new type-to-extension map reading from a file.
40
     *
41
     * @param string $source_file
42
     *   (Optional) the source file. Defaults to the Apache source bind_textdomain_codeset
43
     *   repository file where MIME types and file extensions are associated.
44
     *
45
     * @throws \RuntimeException if file I/O error occurs.
46
     *
47
     * @return MapHandler
48
     *   The map handler with the new map.
49
     */
50 5
    public function createMapFromSourceFile($source_file = MapUpdater::DEFAULT_SOURCE_FILE)
51
    {
52 5
        $map = new MapHandler([]);
53 5
        $lines = file($source_file);
54 5
        foreach ($lines as $line) {
55 4
            if ($line{0} == '#') {
56 4
                continue;
57
            }
58 4
            $line = preg_replace("#\\s+#", ' ', trim($line));
59 4
            $parts = explode(' ', $line);
60 4
            $type = array_shift($parts);
61 4
            foreach ($parts as $extension) {
62 4
                $map->addMapping($type, $extension);
63
            }
64
        }
65 5
        $map_array = $map->get();
66 5
        if (empty($map_array)) {
67 1
            throw new \RuntimeException('No data found in file ' . $source_file);
68
        }
69 4
        return $map;
70
    }
71
72
    /**
73
     * Compares two type-to-extension maps by section.
74
     *
75
     * @param MapHandler $old_map
76
     *   The first map to compare.
77
     * @param MapHandler $new_map
78
     *   The second map to compare.
79
     * @param string $section
80
     *   The first-level array key to compare: 'types' or 'extensions'.
81
     *
82
     * @throws \RuntimeException with diff details if the maps differ.
83
     *
84
     * @return bool
85
     *   True if the maps are equal.
86
     */
87 2
    public function compareMaps(MapHandler $old_map, MapHandler $new_map, $section)
88
    {
89 2
        $old_map->sort();
90 2
        $new_map->sort();
91 2
        $old = $old_map->get();
92 2
        $new = $new_map->get();
93
94 2
        $factory = new Factory;
95 2
        $comparator = $factory->getComparatorFor($old[$section], $new[$section]);
96
        try {
97 2
            $comparator->assertEquals($old[$section], $new[$section]);
98 1
            return true;
99 1
        } catch (ComparisonFailure $failure) {
100 1
            $old_string = var_export($old[$section], true);
101 1
            $new_string = var_export($new[$section], true);
102 1
            if (PHP_VERSION_ID >= 70000) {
103 1
                $differ = new Differ(new UnifiedDiffOutputBuilder("--- Removed\n+++ Added\n"));
104 1
                throw new \RuntimeException($differ->diff($old_string, $new_string));
105
            } else {
106
                throw new \RuntimeException(' ');
107
            }
108
        }
109
    }
110
111
    /**
112
     * Applies to the map an array of overrides.
113
     *
114
     * @param MapHandler $map
115
     *   The map.
116
     * @param array $overrides
117
     *   The overrides to be applied.
118
     *
119
     * @return void
120
     */
121 1
    public function applyOverrides(MapHandler $map, array $overrides)
122
    {
123 1
        foreach ($overrides as $command) {
124 1
            call_user_func_array([$map, $command[0]], $command[1]);
125
        }
126 1
    }
127
128
    /**
129
     * Updates the map at a destination PHP file.
130
     *
131
     * @param MapHandler $map
132
     *   The map.
133
     * @param string $output_file
134
     *   The destination PHP file.
135
     *
136
     * @return void
137
     */
138 1
    public function writeMapToCodeFile(MapHandler $map, $output_file)
139
    {
140 1
        $content = preg_replace(
141 1
            '#public static \$map = (.+?);#s',
142 1
            "public static \$map = " . var_export($map->get(), true) . ";",
143 1
            file_get_contents($output_file)
144
        );
145 1
        file_put_contents($output_file, $content);
146 1
    }
147
}
148