Passed
Push — master ( ad6b2a...b41ca0 )
by Caen
07:45 queued 14s
created

Filesystem::unlink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Facades;
6
7
use Hyde\Foundation\HydeKernel;
8
use Hyde\Framework\Concerns\Internal\ForwardsIlluminateFilesystem;
9
use Illuminate\Support\Collection;
10
11
use function app;
12
13
/**
14
 * Proxies the Laravel File facade with extra features and helpers tailored for HydePHP.
15
 *
16
 * For maximum compatability and interoperability, all path references in HydePHP are relative to the root of the project.
17
 * The helpers here will then prepend the project root to the path before actually interacting with the filesystem.
18
 *
19
 * @see \Hyde\Foundation\Kernel\Filesystem
20
 * @see \Illuminate\Filesystem\Filesystem
21
 */
22
class Filesystem
23
{
24
    use ForwardsIlluminateFilesystem;
25
26
    /**
27
     * Get the base path of the HydePHP project.
28
     *
29
     * @return string
30
     */
31
    public static function basePath(): string
32
    {
33
        return self::kernel()->path();
34
    }
35
36
    /**
37
     * Format the given project path to be absolute. Already absolute paths are normalized.
38
     *
39
     * @param  string  $path
40
     * @return string
41
     */
42
    public static function absolutePath(string $path): string
43
    {
44
        return self::kernel()->pathToAbsolute(self::relativePath($path));
45
    }
46
47
    /**
48
     * Remove the absolute path from the given project path so that it becomes relative.
49
     *
50
     * @param  string  $path
51
     * @return string
52
     */
53
    public static function relativePath(string $path): string
54
    {
55
        return self::kernel()->pathToRelative($path);
56
    }
57
58
    /**
59
     * A smarter glob function that will run the specified glob pattern a bit more intelligently.
60
     * While this method will use the absolute path when interacting with the filesystem,
61
     * the returned collection will only contain relative paths.
62
     *
63
     * @param  string  $pattern
64
     * @param  int  $flags
65
     * @return \Illuminate\Support\Collection<int, string>
66
     */
67
    public static function smartGlob(string $pattern, int $flags = 0): Collection
68
    {
69
        return self::kernel()->filesystem()->smartGlob($pattern, $flags);
70
    }
71
72
    /**
73
     * Find files in the project's directory, with optional filtering by extension and recursion.
74
     *
75
     * The returned collection will be a list of paths relative to the project root.
76
     *
77
     * @param  string  $directory
78
     * @param  string|array<string>|false  $matchExtensions  The file extension(s) to match, or false to match all files.
79
     * @param  bool  $recursive  Whether to search recursively or not.
80
     * @return \Illuminate\Support\Collection<int, string>
81
     */
82
    public static function findFiles(string $directory, string|array|false $matchExtensions = false, bool $recursive = false): Collection
83
    {
84
        return self::kernel()->filesystem()->findFiles($directory, $matchExtensions, $recursive);
85
    }
86
87
    /**
88
     * Touch one or more files in the project's directory.
89
     *
90
     * @param  string|array  $path
91
     * @return bool
92
     */
93
    public static function touch(string|array $path): bool
94
    {
95
        return self::kernel()->filesystem()->touch($path);
96
    }
97
98
    /**
99
     * Unlink one or more files in the project's directory.
100
     *
101
     * @param  string|array  $path
102
     * @return bool
103
     */
104
    public static function unlink(string|array $path): bool
105
    {
106
        return self::kernel()->filesystem()->unlink($path);
107
    }
108
109
    /**
110
     * Unlink a file in the project's directory, but only if it exists.
111
     *
112
     * @param  string  $path
113
     * @return bool True if the file was unlinked, false if it did not exist or failed to unlink.
114
     */
115
    public static function unlinkIfExists(string $path): bool
116
    {
117
        return self::kernel()->filesystem()->unlinkIfExists($path);
118
    }
119
120
    /**
121
     * Get the contents of a file.
122
     *
123
     * @param  string  $path
124
     * @param  bool  $lock
125
     * @return string
126
     *
127
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
128
     */
129
    public static function getContents(string $path, bool $lock = false): string
130
    {
131
        return self::get(...func_get_args());
0 ignored issues
show
Bug introduced by
func_get_args() is expanded, but the parameter $path of Hyde\Facades\Filesystem::get() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

131
        return self::get(/** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
132
    }
133
134
    /**
135
     * Write the contents of a file.
136
     *
137
     * @param  string  $path
138
     * @param  string  $contents
139
     * @param  bool  $lock
140
     * @return int|bool
141
     */
142
    public static function putContents(string $path, string $contents, bool $lock = false): bool|int
143
    {
144
        return self::put(...func_get_args());
145
    }
146
147
    /**
148
     * Improved mime type detection for the given path that can be relative to the project root,
149
     * or a remote URL where this will try to guess the mime type based on the file extension.
150
     *
151
     * @param  string  $path  The path to the file, relative to the project root or a remote URL.
152
     */
153
    public static function findMimeType(string $path): string
154
    {
155
        $extension = self::extension($path);
156
157
        $lookup = [
158
            'txt' => 'text/plain',
159
            'md' => 'text/markdown',
160
            'html' => 'text/html',
161
            'css' => 'text/css',
162
            'svg' => 'image/svg+xml',
163
            'png' => 'image/png',
164
            'jpg' => 'image/jpeg',
165
            'jpeg' => 'image/jpeg',
166
            'gif' => 'image/gif',
167
            'json' => 'application/json',
168
            'js' => 'application/javascript',
169
            'xml' => 'application/xml',
170
        ];
171
172
        if (isset($lookup[$extension])) {
173
            return $lookup[$extension];
174
        }
175
176
        if (extension_loaded('fileinfo') && self::exists($path)) {
177
            return self::mimeType($path);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::mimeType($path) could return the type false which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
178
        }
179
180
        return 'text/plain';
181
    }
182
183
    protected static function filesystem(): \Illuminate\Filesystem\Filesystem
184
    {
185
        return app(\Illuminate\Filesystem\Filesystem::class);
186
    }
187
188
    protected static function kernel(): HydeKernel
189
    {
190
        return HydeKernel::getInstance();
191
    }
192
}
193