Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| DiscoveryService | |
100.00% |
17 / 17 |
|
100.00% |
6 / 6 |
12 | |
100.00% |
1 / 1 |
| getParserClassForModel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getParserInstanceForModel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFileExtensionForModelFiles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFilePathForModelClassFiles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| findModelFromFilePath | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
7 | |||
| createClickableFilepath | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Hyde\Framework\Services; |
| 4 | |
| 5 | use Hyde\Framework\Contracts\AbstractPage; |
| 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 | * The Discovery Service (previously called BuildService) provides |
| 13 | * helper methods for source file autodiscovery used in the building |
| 14 | * process to determine where files are located and how to parse them. |
| 15 | */ |
| 16 | class DiscoveryService |
| 17 | { |
| 18 | public static function getParserClassForModel(string $model): string |
| 19 | { |
| 20 | /** @var AbstractPage $model */ |
| 21 | return $model::$parserClass; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Create and get a constructed instance of a Model's Parser class. |
| 26 | * |
| 27 | * @param string $model Class constant of the Model to get the Parser for. |
| 28 | * @param string $slug The slug of the source file to parse. |
| 29 | * |
| 30 | * @example getParserForModel(MarkdownPost::class, 'hello-world') |
| 31 | * |
| 32 | * @return object The constructed Parser instance. |
| 33 | */ |
| 34 | public static function getParserInstanceForModel(string $model, string $slug): object |
| 35 | { |
| 36 | /** @var AbstractPage $model */ |
| 37 | return new $model::$parserClass($slug); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get the file extension for a models source files. |
| 42 | */ |
| 43 | public static function getFileExtensionForModelFiles(string $model): string |
| 44 | { |
| 45 | /** @var AbstractPage $model */ |
| 46 | return $model::$fileExtension; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get the source directory path of a model. |
| 51 | */ |
| 52 | public static function getFilePathForModelClassFiles(string $model): string |
| 53 | { |
| 54 | /** @var AbstractPage $model */ |
| 55 | return $model::$sourceDirectory; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Determine the Page Model to use for a given file path. |
| 60 | * |
| 61 | * @param string $filepath |
| 62 | * @return string|false The model class constant, or false if none was found. |
| 63 | * |
| 64 | * @see \Tests\Unit\DiscoveryServiceCanFindModelFromCustomSourceFilePathTest |
| 65 | */ |
| 66 | public static function findModelFromFilePath(string $filepath): string|false |
| 67 | { |
| 68 | if (str_starts_with($filepath, MarkdownPost::$sourceDirectory)) { |
| 69 | return MarkdownPost::class; |
| 70 | } |
| 71 | |
| 72 | if (str_starts_with($filepath, DocumentationPage::$sourceDirectory)) { |
| 73 | return DocumentationPage::class; |
| 74 | } |
| 75 | |
| 76 | if (str_starts_with($filepath, MarkdownPage::$sourceDirectory) |
| 77 | && str_ends_with($filepath, '.md')) { |
| 78 | return MarkdownPage::class; |
| 79 | } |
| 80 | |
| 81 | if (str_starts_with($filepath, BladePage::$sourceDirectory) |
| 82 | && str_ends_with($filepath, '.blade.php')) { |
| 83 | return BladePage::class; |
| 84 | } |
| 85 | |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Create a filepath that can be opened in the browser from a terminal. |
| 91 | * |
| 92 | * @param string $filepath |
| 93 | * @return string |
| 94 | */ |
| 95 | public static function createClickableFilepath(string $filepath): string |
| 96 | { |
| 97 | return 'file://'.str_replace( |
| 98 | '\\', |
| 99 | '/', |
| 100 | realpath($filepath) |
| 101 | ); |
| 102 | } |
| 103 | } |