Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
MediaFile
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
9 / 9
14
100.00% covered (success)
100.00%
1 / 1
 all
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 files
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIdentifier
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getContentLength
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getMimeType
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
4
 discoverMediaAssetFiles
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getMediaAssetFiles
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getMediaGlobPattern
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Support\Filesystem;
6
7use Hyde\Hyde;
8use Hyde\Facades\Config;
9use Hyde\Framework\Exceptions\FileNotFoundException;
10use Illuminate\Support\Str;
11
12use function extension_loaded;
13use function file_exists;
14use function array_merge;
15use function array_keys;
16use function filesize;
17use function implode;
18use function pathinfo;
19use function collect;
20use function is_file;
21use function sprintf;
22use function glob;
23
24/**
25 * File abstraction for a project media file.
26 */
27class MediaFile extends ProjectFile
28{
29    /** @var array<string> The default extensions for media types */
30    final public const EXTENSIONS = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'ico', 'css', 'js'];
31
32    /** @return array<string, \Hyde\Support\Filesystem\MediaFile> The array keys are the filenames relative to the _media/ directory */
33    public static function all(): array
34    {
35        return static::discoverMediaAssetFiles();
36    }
37
38    /** @return array<string> Array of filenames relative to the _media/ directory */
39    public static function files(): array
40    {
41        return array_keys(static::all());
42    }
43
44    public function getIdentifier(): string
45    {
46        return Str::after($this->getPath(), Hyde::getMediaDirectory().'/');
47    }
48
49    public function toArray(): array
50    {
51        return array_merge(parent::toArray(), [
52            'length' => $this->getContentLength(),
53            'mimeType' => $this->getMimeType(),
54        ]);
55    }
56
57    public function getContentLength(): int
58    {
59        if (! is_file($this->getAbsolutePath())) {
60            throw new FileNotFoundException($this->path);
61        }
62
63        return filesize($this->getAbsolutePath());
64    }
65
66    public function getMimeType(): string
67    {
68        $extension = pathinfo($this->getAbsolutePath(), PATHINFO_EXTENSION);
69
70        // See if we can find a mime type for the extension instead of
71        // having to rely on a PHP extension and filesystem lookups.
72        $lookup = [
73            'txt' => 'text/plain',
74            'md' => 'text/markdown',
75            'html' => 'text/html',
76            'css' => 'text/css',
77            'svg' => 'image/svg+xml',
78            'png' => 'image/png',
79            'jpg' => 'image/jpeg',
80            'jpeg' => 'image/jpeg',
81            'gif' => 'image/gif',
82            'json' => 'application/json',
83            'js' => 'application/javascript',
84            'xml' => 'application/xml',
85        ];
86
87        if (isset($lookup[$extension])) {
88            return $lookup[$extension];
89        }
90
91        if (extension_loaded('fileinfo') && file_exists($this->getAbsolutePath())) {
92            return mime_content_type($this->getAbsolutePath());
93        }
94
95        return 'text/plain';
96    }
97
98    protected static function discoverMediaAssetFiles(): array
99    {
100        return collect(static::getMediaAssetFiles())->mapWithKeys(function (string $path): array {
101            $file = static::make($path);
102
103            return [$file->getIdentifier() => $file];
104        })->all();
105    }
106
107    protected static function getMediaAssetFiles(): array
108    {
109        return glob(Hyde::path(static::getMediaGlobPattern()), GLOB_BRACE) ?: [];
110    }
111
112    protected static function getMediaGlobPattern(): string
113    {
114        return sprintf(Hyde::getMediaDirectory().'/{*,**/*,**/*/*}.{%s}', implode(',',
115            Config::getArray('hyde.media_extensions', self::EXTENSIONS)
116        ));
117    }
118}