Skip to content

Commit 161fefb

Browse files
author
github-actions
committed
Merge pull request #516 from hydephp/add-a-route-list-command
Add a route:list command hydephp/develop@2cd2f28
1 parent 76113e1 commit 161fefb

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

src/Commands/HydeRouteListCommand.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

src/HydeServiceProvider.php

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ protected function registerHydeConsoleCommands(): void
108108
Commands\HydeBuildSitemapCommand::class,
109109
Commands\HydeBuildRssFeedCommand::class,
110110
Commands\HydeBuildSearchCommand::class,
111+
Commands\HydeRouteListCommand::class,
111112
Commands\HydeMakePostCommand::class,
112113
Commands\HydeMakePageCommand::class,
113114
Commands\HydeValidateCommand::class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Hyde\Framework\Testing\Feature\Commands;
4+
5+
use Hyde\Testing\TestCase;
6+
7+
/**
8+
* @covers \Hyde\Framework\Commands\HydeRouteListCommand
9+
*/
10+
class HydeRouteListCommandTest extends TestCase
11+
{
12+
public function testRouteListCommand()
13+
{
14+
$this->artisan('route:list')
15+
->expectsTable(['Page Type', 'Source File', 'Output File', 'Route Key'], [
16+
[
17+
'BladePage',
18+
'_pages/404.blade.php',
19+
'_site/404.html',
20+
'404',
21+
],
22+
[
23+
'BladePage',
24+
'_pages/index.blade.php',
25+
'_site/index.html',
26+
'index',
27+
],
28+
])->assertExitCode(0);
29+
}
30+
31+
public function testClickableLinks()
32+
{
33+
$this->file('_site/index.html');
34+
$this->artisan('route:list')
35+
->assertExitCode(0);
36+
}
37+
}

0 commit comments

Comments
 (0)