Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
30 / 30 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
CollectionService | |
100.00% |
30 / 30 |
|
100.00% |
6 / 6 |
18 | |
100.00% |
1 / 1 |
getSourceFileListForModel | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
getBladePageList | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getMarkdownPageList | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getMarkdownPostList | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getDocumentationPageList | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getMediaAssetFiles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Hyde\Framework\Services; |
4 | |
5 | use Hyde\Framework\Hyde; |
6 | use Hyde\Framework\Models\BladePage; |
7 | use Hyde\Framework\Models\DocumentationPage; |
8 | use Hyde\Framework\Models\MarkdownPage; |
9 | use Hyde\Framework\Models\MarkdownPost; |
10 | |
11 | /** |
12 | * Contains service methods to return helpful collections of arrays and lists. |
13 | * |
14 | * @see \Tests\Feature\Services\CollectionServiceTest |
15 | */ |
16 | class CollectionService |
17 | { |
18 | /** |
19 | * Supply a model::class constant and get a list of all the existing source file base names. |
20 | * |
21 | * @param string $model |
22 | * @return array|false array on success, false if the class was not found |
23 | * |
24 | * @example CollectionService::getSourceFileListForModel(BladePage::class) |
25 | */ |
26 | public static function getSourceFileListForModel(string $model): array|false |
27 | { |
28 | if ($model == BladePage::class) { |
29 | return self::getBladePageList(); |
30 | } |
31 | |
32 | if ($model == MarkdownPage::class) { |
33 | return self::getMarkdownPageList(); |
34 | } |
35 | |
36 | if ($model == MarkdownPost::class) { |
37 | return self::getMarkdownPostList(); |
38 | } |
39 | |
40 | if ($model == DocumentationPage::class) { |
41 | return self::getDocumentationPageList(); |
42 | } |
43 | |
44 | return false; |
45 | } |
46 | |
47 | /** |
48 | * Get all the Blade files in the resources/views/vendor/hyde/pages directory. |
49 | * |
50 | * @return array |
51 | */ |
52 | public static function getBladePageList(): array |
53 | { |
54 | $array = []; |
55 | |
56 | foreach (glob(Hyde::path(BladePage::$sourceDirectory.'/*.blade.php')) as $filepath) { |
57 | if (! str_starts_with(basename($filepath), '_')) { |
58 | $array[] = basename($filepath, '.blade.php'); |
59 | } |
60 | } |
61 | |
62 | return $array; |
63 | } |
64 | |
65 | /** |
66 | * Get all the Markdown files in the _pages directory. |
67 | * |
68 | * @return array |
69 | */ |
70 | public static function getMarkdownPageList(): array |
71 | { |
72 | $array = []; |
73 | |
74 | foreach (glob(Hyde::path(MarkdownPage::$sourceDirectory.'/*.md')) as $filepath) { |
75 | if (! str_starts_with(basename($filepath), '_')) { |
76 | $array[] = basename($filepath, '.md'); |
77 | } |
78 | } |
79 | |
80 | return $array; |
81 | } |
82 | |
83 | /** |
84 | * Get all the Markdown files in the _posts directory. |
85 | * |
86 | * @return array |
87 | */ |
88 | public static function getMarkdownPostList(): array |
89 | { |
90 | $array = []; |
91 | |
92 | foreach (glob(Hyde::path(MarkdownPost::$sourceDirectory.'/*.md')) as $filepath) { |
93 | if (! str_starts_with(basename($filepath), '_')) { |
94 | $array[] = basename($filepath, '.md'); |
95 | } |
96 | } |
97 | |
98 | return $array; |
99 | } |
100 | |
101 | /** |
102 | * Get all the Markdown files in the _docs directory. |
103 | * |
104 | * @return array |
105 | */ |
106 | public static function getDocumentationPageList(): array |
107 | { |
108 | $array = []; |
109 | |
110 | foreach (glob(Hyde::path(DocumentationPage::$sourceDirectory.'/*.md')) as $filepath) { |
111 | if (! str_starts_with(basename($filepath), '_')) { |
112 | $array[] = basename($filepath, '.md'); |
113 | } |
114 | } |
115 | |
116 | return $array; |
117 | } |
118 | |
119 | /** |
120 | * Get all the Media asset file paths. |
121 | * Returns a full file path, unlike the other get*List methods. |
122 | */ |
123 | public static function getMediaAssetFiles(): array |
124 | { |
125 | return glob(Hyde::path('_media/*.{png,svg,jpg,jpeg,gif,ico,css,js}'), GLOB_BRACE); |
126 | } |
127 | } |