Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
27 / 27 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
| RouteListItem | |
100.00% |
27 / 27 |
|
100.00% |
11 / 11 |
19 | |
100.00% |
1 / 1 |
| format | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| stylePageType | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| styleSourcePath | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| styleOutputPath | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| styleRouteKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPageType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getSourcePath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getOutputPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| href | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isPageDiscoverable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Hyde\Support\Internal; |
| 6 | |
| 7 | use Hyde\Hyde; |
| 8 | use Hyde\Pages\InMemoryPage; |
| 9 | use Hyde\Support\Models\Route; |
| 10 | use Hyde\Console\Concerns\Command; |
| 11 | |
| 12 | use function filled; |
| 13 | use function sprintf; |
| 14 | use function file_exists; |
| 15 | use function class_basename; |
| 16 | use function str_starts_with; |
| 17 | |
| 18 | /** |
| 19 | * @internal This class is internal and should not be depended on outside the HydePHP framework code. |
| 20 | */ |
| 21 | class RouteListItem |
| 22 | { |
| 23 | protected Route $route; |
| 24 | |
| 25 | public static function format(Route $route): array |
| 26 | { |
| 27 | $item = new static($route); |
| 28 | |
| 29 | return [ |
| 30 | 'page_type' => $item->stylePageType($route->getPageClass()), |
| 31 | 'source_file' => $item->styleSourcePath($route->getSourcePath()), |
| 32 | 'output_file' => $item->styleOutputPath($route->getOutputPath()), |
| 33 | 'route_key' => $item->styleRouteKey($route->getRouteKey()), |
| 34 | ]; |
| 35 | } |
| 36 | |
| 37 | protected function __construct(Route $route) |
| 38 | { |
| 39 | $this->route = $route; |
| 40 | } |
| 41 | |
| 42 | protected function stylePageType(string $class): string |
| 43 | { |
| 44 | $type = $this->getPageType($class); |
| 45 | |
| 46 | $page = $this->route->getPage(); |
| 47 | |
| 48 | /** @experimental The typeLabel macro is experimental */ |
| 49 | if ($page instanceof InMemoryPage && $page->hasMacro('typeLabel')) { |
| 50 | $type .= sprintf(' <fg=gray>(%s)</>', (string) $page->__call('typeLabel', [])); |
| 51 | } |
| 52 | |
| 53 | return $type; |
| 54 | } |
| 55 | |
| 56 | protected function styleSourcePath(string $path): string |
| 57 | { |
| 58 | if ($this->getSourcePath($path) === 'none') { |
| 59 | return '<fg=gray>none</>'; |
| 60 | } |
| 61 | |
| 62 | return file_exists(Hyde::path($path)) |
| 63 | ? $this->href(Command::fileLink(Hyde::path($path)), $this->getSourcePath($path)) |
| 64 | : $this->getSourcePath($path); |
| 65 | } |
| 66 | |
| 67 | protected function styleOutputPath(string $path): string |
| 68 | { |
| 69 | return file_exists(Hyde::sitePath($path)) |
| 70 | ? $this->href(Command::fileLink(Hyde::sitePath($path)), $this->getOutputPath($path)) |
| 71 | : $this->getOutputPath($path); |
| 72 | } |
| 73 | |
| 74 | protected function styleRouteKey(string $key): string |
| 75 | { |
| 76 | return $key; |
| 77 | } |
| 78 | |
| 79 | protected function getPageType(string $class): string |
| 80 | { |
| 81 | return str_starts_with($class, 'Hyde') ? class_basename($class) : $class; |
| 82 | } |
| 83 | |
| 84 | protected function getSourcePath(string $path): string |
| 85 | { |
| 86 | return $this->isPageDiscoverable() ? $path : 'none'; |
| 87 | } |
| 88 | |
| 89 | protected function getOutputPath(string $path): string |
| 90 | { |
| 91 | return Hyde::getOutputDirectory()."/$path"; |
| 92 | } |
| 93 | |
| 94 | protected function href(string $link, string $label): string |
| 95 | { |
| 96 | return "<href=$link>$label</>"; |
| 97 | } |
| 98 | |
| 99 | protected function isPageDiscoverable(): bool |
| 100 | { |
| 101 | return filled($this->route->getSourcePath()) && ! $this->route->getPage() instanceof InMemoryPage; |
| 102 | } |
| 103 | } |