Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
15 / 15 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
FluentPathHelpers | |
100.00% |
15 / 15 |
|
100.00% |
7 / 7 |
10 | |
100.00% |
1 / 1 |
getModelSourcePath | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getBladePagePath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getMarkdownPagePath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getMarkdownPostPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDocumentationPagePath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSiteOutputPath | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
pathToRelative | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Hyde\Framework\Concerns\Internal; |
4 | |
5 | use Hyde\Framework\Models\BladePage; |
6 | use Hyde\Framework\Models\DocumentationPage; |
7 | use Hyde\Framework\Models\MarkdownPage; |
8 | use Hyde\Framework\Models\MarkdownPost; |
9 | use Hyde\Framework\Services\DiscoveryService; |
10 | use Hyde\Framework\StaticPageBuilder; |
11 | |
12 | /** |
13 | * Offloads file helper methods for the Hyde Facade. |
14 | * |
15 | * Provides a more fluent way of getting either the absolute path |
16 | * to a model's source directory, or an absolute path to a file within it. |
17 | * |
18 | * These are intended to be used as a dynamic alternative to legacy code |
19 | * Hyde::path('_pages/foo') becomes Hyde::getBladePagePath('foo') |
20 | * |
21 | * @see \Hyde\Framework\Hyde |
22 | * @see \Tests\Unit\FluentPathHelpersTest |
23 | */ |
24 | trait FluentPathHelpers |
25 | { |
26 | public static function getModelSourcePath(string $model, string $path = ''): string |
27 | { |
28 | if (empty($path)) { |
29 | return static::path(DiscoveryService::getFilePathForModelClassFiles($model)); |
30 | } |
31 | |
32 | $path = trim($path, '/\\'); |
33 | |
34 | return static::path(DiscoveryService::getFilePathForModelClassFiles($model).DIRECTORY_SEPARATOR.$path); |
35 | } |
36 | |
37 | public static function getBladePagePath(string $path = ''): string |
38 | { |
39 | return static::getModelSourcePath(BladePage::class, $path); |
40 | } |
41 | |
42 | public static function getMarkdownPagePath(string $path = ''): string |
43 | { |
44 | return static::getModelSourcePath(MarkdownPage::class, $path); |
45 | } |
46 | |
47 | public static function getMarkdownPostPath(string $path = ''): string |
48 | { |
49 | return static::getModelSourcePath(MarkdownPost::class, $path); |
50 | } |
51 | |
52 | public static function getDocumentationPagePath(string $path = ''): string |
53 | { |
54 | return static::getModelSourcePath(DocumentationPage::class, $path); |
55 | } |
56 | |
57 | /** |
58 | * Get the absolute path to the compiled site directory, or a file within it. |
59 | */ |
60 | public static function getSiteOutputPath(string $path = ''): string |
61 | { |
62 | if (empty($path)) { |
63 | return StaticPageBuilder::$outputPath; |
64 | } |
65 | |
66 | $path = trim($path, '/\\'); |
67 | |
68 | return StaticPageBuilder::$outputPath.DIRECTORY_SEPARATOR.$path; |
69 | } |
70 | |
71 | /** |
72 | * Decode an absolute path created with a Hyde::path() helper into its relative counterpart. |
73 | */ |
74 | public static function pathToRelative(string $path): string |
75 | { |
76 | return str_starts_with($path, static::path()) ? trim(str_replace( |
77 | static::path(), '', $path), '/\\' |
78 | ) : $path; |
79 | } |
80 | } |