Test Failed
Pull Request — master (#13)
by mon
02:42
created

MapUpdater::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 1
crap 1
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
     * The AbstractMap object to update.
14
     *
15
     * @var AbstractMap
16
     */
17
    protected $map;
18
19
    /**
20
     * Returns the default file with override commands to be executed.
21
     *
22
     * The YAML file provides an array of calls to MapHandler methods to be
23
     * executed sequentially. Each entry indicates the method to be invoked and
24
     * the arguments to be passed in.
25
     *
26
     * @return string
27
     */
28 1
    public static function getDefaultMapBuildFile()
29
    {
30 1
        return __DIR__ . '/../resources/default_map_build.yml';
31
    }
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param AbstractMap $map
37
     *   The map.
38
     */
39 7
    public function __construct(AbstractMap $map)
40
    {
41 7
        $this->map = $map;
42 7
    }
43
44
    /**
45
     * Loads a new type-to-extension map reading from a file in Apache format.
46
     *
47
     * @param string $source_file
48
     *   The source file. The file must conform to the format in the Apache
49
     *   source code repository file where MIME types and file extensions are
50
     *   associated.
51
     *
52
     * @throws \RuntimeException if file I/O error occurs.
53
     *
54
     * @return $this
55
     */
56 3
    public function loadMapFromApacheFile($source_file)
57
    {
58 3
        $lines = file($source_file);
59 3
        foreach ($lines as $line) {
60 2
            if ($line{0} == '#') {
61 2
                continue;
62
            }
63 2
            $line = preg_replace("#\\s+#", ' ', trim($line));
64 2
            $parts = explode(' ', $line);
65 2
            $type = array_shift($parts);
66 2
            foreach ($parts as $extension) {
67 2
                $this->map->addMapping($type, $extension);
68
            }
69
        }
70 3
        $this->map->sort();
71 2
        return $this;
72
    }
73
74
    /**
75
     * Loads a new type-to-extension map reading from a Freedesktop.org file.
76
     *
77
     * @param string $source_file
78
     *   The source file. The file must conform to the format in the
79
     *   Freedesktop.org database.
80
     *
81
     * @throws \RuntimeException if file I/O error occurs.
82
     *
83
     * @return $this
84
     */
85 2
    public function loadMapFromFreedesktopFile($source_file)
86
    {
87 2
        $xml = simplexml_load_string(file_get_contents($source_file));
88 2
        foreach ($xml as $node) {
89 1
            $exts = [];
90 1
            foreach ($node->glob as $glob) {
91 1
                $pattern = (string) $glob['pattern'];
92 1
                if ('*' != $pattern[0] || '.' != $pattern[1]) {
93 1
                    continue;
94
                }
95 1
                $exts[] = substr($pattern, 2);
96
            }
97 1
            if (empty($exts)) {
98 1
                continue;
99
            }
100 1
            $mt = (string) $node['type'];
101 1
            foreach ($exts as $ext) {
102 1
                $this->map->addMapping($mt, $ext);
103
            }
104
        }
105 2
        $this->map->sort();
106 1
        return $this;
107
    }
108
109
    /**
110
     * Applies to the map an array of overrides.
111
     *
112
     * @param array $overrides
113
     *   The overrides to be applied.
114
     *
115
     * @return $this
116
     */
117 1
    public function applyOverrides(array $overrides)
118
    {
119 1
        foreach ($overrides as $command) {
120 1
            call_user_func_array([$this->map, $command[0]], $command[1]);
121
        }
122 1
        return $this;
123
    }
124
125
    /**
126
     * Updates the map at a destination PHP file.
127
     *
128
     * @return $this
129
     */
130 1
    public function writeMapToPhpClassFile($file)
131
    {
132 1
        $content = preg_replace(
133 1
            '#protected static \$map = (.+?);#s',
134 1
            "protected static \$map = " . var_export($this->map->getMapArray(), true) . ";",
135 1
            file_get_contents($file)
136
        );
137 1
        file_put_contents($file, $content);
138 1
        return $this;
139
    }
140
}
141