Passed
Push — master ( 4f8b07...d0e934 )
by Caen
04:08 queued 12s
created

Filesystem::getSiteOutputPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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