Passed
Push — master ( f5da7e...7b39d3 )
by Caen
03:09 queued 13s
created

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