Passed
Push — master ( 001f10...448d62 )
by Caen
03:23 queued 12s
created

Filesystem::sitePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 2
b 0
f 0
cc 2
nc 2
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 are relative to the root of the application.
27
 *
28
 * @see \Hyde\Framework\Testing\Feature\Foundation\FilesystemTest
29
 */
30
class Filesystem
31
{
32
    protected HydeKernel $kernel;
33
34
    public function __construct(HydeKernel $kernel)
35
    {
36
        $this->kernel = $kernel;
37
    }
38
39
    public function getBasePath(): string
40
    {
41
        return $this->kernel->getBasePath();
42
    }
43
44
    /**
45
     * Get an absolute file path from a supplied relative path.
46
     *
47
     * The function returns the fully qualified path to your site's root directory.
48
     *
49
     * You may also use the function to generate a fully qualified path to a given file
50
     * relative to the project root directory when supplying the path argument.
51
     *
52
     * @param  string  $path
53
     * @return string
54
     */
55
    public function path(string $path = ''): string
56
    {
57
        if (empty($path)) {
58
            return $this->getBasePath();
59
        }
60
61
        $path = unslash($path);
62
63
        return $this->implode($this->getBasePath(), $path);
64
    }
65
66
    /**
67
     * Get an absolute file path from a supplied relative path.
68
     */
69
    public function pathToAbsolute(string $path): string
70
    {
71
        return $this->path($path);
72
    }
73
74
    /**
75
     * Decode an absolute path created with a Hyde::path() helper into its relative counterpart.
76
     */
77
    public function pathToRelative(string $path): string
78
    {
79
        return str_starts_with($path, $this->path())
80
            ? unslash(str_replace($this->path(), '', $path))
81
            : $path;
82
    }
83
84
    /**
85
     * Get the absolute path to the compiled site directory, or a file within it.
86
     */
87
    public function sitePath(string $path = ''): string
88
    {
89
        if (empty($path)) {
90
            return Hyde::path(Site::$outputPath);
91
        }
92
93
        $path = unslash($path);
94
95
        return Hyde::path(Site::$outputPath.DIRECTORY_SEPARATOR.$path);
96
    }
97
98
    /**
99
     * Works similarly to the path() function, but returns a file in the Framework package.
100
     *
101
     * @param  string  $path
102
     * @return string
103
     */
104
    public function vendorPath(string $path = ''): string
105
    {
106
        return $this->path('vendor/hyde/framework/'.unslash($path));
107
    }
108
109
    /**
110
     * Wrapper for the copy function, but using project relative paths.
111
     */
112
    public function copy(string $from, string $to): bool
113
    {
114
        return copy($this->path($from), $this->path($to));
115
    }
116
117
    /**
118
     * Touch one or more files in the project's directory.
119
     */
120
    public function touch(string|array $path): bool
121
    {
122
        if (is_string($path)) {
0 ignored issues
show
introduced by
The condition is_string($path) is always false.
Loading history...
123
            return touch($this->path($path));
124
        }
125
126
        foreach ($path as $p) {
127
            touch($this->path($p));
128
        }
129
130
        return true;
131
    }
132
133
    /**
134
     * Unlink one or more files in the project's directory.
135
     */
136
    public function unlink(string|array $path): bool
137
    {
138
        if (is_string($path)) {
0 ignored issues
show
introduced by
The condition is_string($path) is always false.
Loading history...
139
            return unlink($this->path($path));
140
        }
141
142
        foreach ($path as $p) {
143
            unlink($this->path($p));
144
        }
145
146
        return true;
147
    }
148
149
    /**
150
     * Fluent file helper methods.
151
     *
152
     * Provides a more fluent way of getting either the absolute path
153
     * to a model's source directory, or an absolute path to a file within it.
154
     *
155
     * These are intended to be used as a dynamic alternative to legacy code
156
     * Hyde::path('_pages/foo') becomes Hyde::getBladePagePath('foo')
157
     */
158
    public function getModelSourcePath(string $model, string $path = ''): string
159
    {
160
        if (empty($path)) {
161
            return $this->path(DiscoveryService::getModelSourceDirectory($model));
162
        }
163
164
        $path = unslash($path);
165
166
        return $this->path($this->implode(DiscoveryService::getModelSourceDirectory($model), $path));
167
    }
168
169
    public function getBladePagePath(string $path = ''): string
170
    {
171
        return $this->getModelSourcePath(BladePage::class, $path);
172
    }
173
174
    public function getMarkdownPagePath(string $path = ''): string
175
    {
176
        return $this->getModelSourcePath(MarkdownPage::class, $path);
177
    }
178
179
    public function getMarkdownPostPath(string $path = ''): string
180
    {
181
        return $this->getModelSourcePath(MarkdownPost::class, $path);
182
    }
183
184
    public function getDocumentationPagePath(string $path = ''): string
185
    {
186
        return $this->getModelSourcePath(DocumentationPage::class, $path);
187
    }
188
189
    public function smartGlob(string $pattern, int $flags = 0): Collection
190
    {
191
        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

191
        return collect(/** @scrutinizer ignore-type */ \Hyde\Facades\Filesystem::glob($pattern, $flags))
Loading history...
192
            ->map(fn (string $path): string => $this->pathToRelative($path));
193
    }
194
195
    /** @internal */
196
    public function qualifyPossiblePathArray(array|string $paths): array|string
197
    {
198
        if (is_array($paths)) {
0 ignored issues
show
introduced by
The condition is_array($paths) is always true.
Loading history...
199
            return array_map(fn ($path) => $this->pathToAbsolute($path), $paths);
200
        }
201
202
        return $this->pathToAbsolute($paths);
203
    }
204
205
    /**
206
     * Implode path components into a string with directory separators.
207
     */
208
    public static function implode(string $base, string ...$paths): string
209
    {
210
        return implode(DIRECTORY_SEPARATOR, array_merge([$base], $paths));
211
    }
212
}
213