|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Hyde\Framework\Commands; |
| 4 | + |
| 5 | +use Hyde\Framework\Hyde; |
| 6 | +use Hyde\Framework\Services\DiscoveryService; |
| 7 | +use LaravelZero\Framework\Commands\Command; |
| 8 | + |
| 9 | +/** |
| 10 | + * Hyde command to display the list of site routes. |
| 11 | + * |
| 12 | + * @see \Hyde\Framework\Testing\Feature\Commands\HydeRouteListCommandTest |
| 13 | + */ |
| 14 | +class HydeRouteListCommand extends Command |
| 15 | +{ |
| 16 | + protected $signature = 'route:list'; |
| 17 | + protected $description = 'Display all registered routes.'; |
| 18 | + |
| 19 | + public function handle(): int |
| 20 | + { |
| 21 | + $this->table([ |
| 22 | + 'Page Type', |
| 23 | + 'Source File', |
| 24 | + 'Output File', |
| 25 | + 'Route Key', |
| 26 | + ], $this->getRoutes()); |
| 27 | + |
| 28 | + return 0; |
| 29 | + } |
| 30 | + |
| 31 | + protected function getRoutes(): array |
| 32 | + { |
| 33 | + $routes = []; |
| 34 | + /** @var \Hyde\Framework\Models\Route $route */ |
| 35 | + foreach (Hyde::routes() as $route) { |
| 36 | + $routes[] = [ |
| 37 | + $this->formatPageType($route->getPageType()), |
| 38 | + $this->formatSourcePath($route->getSourcePath()), |
| 39 | + $this->formatOutputPath($route->getOutputPath()), |
| 40 | + $route->getRouteKey(), |
| 41 | + ]; |
| 42 | + } |
| 43 | + |
| 44 | + return $routes; |
| 45 | + } |
| 46 | + |
| 47 | + protected function formatPageType(string $class): string |
| 48 | + { |
| 49 | + return str_replace('Hyde\\Framework\\Models\\Pages\\', '', $class); |
| 50 | + } |
| 51 | + |
| 52 | + protected function formatSourcePath(string $path): string |
| 53 | + { |
| 54 | + return $this->clickablePathLink(DiscoveryService::createClickableFilepath(Hyde::path($path)), $path); |
| 55 | + } |
| 56 | + |
| 57 | + protected function formatOutputPath(string $path): string |
| 58 | + { |
| 59 | + if (! file_exists(Hyde::sitePath($path))) { |
| 60 | + return "_site/$path"; |
| 61 | + } |
| 62 | + |
| 63 | + return $this->clickablePathLink(DiscoveryService::createClickableFilepath(Hyde::sitePath($path)), "_site/$path"); |
| 64 | + } |
| 65 | + |
| 66 | + protected function clickablePathLink(string $link, string $path): string |
| 67 | + { |
| 68 | + return "<href=$link>$path</>"; |
| 69 | + } |
| 70 | +} |
0 commit comments