Passed
Push — master ( 25efb9...c70e91 )
by Caen
04:09 queued 12s
created

Filesystem::getMarkdownPostPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation\Kernel;
6
7
use Hyde\Hyde;
8
use Hyde\Foundation\HydeKernel;
9
use Hyde\Foundation\PharSupport;
10
use Illuminate\Support\Collection;
11
use function Hyde\normalize_slashes;
12
use function Hyde\path_join;
13
use function file_exists;
14
use function str_replace;
15
use function array_map;
16
use function is_string;
17
use function is_array;
18
use function collect;
19
use function unslash;
20
use function unlink;
21
use function touch;
22
23
/**
24
 * File helper methods, bound to the HydeKernel instance, and is an integral part of the framework.
25
 *
26
 * All paths arguments are relative to the root of the application,
27
 * and will be automatically resolved to absolute paths.
28
 *
29
 * @see \Hyde\Framework\Testing\Feature\Foundation\FilesystemTest
30
 */
31
class Filesystem
32
{
33
    protected HydeKernel $kernel;
34
35
    public function __construct(HydeKernel $kernel)
36
    {
37
        $this->kernel = $kernel;
38
    }
39
40
    public function getBasePath(): string
41
    {
42
        return $this->kernel->getBasePath();
43
    }
44
45
    /**
46
     * Get an absolute file path from a supplied relative path.
47
     *
48
     * The function returns the fully qualified path to your site's root directory.
49
     *
50
     * You may also use the function to generate a fully qualified path to a given file
51
     * relative to the project root directory when supplying the path argument.
52
     */
53
    public function path(string $path = ''): string
54
    {
55
        if (empty($path)) {
56
            return $this->getBasePath();
57
        }
58
59
        $path = unslash($this->pathToRelative($path));
60
61
        return path_join($this->getBasePath(), $path);
62
    }
63
64
    /**
65
     * Get an absolute file path from a supplied relative path.
66
     *
67
     * Input types are matched, meaning that if the input is a string so will the output be.
68
     */
69
    public function pathToAbsolute(string|array $path): string|array
70
    {
71
        if (is_array($path)) {
0 ignored issues
show
introduced by
The condition is_array($path) is always true.
Loading history...
72
            return array_map(fn (string $path): string => $this->pathToAbsolute($path), $path);
73
        }
74
75
        return $this->path($path);
76
    }
77
78
    /**
79
     * Decode an absolute path created with a Hyde::path() helper into its relative counterpart.
80
     */
81
    public function pathToRelative(string $path): string
82
    {
83
        return normalize_slashes(str_starts_with($path, $this->path())
84
            ? unslash(str_replace($this->path(), '', $path))
85
            : $path);
86
    }
87
88
    /**
89
     * Get the absolute path to the media source directory, or a file within it.
90
     */
91
    public function mediaPath(string $path = ''): string
92
    {
93
        if (empty($path)) {
94
            return $this->path(Hyde::getMediaDirectory());
95
        }
96
97
        $path = unslash($path);
98
99
        return $this->path(Hyde::getMediaDirectory()."/$path");
100
    }
101
102
    /**
103
     * Get the absolute path to the compiled site directory, or a file within it.
104
     */
105
    public function sitePath(string $path = ''): string
106
    {
107
        if (empty($path)) {
108
            return $this->path(Hyde::getOutputDirectory());
109
        }
110
111
        $path = unslash($path);
112
113
        return $this->path(Hyde::getOutputDirectory()."/$path");
114
    }
115
116
    /**
117
     * Get the absolute path to the compiled site's media directory, or a file within it.
118
     */
119
    public function siteMediaPath(string $path = ''): string
120
    {
121
        if (empty($path)) {
122
            return $this->sitePath(Hyde::getMediaOutputDirectory());
123
        }
124
125
        $path = unslash($path);
126
127
        return $this->sitePath(Hyde::getMediaOutputDirectory()."/$path");
128
    }
129
130
    /**
131
     * Works similarly to the path() function, but returns a file in the Framework package.
132
     *
133
     * @internal This is not intended to be used outside the HydePHP framework.
134
     */
135
    public function vendorPath(string $path = '', string $package = 'framework'): string
136
    {
137
        if (PharSupport::running() && ! PharSupport::hasVendorDirectory()) {
138
            return PharSupport::vendorPath($path, $package);
139
        }
140
141
        return $this->path("vendor/hyde/$package/".unslash($path));
142
    }
143
144
    /**
145
     * Touch one or more files in the project's directory.
146
     */
147
    public function touch(string|array $path): bool
148
    {
149
        if (is_string($path)) {
0 ignored issues
show
introduced by
The condition is_string($path) is always false.
Loading history...
150
            return touch($this->path($path));
151
        }
152
153
        foreach ($path as $p) {
154
            touch($this->path($p));
155
        }
156
157
        return true;
158
    }
159
160
    /**
161
     * Unlink one or more files in the project's directory.
162
     */
163
    public function unlink(string|array $path): bool
164
    {
165
        if (is_string($path)) {
0 ignored issues
show
introduced by
The condition is_string($path) is always false.
Loading history...
166
            return unlink($this->path($path));
167
        }
168
169
        foreach ($path as $p) {
170
            unlink($this->path($p));
171
        }
172
173
        return true;
174
    }
175
176
    /**
177
     * Unlink a file in the project's directory, but only if it exists.
178
     */
179
    public function unlinkIfExists(string $path): bool
180
    {
181
        if (file_exists($this->path($path))) {
182
            return unlink($this->path($path));
183
        }
184
185
        return false;
186
    }
187
188
    public function smartGlob(string $pattern, int $flags = 0): Collection
189
    {
190
        return collect(\Hyde\Facades\Filesystem::glob($pattern, $flags))
0 ignored issues
show
Bug introduced by
Hyde\Facades\Filesystem::glob($pattern, $flags) of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

190
        return collect(/** @scrutinizer ignore-type */ \Hyde\Facades\Filesystem::glob($pattern, $flags))
Loading history...
191
            ->map(fn (string $path): string => $this->pathToRelative($path));
192
    }
193
}
194