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

Filesystem::getMarkdownPagePath()   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\Foundation;
6
7
use function collect;
8
use function copy;
9
use Hyde\Facades\Site;
10
use Hyde\Framework\Services\DiscoveryService;
11
use Hyde\Hyde;
12
use Hyde\Pages\BladePage;
13
use Hyde\Pages\DocumentationPage;
14
use Hyde\Pages\MarkdownPage;
15
use Hyde\Pages\MarkdownPost;
16
use Illuminate\Support\Collection;
17
use function is_string;
18
use function str_replace;
19
use function touch;
20
use function unlink;
21
use function unslash;
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
     * @param  string  $path
54
     * @return string
55
     */
56
    public function path(string $path = ''): string
57
    {
58
        if (empty($path)) {
59
            return $this->getBasePath();
60
        }
61
62
        $path = unslash($path);
63
64
        return $this->implode($this->getBasePath(), $path);
65
    }
66
67
    /**
68
     * Get an absolute file path from a supplied relative path.
69
     */
70
    public function pathToAbsolute(string $path): string
71
    {
72
        return $this->path($path);
73
    }
74
75
    /**
76
     * Decode an absolute path created with a Hyde::path() helper into its relative counterpart.
77
     */
78
    public function pathToRelative(string $path): string
79
    {
80
        return str_starts_with($path, $this->path())
81
            ? unslash(str_replace($this->path(), '', $path))
82
            : $path;
83
    }
84
85
    /**
86
     * Get the absolute path to the compiled site directory, or a file within it.
87
     */
88
    public function sitePath(string $path = ''): string
89
    {
90
        if (empty($path)) {
91
            return Hyde::path(Site::$outputPath);
92
        }
93
94
        $path = unslash($path);
95
96
        return Hyde::path(Site::$outputPath.DIRECTORY_SEPARATOR.$path);
97
    }
98
99
    /**
100
     * Works similarly to the path() function, but returns a file in the Framework package.
101
     *
102
     * @param  string  $path
103
     * @return string
104
     */
105
    public function vendorPath(string $path = ''): string
106
    {
107
        return $this->path('vendor/hyde/framework/'.unslash($path));
108
    }
109
110
    /**
111
     * Wrapper for the copy function, but using project relative paths.
112
     */
113
    public function copy(string $from, string $to): bool
114
    {
115
        return copy($this->path($from), $this->path($to));
116
    }
117
118
    /**
119
     * Touch one or more files in the project's directory.
120
     */
121
    public function touch(string|array $path): bool
122
    {
123
        if (is_string($path)) {
0 ignored issues
show
introduced by
The condition is_string($path) is always false.
Loading history...
124
            return touch($this->path($path));
125
        }
126
127
        foreach ($path as $p) {
128
            touch($this->path($p));
129
        }
130
131
        return true;
132
    }
133
134
    /**
135
     * Unlink one or more files in the project's directory.
136
     */
137
    public function unlink(string|array $path): bool
138
    {
139
        if (is_string($path)) {
0 ignored issues
show
introduced by
The condition is_string($path) is always false.
Loading history...
140
            return unlink($this->path($path));
141
        }
142
143
        foreach ($path as $p) {
144
            unlink($this->path($p));
145
        }
146
147
        return true;
148
    }
149
150
    /**
151
     * Unlink a file in the project's directory, but only if it exists.
152
     */
153
    public function unlinkIfExists(string $path): bool
154
    {
155
        if (file_exists($this->path($path))) {
156
            return unlink($this->path($path));
157
        }
158
159
        return false;
160
    }
161
162
    /**
163
     * Fluent file helper methods.
164
     *
165
     * Provides a more fluent way of getting either the absolute path
166
     * to a model's source directory, or an absolute path to a file within it.
167
     *
168
     * These are intended to be used as a dynamic alternative to legacy code
169
     * Hyde::path('_pages/foo') becomes Hyde::getBladePagePath('foo')
170
     */
171
    public function getModelSourcePath(string $model, string $path = ''): string
172
    {
173
        if (empty($path)) {
174
            return $this->path(DiscoveryService::getModelSourceDirectory($model));
175
        }
176
177
        $path = unslash($path);
178
179
        return $this->path($this->implode(DiscoveryService::getModelSourceDirectory($model), $path));
180
    }
181
182
    public function getBladePagePath(string $path = ''): string
183
    {
184
        return $this->getModelSourcePath(BladePage::class, $path);
185
    }
186
187
    public function getMarkdownPagePath(string $path = ''): string
188
    {
189
        return $this->getModelSourcePath(MarkdownPage::class, $path);
190
    }
191
192
    public function getMarkdownPostPath(string $path = ''): string
193
    {
194
        return $this->getModelSourcePath(MarkdownPost::class, $path);
195
    }
196
197
    public function getDocumentationPagePath(string $path = ''): string
198
    {
199
        return $this->getModelSourcePath(DocumentationPage::class, $path);
200
    }
201
202
    public function smartGlob(string $pattern, int $flags = 0): Collection
203
    {
204
        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

204
        return collect(/** @scrutinizer ignore-type */ \Hyde\Facades\Filesystem::glob($pattern, $flags))
Loading history...
205
            ->map(fn (string $path): string => $this->pathToRelative($path));
206
    }
207
208
    /** @internal */
209
    public function qualifyPossiblePathArray(array|string $paths): array|string
210
    {
211
        if (is_array($paths)) {
0 ignored issues
show
introduced by
The condition is_array($paths) is always true.
Loading history...
212
            return array_map(fn ($path) => $this->pathToAbsolute($path), $paths);
213
        }
214
215
        return $this->pathToAbsolute($paths);
216
    }
217
218
    /**
219
     * Implode path components into a string with directory separators.
220
     */
221
    public static function implode(string $base, string ...$paths): string
222
    {
223
        return implode(DIRECTORY_SEPARATOR, array_merge([$base], $paths));
224
    }
225
}
226