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
|
|
|
* If a method uses the name `path` it refers to an internal file path. |
17
|
|
|
* if a method uses the name `link` it refers to a web link used in Blade templates. |
18
|
|
|
* |
19
|
|
|
* @see \Hyde\Framework\Testing\Feature\Foundation\FilesystemTest |
20
|
|
|
*/ |
21
|
|
|
class Filesystem |
22
|
|
|
{ |
23
|
|
|
protected HydeKernelContract $kernel; |
24
|
|
|
|
25
|
|
|
public function __construct(HydeKernelContract $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 allows choosing if files may be overwritten. |
70
|
|
|
* |
71
|
|
|
* @param string $from The source file path. |
72
|
|
|
* @param string $to The destination file path. |
73
|
|
|
* @param bool $force If true, existing files will be overwritten. |
74
|
|
|
* @return bool|int Returns true|false on copy() success|failure, or an error code on failure |
75
|
|
|
*/ |
76
|
|
|
public function copy(string $from, string $to, bool $force = false): bool|int |
77
|
|
|
{ |
78
|
|
|
if (! file_exists($from)) { |
79
|
|
|
return 404; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (file_exists($to) && ! $force) { |
83
|
|
|
return 409; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return copy($from, $to); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Fluent file helper methods. |
91
|
|
|
* |
92
|
|
|
* Provides a more fluent way of getting either the absolute path |
93
|
|
|
* to a model's source directory, or an absolute path to a file within it. |
94
|
|
|
* |
95
|
|
|
* These are intended to be used as a dynamic alternative to legacy code |
96
|
|
|
* Hyde::path('_pages/foo') becomes Hyde::getBladePagePath('foo') |
97
|
|
|
*/ |
98
|
|
|
public function getModelSourcePath(string $model, string $path = ''): string |
99
|
|
|
{ |
100
|
|
|
if (empty($path)) { |
101
|
|
|
return $this->path(DiscoveryService::getFilePathForModelClassFiles($model)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$path = unslash($path); |
105
|
|
|
|
106
|
|
|
return $this->path(DiscoveryService::getFilePathForModelClassFiles($model).DIRECTORY_SEPARATOR.$path); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getBladePagePath(string $path = ''): string |
110
|
|
|
{ |
111
|
|
|
return $this->getModelSourcePath(BladePage::class, $path); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getMarkdownPagePath(string $path = ''): string |
115
|
|
|
{ |
116
|
|
|
return $this->getModelSourcePath(MarkdownPage::class, $path); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getMarkdownPostPath(string $path = ''): string |
120
|
|
|
{ |
121
|
|
|
return $this->getModelSourcePath(MarkdownPost::class, $path); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getDocumentationPagePath(string $path = ''): string |
125
|
|
|
{ |
126
|
|
|
return $this->getModelSourcePath(DocumentationPage::class, $path); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the absolute path to the compiled site directory, or a file within it. |
131
|
|
|
*/ |
132
|
|
|
public function getSiteOutputPath(string $path = ''): string |
133
|
|
|
{ |
134
|
|
|
if (empty($path)) { |
135
|
|
|
return StaticPageBuilder::$outputPath; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$path = unslash($path); |
139
|
|
|
|
140
|
|
|
return StaticPageBuilder::$outputPath.DIRECTORY_SEPARATOR.$path; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Decode an absolute path created with a Hyde::path() helper into its relative counterpart. |
145
|
|
|
*/ |
146
|
|
|
public function pathToRelative(string $path): string |
147
|
|
|
{ |
148
|
|
|
return str_starts_with($path, $this->path()) ? unslash(str_replace( |
149
|
|
|
$this->path(), |
150
|
|
|
'', |
151
|
|
|
$path |
152
|
|
|
)) : $path; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|