Passed
Push — master ( ce5bc3...2aaa83 )
by Caen
03:07 queued 11s
created

Filesystem::relativePath()   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 Hyde\Support\Contracts\FilesystemContract;
10
use Illuminate\Support\Collection;
11
use Illuminate\Support\Facades\File;
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\Filesystem
20
 * @see \Illuminate\Filesystem\Filesystem
21
 * @see \Hyde\Framework\Testing\Feature\FilesystemFacadeTest
22
 */
23
class Filesystem implements FilesystemContract
24
{
25
    use ForwardsIlluminateFilesystem;
26
27
    /**
28
     * Get the base path of the HydePHP project.
29
     *
30
     * @return string
31
     */
32
    public static function basePath(): string
33
    {
34
        return self::kernel()->path();
35
    }
36
37
    /**
38
     * Format the given project path to be absolute. Already absolute paths are normalized.
39
     *
40
     * @param  string  $path
41
     * @return string
42
     */
43
    public static function absolutePath(string $path): string
44
    {
45
        return self::kernel()->pathToAbsolute(self::relativePath($path));
46
    }
47
48
    /**
49
     * Remove the absolute path from the given project path so that it becomes relative.
50
     *
51
     * @param  string  $path
52
     * @return string
53
     */
54
    public static function relativePath(string $path): string
55
    {
56
        return self::kernel()->pathToRelative($path);
57
    }
58
59
    /**
60
     * A smarter glob function that will run the specified glob pattern a bit more intelligently.
61
     * While this method will use the absolute path when interacting with the filesystem,
62
     * the returned collection will only contain relative paths.
63
     *
64
     * @param  string  $pattern
65
     * @param  int  $flags
66
     * @return \Illuminate\Support\Collection<string>
67
     */
68
    public static function smartGlob(string $pattern, int $flags = 0): Collection
69
    {
70
        return self::kernel()->filesystem()->smartGlob($pattern, $flags);
71
    }
72
73
    /**
74
     * Touch one or more files in the project's directory.
75
     *
76
     * @param  string|array  $path
77
     * @return bool
78
     */
79
    public static function touch(string|array $path): bool
80
    {
81
        return self::kernel()->filesystem()->touch($path);
82
    }
83
84
    /**
85
     * Unlink one or more files in the project's directory.
86
     *
87
     * @param  string|array  $path
88
     * @return bool
89
     */
90
    public static function unlink(string|array $path): bool
91
    {
92
        return self::kernel()->filesystem()->unlink($path);
93
    }
94
95
    /**
96
     * Unlink a file in the project's directory, but only if it exists.
97
     *
98
     * @param  string  $path
99
     * @return bool True if the file was unlinked, false if it did not exist or failed to unlink.
100
     */
101
    public static function unlinkIfExists(string $path): bool
102
    {
103
        return self::kernel()->filesystem()->unlinkIfExists($path);
104
    }
105
106
    /**
107
     * Get the contents of a file.
108
     *
109
     * @param  string  $path
110
     * @param  bool  $lock
111
     * @return string
112
     *
113
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
114
     */
115
    public static function getContents(string $path, bool $lock = false): string
116
    {
117
        return self::get($path, $lock);
118
    }
119
120
    /**
121
     * Write the contents of a file.
122
     *
123
     * @param  string  $path
124
     * @param  string  $contents
125
     * @param  bool  $lock
126
     * @return int|bool
127
     */
128
    public static function putContents(string $path, string $contents, bool $lock = false): bool|int
129
    {
130
        return self::put($path, $contents, $lock);
131
    }
132
133
    protected static function qualifyPossiblePathArray(array|string $paths): array|string
134
    {
135
        return self::kernel()->filesystem()->qualifyPossiblePathArray($paths);
136
    }
137
138
    protected static function filesystem(): \Illuminate\Filesystem\Filesystem
139
    {
140
        return File::getFacadeRoot();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Illuminate\Suppor...s\File::getFacadeRoot() could return the type null which is incompatible with the type-hinted return Illuminate\Filesystem\Filesystem. Consider adding an additional type-check to rule them out.
Loading history...
141
    }
142
143
    protected static function kernel(): HydeKernel
144
    {
145
        return HydeKernel::getInstance();
146
    }
147
}
148