|
2 | 2 |
|
3 | 3 | namespace Hyde\Framework;
|
4 | 4 |
|
5 |
| -use Composer\InstalledVersions; |
6 |
| -use Hyde\Framework\Contracts\RouteContract; |
7 |
| -use Hyde\Framework\Helpers\Features; |
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 |
| -use Illuminate\Support\Facades\View; |
14 |
| -use Illuminate\Support\Str; |
15 |
| -use Illuminate\Support\Traits\Macroable; |
| 5 | +use Illuminate\Support\Facades\Facade; |
16 | 6 |
|
17 | 7 | /**
|
18 |
| - * Encapsulates a HydePHP project, providing helpful methods for interacting with it. |
| 8 | + * General facade for Hyde services. |
19 | 9 | *
|
20 |
| - * @see \Hyde\Framework\Facades\Hyde |
| 10 | + * @see \Hyde\Framework\HydeKernel |
21 | 11 | *
|
22 | 12 | * @author Caen De Silva <caen@desilva.se>
|
23 | 13 | * @copyright 2022 Caen De Silva
|
24 | 14 | * @license MIT License
|
25 | 15 | *
|
26 | 16 | * @link https://hydephp.com/
|
27 | 17 | */
|
28 |
| -class Hyde |
| 18 | +class Hyde extends Facade |
29 | 19 | {
|
30 |
| - use Macroable; |
31 |
| - |
32 |
| - protected string $basePath; |
33 |
| - |
34 |
| - public function __construct(?string $basePath = null) |
35 |
| - { |
36 |
| - $this->basePath = $basePath ?? getcwd(); |
37 |
| - } |
38 |
| - |
39 |
| - public static function version(): string |
40 |
| - { |
41 |
| - return InstalledVersions::getPrettyVersion('hyde/framework') ?: 'unreleased'; |
42 |
| - } |
43 |
| - |
44 |
| - public static function getBasePath(): string |
45 |
| - { |
46 |
| - /** @deprecated Set path in constructor when instantiating the Singleton. */ |
47 |
| - if (! isset(static::$basePath)) { |
48 |
| - static::$basePath = getcwd(); |
49 |
| - } |
50 |
| - |
51 |
| - return static::$basePath; |
52 |
| - } |
53 |
| - |
54 |
| - /** |
55 |
| - * @deprecated Set path in constructor when instantiating the Singleton. |
56 |
| - */ |
57 |
| - public static function setBasePath(string $path): void |
58 |
| - { |
59 |
| - static::$basePath = $path; |
60 |
| - } |
61 |
| - |
62 |
| - // HydeHelperFacade |
63 |
| - |
64 |
| - public static function features(): Features |
65 |
| - { |
66 |
| - return new Features; |
67 |
| - } |
68 |
| - |
69 |
| - public static function hasFeature(string $feature): bool |
70 |
| - { |
71 |
| - return Features::enabled($feature); |
72 |
| - } |
73 |
| - |
74 |
| - public static function makeTitle(string $slug): string |
75 |
| - { |
76 |
| - $alwaysLowercase = ['a', 'an', 'the', 'in', 'on', 'by', 'with', 'of', 'and', 'or', 'but']; |
77 |
| - |
78 |
| - return ucfirst(str_ireplace( |
79 |
| - $alwaysLowercase, |
80 |
| - $alwaysLowercase, |
81 |
| - Str::headline($slug) |
82 |
| - )); |
83 |
| - } |
84 |
| - |
85 |
| - /** |
86 |
| - * File helper methods. |
87 |
| - * |
88 |
| - * If a method uses the name `path` it refers to an internal file path. |
89 |
| - * if a method uses the name `link` it refers to a web link used in Blade templates. |
90 |
| - */ |
91 |
| - |
92 |
| - /** |
93 |
| - * Get an absolute file path from a supplied relative path. |
94 |
| - * |
95 |
| - * The function returns the fully qualified path to your site's root directory. |
96 |
| - * |
97 |
| - * You may also use the function to generate a fully qualified path to a given file |
98 |
| - * relative to the project root directory when supplying the path argument. |
99 |
| - * |
100 |
| - * @param string $path |
101 |
| - * @return string |
102 |
| - */ |
103 |
| - public static function path(string $path = ''): string |
104 |
| - { |
105 |
| - if (empty($path)) { |
106 |
| - return static::getBasePath(); |
107 |
| - } |
108 |
| - |
109 |
| - $path = unslash($path); |
110 |
| - |
111 |
| - return static::getBasePath().DIRECTORY_SEPARATOR.$path; |
112 |
| - } |
113 |
| - |
114 |
| - /** |
115 |
| - * Works similarly to the path() function, but returns a file in the Framework package. |
116 |
| - * |
117 |
| - * @param string $path |
118 |
| - * @return string |
119 |
| - */ |
120 |
| - public static function vendorPath(string $path = ''): string |
121 |
| - { |
122 |
| - return static::path('vendor/hyde/framework/'.unslash($path)); |
123 |
| - } |
124 |
| - |
125 |
| - /** |
126 |
| - * Format a link to an HTML file, allowing for pretty URLs, if enabled. |
127 |
| - * |
128 |
| - * @see \Hyde\Framework\Testing\Unit\FileHelperPageLinkPrettyUrlTest |
129 |
| - */ |
130 |
| - public static function pageLink(string $destination): string |
131 |
| - { |
132 |
| - if (config('site.pretty_urls', false) === true) { |
133 |
| - if (str_ends_with($destination, '.html')) { |
134 |
| - if ($destination === 'index.html') { |
135 |
| - return '/'; |
136 |
| - } |
137 |
| - if ($destination === DocumentationPage::getOutputDirectory().'/index.html') { |
138 |
| - return DocumentationPage::getOutputDirectory().'/'; |
139 |
| - } |
140 |
| - |
141 |
| - return substr($destination, 0, -5); |
142 |
| - } |
143 |
| - } |
144 |
| - |
145 |
| - return $destination; |
146 |
| - } |
147 |
| - |
148 |
| - /** |
149 |
| - * Inject the proper number of `../` before the links in Blade templates. |
150 |
| - * |
151 |
| - * Since v0.50.x you no longer have to supply a current page as it will be automatically retrieved from the View. |
152 |
| - * |
153 |
| - * @param string $destination relative to output directory on compiled site |
154 |
| - * @param string|null $current the current URI path relative to the site root |
155 |
| - * @return string |
156 |
| - * |
157 |
| - * @see \Hyde\Framework\Testing\Unit\FileHelperRelativeLinkTest |
158 |
| - */ |
159 |
| - public static function relativeLink(string $destination, ?string $current = null): string |
160 |
| - { |
161 |
| - if (str_starts_with($destination, '../')) { |
162 |
| - return $destination; |
163 |
| - } |
164 |
| - |
165 |
| - if ($current === null) { |
166 |
| - $current = static::currentPage(); |
167 |
| - } |
168 |
| - |
169 |
| - $nestCount = substr_count($current, '/'); |
170 |
| - $route = ''; |
171 |
| - if ($nestCount > 0) { |
172 |
| - $route .= str_repeat('../', $nestCount); |
173 |
| - } |
174 |
| - $route .= static::pageLink($destination); |
175 |
| - |
176 |
| - return str_replace('//', '/', $route); |
177 |
| - } |
178 |
| - |
179 |
| - /** |
180 |
| - * Get the current page path, or fall back to the root path. |
181 |
| - */ |
182 |
| - public static function currentPage(): string |
183 |
| - { |
184 |
| - return View::shared('currentPage', ''); |
185 |
| - } |
186 |
| - |
187 |
| - /** |
188 |
| - * Get the current page route, or fall back to null. |
189 |
| - */ |
190 |
| - public static function currentRoute(): ?RouteContract |
191 |
| - { |
192 |
| - return View::shared('currentRoute'); |
193 |
| - } |
194 |
| - |
195 |
| - /** |
196 |
| - * Gets a relative web link to the given image stored in the _site/media folder. |
197 |
| - * Since v0.50.x you no longer have to supply a current page as it will be automatically retrieved from the View. |
198 |
| - */ |
199 |
| - public static function image(string $name, string $current = null): string |
200 |
| - { |
201 |
| - if ($current === null) { |
202 |
| - $current = static::currentPage(); |
203 |
| - } |
204 |
| - |
205 |
| - if (str_starts_with($name, 'http')) { |
206 |
| - return $name; |
207 |
| - } |
208 |
| - |
209 |
| - return static::relativeLink('media/'.basename($name), $current); |
210 |
| - } |
211 |
| - |
212 |
| - /** |
213 |
| - * Return a qualified URI path, if SITE_URL is set in .env, else return false. |
214 |
| - * |
215 |
| - * @param string|null $path optional relative path suffix. Omit to return base url. |
216 |
| - * @return string|false |
217 |
| - */ |
218 |
| - public static function uriPath(?string $path = ''): string|false |
219 |
| - { |
220 |
| - if (config('site.url', false)) { |
221 |
| - return rtrim(config('site.url'), '/').'/'.(trim($path, '/') ?? ''); |
222 |
| - } |
223 |
| - |
224 |
| - return false; |
225 |
| - } |
226 |
| - |
227 |
| - /** |
228 |
| - * Wrapper for the copy function, but allows choosing if files may be overwritten. |
229 |
| - * |
230 |
| - * @param string $from The source file path. |
231 |
| - * @param string $to The destination file path. |
232 |
| - * @param bool $force If true, existing files will be overwritten. |
233 |
| - * @return bool|int Returns true|false on copy() success|failure, or an error code on failure |
234 |
| - */ |
235 |
| - public static function copy(string $from, string $to, bool $force = false): bool|int |
236 |
| - { |
237 |
| - if (! file_exists($from)) { |
238 |
| - return 404; |
239 |
| - } |
240 |
| - |
241 |
| - if (file_exists($to) && ! $force) { |
242 |
| - return 409; |
243 |
| - } |
244 |
| - |
245 |
| - return copy($from, $to); |
246 |
| - } |
247 |
| - |
248 |
| - /** |
249 |
| - * Fluent file helper methods. |
250 |
| - * |
251 |
| - * Provides a more fluent way of getting either the absolute path |
252 |
| - * to a model's source directory, or an absolute path to a file within it. |
253 |
| - * |
254 |
| - * These are intended to be used as a dynamic alternative to legacy code |
255 |
| - * Hyde::path('_pages/foo') becomes Hyde::getBladePagePath('foo') |
256 |
| - */ |
257 |
| - public static function getModelSourcePath(string $model, string $path = ''): string |
258 |
| - { |
259 |
| - if (empty($path)) { |
260 |
| - return static::path(DiscoveryService::getFilePathForModelClassFiles($model)); |
261 |
| - } |
262 |
| - |
263 |
| - $path = unslash($path); |
264 |
| - |
265 |
| - return static::path(DiscoveryService::getFilePathForModelClassFiles($model).DIRECTORY_SEPARATOR.$path); |
266 |
| - } |
267 |
| - |
268 |
| - public static function getBladePagePath(string $path = ''): string |
269 |
| - { |
270 |
| - return static::getModelSourcePath(BladePage::class, $path); |
271 |
| - } |
272 |
| - |
273 |
| - public static function getMarkdownPagePath(string $path = ''): string |
274 |
| - { |
275 |
| - return static::getModelSourcePath(MarkdownPage::class, $path); |
276 |
| - } |
277 |
| - |
278 |
| - public static function getMarkdownPostPath(string $path = ''): string |
279 |
| - { |
280 |
| - return static::getModelSourcePath(MarkdownPost::class, $path); |
281 |
| - } |
282 |
| - |
283 |
| - public static function getDocumentationPagePath(string $path = ''): string |
284 |
| - { |
285 |
| - return static::getModelSourcePath(DocumentationPage::class, $path); |
286 |
| - } |
287 |
| - |
288 |
| - /** |
289 |
| - * Get the absolute path to the compiled site directory, or a file within it. |
290 |
| - */ |
291 |
| - public static function getSiteOutputPath(string $path = ''): string |
292 |
| - { |
293 |
| - if (empty($path)) { |
294 |
| - return StaticPageBuilder::$outputPath; |
295 |
| - } |
296 |
| - |
297 |
| - $path = unslash($path); |
298 |
| - |
299 |
| - return StaticPageBuilder::$outputPath.DIRECTORY_SEPARATOR.$path; |
300 |
| - } |
301 |
| - |
302 |
| - /** |
303 |
| - * Decode an absolute path created with a Hyde::path() helper into its relative counterpart. |
304 |
| - */ |
305 |
| - public static function pathToRelative(string $path): string |
| 20 | + protected static function getFacadeAccessor(): string |
306 | 21 | {
|
307 |
| - return str_starts_with($path, static::path()) ? unslash(str_replace( |
308 |
| - static::path(), |
309 |
| - '', |
310 |
| - $path |
311 |
| - )) : $path; |
| 22 | + return HydeKernel::class; |
312 | 23 | }
|
313 | 24 | }
|
0 commit comments