Passed
Pull Request — master (#13)
by mon
02:06
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 5
    public function __construct(AbstractMap $map)
40
    {
41 5
        $this->map = $map;
42 5
    }
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
        return $this;
71
    }
72
73
    /**
74
     * Loads a new type-to-extension map reading from a Freedesktop.org file.
75
     *
76
     * @param string $source_file
77
     *   The source file. The file must conform to the format in the
78
     *   Freedesktop.org database.
79
     *
80
     * @throws \RuntimeException if file I/O error occurs.
81
     *
82
     * @return $this
83
     */
84
    public function loadMapFromFreedesktopFile($source_file)
85
    {
86
        $xml = simplexml_load_string(file_get_contents($source_file));
87
        foreach ($xml as $node) {
88
            $exts = [];
89
            foreach ($node->glob as $glob) {
90
                $pattern = (string) $glob['pattern'];
91
                if ('*' != $pattern[0] || '.' != $pattern[1]) {
92
                    continue;
93
                }
94
                $exts[] = substr($pattern, 2);
95
            }
96
            if (empty($exts)) {
97
                continue;
98
            }
99
            $mt = (string) $node['type'];
100
            foreach ($exts as $ext) {
101
                $this->map->addMapping($mt, $ext);
102
            }
103
            //$new[$mt] = $exts;
104
/*              foreach ($node->alias as $alias) {
105
                $mt = strtolower((string) $alias['type']);
106
                $new[$mt] = array_merge($new[$mt] ?? [], $exts);
107
            }*/
108
        }
109
        return $this;
110
    }
111
112
    /**
113
     * Applies to the map an array of overrides.
114
     *
115
     * @param array $overrides
116
     *   The overrides to be applied.
117
     *
118
     * @return $this
119
     */
120 1
    public function applyOverrides(array $overrides)
121
    {
122 1
        foreach ($overrides as $command) {
123 1
            call_user_func_array([$this->map, $command[0]], $command[1]);
124
        }
125 1
        return $this;
126
    }
127
128
    /**
129
     * Updates the map at a destination PHP file.
130
     *
131
     * @return $this
132
     */
133 1
    public function writeMapToPhpClassFile($file)
134
    {
135 1
        $content = preg_replace(
136 1
            '#protected static \$map = (.+?);#s',
137 1
            "protected static \$map = " . var_export($this->map->getMapArray(), true) . ";",
138 1
            file_get_contents($file)
139
        );
140 1
        file_put_contents($file, $content);
141 1
        return $this;
142
    }
143
}
144