Passed
Push — master ( b2e70b...c39e2e )
by Caen
03:08 queued 11s
created

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