Util::guessMimeType()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 5
nc 2
nop 2
crap 3
1
<?php
2
3
namespace League\Flysystem;
4
5
use League\Flysystem\Util\MimeType;
6
use LogicException;
7
8
class Util
9
{
10
    /**
11
     * Get normalized pathinfo.
12
     *
13
     * @param string $path
14
     *
15
     * @return array pathinfo
16
     */
17 12
    public static function pathinfo($path)
18
    {
19 12
        $pathinfo = pathinfo($path) + compact('path');
20 12
        $pathinfo['dirname'] = array_key_exists('dirname', $pathinfo)
21 12
            ? static::normalizeDirname($pathinfo['dirname']) : '';
22
23 12
        return $pathinfo;
24
    }
25
26
    /**
27
     * Normalize a dirname return value.
28
     *
29
     * @param string $dirname
30
     *
31
     * @return string normalized dirname
32
     */
33 36
    public static function normalizeDirname($dirname)
34
    {
35 36
        return $dirname === '.' ? '' : $dirname;
36
    }
37
38
    /**
39
     * Get a normalized dirname from a path.
40
     *
41
     * @param string $path
42
     *
43
     * @return string dirname
44
     */
45 33
    public static function dirname($path)
46
    {
47 33
        return static::normalizeDirname(dirname($path));
48
    }
49
50
    /**
51
     * Map result arrays.
52
     *
53
     * @param array $object
54
     * @param array $map
55
     *
56
     * @return array mapped result
57
     */
58 6
    public static function map(array $object, array $map)
59
    {
60 6
        $result = array();
61
62 6
        foreach ($map as $from => $to) {
63 6
            if ( ! isset($object[$from])) {
64 3
                continue;
65
            }
66
67 6
            $result[$to] = $object[$from];
68 6
        }
69
70 6
        return $result;
71
    }
72
73
    /**
74
     * Normalize path.
75
     *
76
     * @param string $path
77
     *
78
     * @throws LogicException
79
     *
80
     * @return string
81
     */
82 252
    public static function normalizePath($path)
83
    {
84
        // Remove any kind of funky unicode whitespace
85 252
        $normalized = preg_replace('#\p{C}+|^\./#u', '', $path);
86 252
        $normalized = static::normalizeRelativePath($normalized);
87
88 252
        if (preg_match('#/\.{2}|^\.{2}/|^\.{2}$#', $normalized)) {
89 9
            throw new LogicException(
90 9
                'Path is outside of the defined root, path: [' . $path . '], resolved: [' . $normalized . ']'
91 9
            );
92
        }
93
94 243
        $normalized = preg_replace('#\\\{2,}#', '\\', trim($normalized, '\\'));
95 243
        $normalized = preg_replace('#/{2,}#', '/', trim($normalized, '/'));
96
97 243
        return $normalized;
98
    }
99
100
    /**
101
     * Normalize relative directories in a path.
102
     *
103
     * @param string $path
104
     *
105
     * @return string
106
     */
107 252
    public static function normalizeRelativePath($path)
108
    {
109
        // Path remove self referring paths ("/./").
110 252
        $path = preg_replace('#/\.(?=/)|^\./|(/|^)\./?$#', '', $path);
111
112
        // Regex for resolving relative paths
113 252
        $regex = '#/*[^/\.]+/\.\.#Uu';
114
115 252
        while (preg_match($regex, $path)) {
116 15
            $path = preg_replace($regex, '', $path);
117 15
        }
118
119 252
        return $path;
120
    }
121
122
    /**
123
     * Normalize prefix.
124
     *
125
     * @param string $prefix
126
     * @param string $separator
127
     *
128
     * @return string normalized path
129
     */
130 3
    public static function normalizePrefix($prefix, $separator)
131
    {
132 3
        return rtrim($prefix, $separator) . $separator;
133
    }
134
135
    /**
136
     * Get content size.
137
     *
138
     * @param string $contents
139
     *
140
     * @return int content size
141
     */
142 3
    public static function contentSize($contents)
143
    {
144 3
        return defined('MB_OVERLOAD_STRING') ? mb_strlen($contents, '8bit') : strlen($contents);
145
    }
146
147
    /**
148
     * Guess MIME Type based on the path of the file and it's content.
149
     *
150
     * @param string $path
151
     * @param string|resource $content
152
     *
153
     * @return string|null MIME Type or NULL if no extension detected
154
     */
155 27
    public static function guessMimeType($path, $content)
156
    {
157 27
        $mimeType = MimeType::detectByContent($content);
158
159 27
        if ( ! (empty($mimeType) || in_array($mimeType, array('application/x-empty', 'text/plain', 'text/x-asm')))) {
160 3
            return $mimeType;
161
        }
162
163 24
        return MimeType::detectByFilename($path);
164
    }
165
166
    /**
167
     * Emulate directories.
168
     *
169
     * @param array $listing
170
     *
171
     * @return array listing with emulated directories
172
     */
173 3
    public static function emulateDirectories(array $listing)
174
    {
175 3
        $directories = array();
176 3
        $listedDirectories = array();
177
178 3
        foreach ($listing as $object) {
179 3
            list($directories, $listedDirectories) = static::emulateObjectDirectories(
180 3
                $object,
181 3
                $directories,
182
                $listedDirectories
183 3
            );
184 3
        }
185
186 3
        $directories = array_diff(array_unique($directories), array_unique($listedDirectories));
187
188 3
        foreach ($directories as $directory) {
189 3
            $listing[] = static::pathinfo($directory) + array('type' => 'dir');
190 3
        }
191
192 3
        return $listing;
193
    }
194
195
    /**
196
     * Ensure a Config instance.
197
     *
198
     * @param null|array|Config $config
199
     *
200
     * @return Config config instance
201
     *
202
     * @throw  LogicException
203
     */
204 6
    public static function ensureConfig($config)
205
    {
206 6
        if ($config === null) {
207 3
            return new Config();
208
        }
209
210 6
        if ($config instanceof Config) {
211 3
            return $config;
212
        }
213
214 6
        if (is_array($config)) {
215 3
            return new Config($config);
216
        }
217
218 3
        throw new LogicException('A config should either be an array or a Flysystem\Config object.');
219
    }
220
221
    /**
222
     * Rewind a stream.
223
     *
224
     * @param resource $resource
225
     */
226 36
    public static function rewindStream($resource)
227
    {
228 36
        if (ftell($resource) !== 0 && static::isSeekableStream($resource)) {
229 18
            rewind($resource);
230 18
        }
231 36
    }
232
233 18
    public static function isSeekableStream($resource)
234
    {
235 18
        $metadata = stream_get_meta_data($resource);
236
237 18
        return $metadata['seekable'];
238
    }
239
240
    /**
241
     * Get the size of a stream.
242
     *
243
     * @param resource $resource
244
     *
245
     * @return int stream size
246
     */
247 3
    public static function getStreamSize($resource)
248
    {
249 3
        $stat = fstat($resource);
250
251 3
        return $stat['size'];
252
    }
253
254
    /**
255
     * Emulate the directories of a single object.
256
     *
257
     * @param array $object
258
     * @param array $directories
259
     * @param array $listedDirectories
260
     *
261
     * @return array
262
     */
263 3
    protected static function emulateObjectDirectories(array $object, array $directories, array $listedDirectories)
264
    {
265 3
        if ($object['type'] === 'dir') {
266 3
            $listedDirectories[] = $object['path'];
267 3
        }
268
269 3
        if (empty($object['dirname'])) {
270 3
            return array($directories, $listedDirectories);
271
        }
272
273 3
        $parent = $object['dirname'];
274
275 3
        while ( ! empty($parent) && ! in_array($parent, $directories)) {
276 3
            $directories[] = $parent;
277 3
            $parent = static::dirname($parent);
278 3
        }
279
280 3
        if (isset($object['type']) && $object['type'] === 'dir') {
281 3
            $listedDirectories[] = $object['path'];
282
283 3
            return array($directories, $listedDirectories);
284
        }
285
286 3
        return array($directories, $listedDirectories);
287
    }
288
}
289