diff --git a/packages/framework/src/Console/Commands/RouteListCommand.php b/packages/framework/src/Console/Commands/RouteListCommand.php index 08ea7b712f0..bb93ecab52c 100644 --- a/packages/framework/src/Console/Commands/RouteListCommand.php +++ b/packages/framework/src/Console/Commands/RouteListCommand.php @@ -5,14 +5,12 @@ namespace Hyde\Console\Commands; use Hyde\Hyde; -use Hyde\Pages\InMemoryPage; -use Hyde\Support\Models\Route; use Hyde\Console\Concerns\Command; -use Hyde\Support\Internal\RouteList; use Hyde\Support\Internal\RouteListItem; -use function sprintf; -use function file_exists; +use function array_map; +use function array_keys; +use function array_values; /** * Display the list of site routes. @@ -27,54 +25,20 @@ class RouteListCommand extends Command public function handle(): int { - $routes = $this->routeListClass(); + $routes = $this->generate(); - $this->table($routes->headers(), $routes->toArray()); + $this->table($this->makeHeader($routes), $routes); return Command::SUCCESS; } - protected function routeListClass(): RouteList + protected function generate(): array { - return new class extends RouteList - { - protected static function routeToListItem(Route $route): RouteListItem - { - return new class($route) extends RouteListItem - { - protected function stylePageType(string $class): string - { - $type = parent::stylePageType($class); - - $page = $this->route->getPage(); - /** @experimental The typeLabel macro is experimental */ - if ($page instanceof InMemoryPage && $page->hasMacro('typeLabel')) { - $type .= sprintf(' (%s)', (string) $page->__call('typeLabel', [])); - } - - return $type; - } - - protected function styleSourcePath(string $path): string - { - return parent::styleSourcePath($path) !== 'none' - ? $this->href(Command::fileLink(Hyde::path($path)), $path) - : 'none'; - } - - protected function styleOutputPath(string $path): string - { - return file_exists(Hyde::sitePath($path)) - ? $this->href(Command::fileLink(Hyde::sitePath($path)), parent::styleOutputPath($path)) - : parent::styleOutputPath($path); - } + return array_map([RouteListItem::class, 'format'], array_values(Hyde::routes()->all())); + } - protected function href(string $link, string $label): string - { - return "$label"; - } - }; - } - }; + protected function makeHeader(array $routes): array + { + return array_map([Hyde::class, 'makeTitle'], array_keys($routes[0])); } } diff --git a/packages/framework/src/Support/Internal/RouteList.php b/packages/framework/src/Support/Internal/RouteList.php deleted file mode 100644 index 6160fd424b8..00000000000 --- a/packages/framework/src/Support/Internal/RouteList.php +++ /dev/null @@ -1,53 +0,0 @@ -> */ - protected array $routes; - - public function __construct() - { - $this->routes = $this->generate(); - } - - public function toArray(): array - { - return $this->routes; - } - - public function headers(): array - { - return array_map(function (string $key): string { - return ucwords(str_replace('_', ' ', $key)); - }, array_keys($this->routes[0])); - } - - protected function generate(): array - { - return collect(Hyde::routes())->map(function (Route $route): array { - return $this->routeToListItem($route)->toArray(); - })->values()->toArray(); - } - - protected static function routeToListItem(Route $route): RouteListItem - { - return new RouteListItem($route); - } -} diff --git a/packages/framework/src/Support/Internal/RouteListItem.php b/packages/framework/src/Support/Internal/RouteListItem.php index 9dac9d5f022..43e11c1b9be 100644 --- a/packages/framework/src/Support/Internal/RouteListItem.php +++ b/packages/framework/src/Support/Internal/RouteListItem.php @@ -7,46 +7,68 @@ use Hyde\Hyde; use Hyde\Pages\InMemoryPage; use Hyde\Support\Models\Route; -use Illuminate\Contracts\Support\Arrayable; +use Hyde\Console\Concerns\Command; +use function filled; +use function sprintf; +use function file_exists; use function class_basename; use function str_starts_with; /** * @internal This class is internal and should not be depended on outside the HydePHP framework code. */ -class RouteListItem implements Arrayable +class RouteListItem { protected Route $route; - public function __construct(Route $route) + public static function format(Route $route): array { - $this->route = $route; - } + $item = new static($route); - public function toArray(): array - { return [ - 'page_type' => $this->stylePageType($this->route->getPageClass()), - 'source_file' => $this->styleSourcePath($this->route->getSourcePath()), - 'output_file' => $this->styleOutputPath($this->route->getOutputPath()), - 'route_key' => $this->styleRouteKey($this->route->getRouteKey()), + 'page_type' => $item->stylePageType($route->getPageClass()), + 'source_file' => $item->styleSourcePath($route->getSourcePath()), + 'output_file' => $item->styleOutputPath($route->getOutputPath()), + 'route_key' => $item->styleRouteKey($route->getRouteKey()), ]; } + protected function __construct(Route $route) + { + $this->route = $route; + } + protected function stylePageType(string $class): string { - return str_starts_with($class, 'Hyde') ? class_basename($class) : $class; + $type = $this->getPageType($class); + + $page = $this->route->getPage(); + + /** @experimental The typeLabel macro is experimental */ + if ($page instanceof InMemoryPage && $page->hasMacro('typeLabel')) { + $type .= sprintf(' (%s)', (string) $page->__call('typeLabel', [])); + } + + return $type; } protected function styleSourcePath(string $path): string { - return $this->isPageDiscoverable() ? $path : 'none'; + if ($this->getSourcePath($path) === 'none') { + return 'none'; + } + + return file_exists(Hyde::path($path)) + ? $this->href(Command::fileLink(Hyde::path($path)), $this->getSourcePath($path)) + : $this->getSourcePath($path); } protected function styleOutputPath(string $path): string { - return Hyde::getOutputDirectory()."/$path"; + return file_exists(Hyde::sitePath($path)) + ? $this->href(Command::fileLink(Hyde::sitePath($path)), $this->getOutputPath($path)) + : $this->getOutputPath($path); } protected function styleRouteKey(string $key): string @@ -54,8 +76,28 @@ protected function styleRouteKey(string $key): string return $key; } + protected function getPageType(string $class): string + { + return str_starts_with($class, 'Hyde') ? class_basename($class) : $class; + } + + protected function getSourcePath(string $path): string + { + return $this->isPageDiscoverable() ? $path : 'none'; + } + + protected function getOutputPath(string $path): string + { + return Hyde::getOutputDirectory()."/$path"; + } + + protected function href(string $link, string $label): string + { + return "$label"; + } + protected function isPageDiscoverable(): bool { - return $this->route->getSourcePath() && ! $this->route->getPage() instanceof InMemoryPage; + return filled($this->route->getSourcePath()) && ! $this->route->getPage() instanceof InMemoryPage; } } diff --git a/packages/framework/tests/Feature/RouteListTest.php b/packages/framework/tests/Feature/RouteListTest.php deleted file mode 100644 index f4f977652a9..00000000000 --- a/packages/framework/tests/Feature/RouteListTest.php +++ /dev/null @@ -1,62 +0,0 @@ -assertSame([ - [ - 'page_type' => 'BladePage', - 'source_file' => '_pages/404.blade.php', - 'output_file' => '_site/404.html', - 'route_key' => '404', - ], - [ - 'page_type' => 'BladePage', - 'source_file' => '_pages/index.blade.php', - 'output_file' => '_site/index.html', - 'route_key' => 'index', - ], - ], (new RouteList())->toArray()); - } - - public function testHeaders() - { - $this->assertSame([ - 'Page Type', - 'Source File', - 'Output File', - 'Route Key', - ], (new RouteList())->headers()); - } - - public function testWithDynamicPages() - { - Hyde::routes()->forget('404'); - Hyde::routes()->forget('index'); - Hyde::routes()->put('foo', new Route(new InMemoryPage('foo'))); - - $this->assertSame([ - [ - 'page_type' => 'InMemoryPage', - 'source_file' => 'none', - 'output_file' => '_site/foo.html', - 'route_key' => 'foo', - ], - ], (new RouteList())->toArray()); - } -}