Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
Filesystem
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
11 / 11
11
100.00% covered (success)
100.00%
1 / 1
 basePath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 absolutePath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 relativePath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 smartGlob
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 touch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 unlink
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 unlinkIfExists
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getContents
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 putContents
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 filesystem
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 kernel
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Facades;
6
7use Hyde\Foundation\HydeKernel;
8use Hyde\Framework\Concerns\Internal\ForwardsIlluminateFilesystem;
9use Illuminate\Support\Collection;
10
11use 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 */
22class 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     * Touch one or more files in the project's directory.
74     *
75     * @param  string|array  $path
76     * @return bool
77     */
78    public static function touch(string|array $path): bool
79    {
80        return self::kernel()->filesystem()->touch($path);
81    }
82
83    /**
84     * Unlink one or more files in the project's directory.
85     *
86     * @param  string|array  $path
87     * @return bool
88     */
89    public static function unlink(string|array $path): bool
90    {
91        return self::kernel()->filesystem()->unlink($path);
92    }
93
94    /**
95     * Unlink a file in the project's directory, but only if it exists.
96     *
97     * @param  string  $path
98     * @return bool True if the file was unlinked, false if it did not exist or failed to unlink.
99     */
100    public static function unlinkIfExists(string $path): bool
101    {
102        return self::kernel()->filesystem()->unlinkIfExists($path);
103    }
104
105    /**
106     * Get the contents of a file.
107     *
108     * @param  string  $path
109     * @param  bool  $lock
110     * @return string
111     *
112     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
113     */
114    public static function getContents(string $path, bool $lock = false): string
115    {
116        return self::get(...func_get_args());
117    }
118
119    /**
120     * Write the contents of a file.
121     *
122     * @param  string  $path
123     * @param  string  $contents
124     * @param  bool  $lock
125     * @return int|bool
126     */
127    public static function putContents(string $path, string $contents, bool $lock = false): bool|int
128    {
129        return self::put(...func_get_args());
130    }
131
132    protected static function filesystem(): \Illuminate\Filesystem\Filesystem
133    {
134        return app(\Illuminate\Filesystem\Filesystem::class);
135    }
136
137    protected static function kernel(): HydeKernel
138    {
139        return HydeKernel::getInstance();
140    }
141}