Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to massively simplify internal route list helper classes #1679

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c397747
Refactor to massively simplify internal helper classes
caendesilva Apr 25, 2024
8e5bc4d
Reorder helper methods
caendesilva Apr 25, 2024
9b10bb1
Inline simplified helper method
caendesilva Apr 25, 2024
ca6c8ab
Simplify closure type handling
caendesilva Apr 25, 2024
014ef94
Revert "Simplify closure type handling"
caendesilva Apr 25, 2024
d6d897f
Unwrap collect call for collection
caendesilva Apr 25, 2024
1fd09ce
Replace unnecessarily complex collection calls with array map
caendesilva Apr 25, 2024
66293c3
Normalize code syntax
caendesilva Apr 25, 2024
43ac245
Simplify helper class
caendesilva Apr 25, 2024
cebe636
Simplify the helpers further
caendesilva Apr 25, 2024
d9db79d
Use filled function instead of implicit check
caendesilva Apr 25, 2024
5476b9f
Extract method
caendesilva Apr 25, 2024
0db4925
Inline simplified internal route list helper class
caendesilva Apr 25, 2024
b3b469f
Inline method
caendesilva Apr 25, 2024
90ff369
Unwrap list syntax
caendesilva Apr 25, 2024
8915638
Inline local variable
caendesilva Apr 25, 2024
88acb8f
Rename method
caendesilva Apr 25, 2024
e851532
Move up method
caendesilva Apr 25, 2024
d24e181
Use helper function with same semantics
caendesilva Apr 25, 2024
a190602
Use short array map syntax
caendesilva Apr 25, 2024
d02fb50
Refactor entry point to be static
caendesilva Apr 25, 2024
a83c765
Scope down internal helper API
caendesilva Apr 25, 2024
b566214
Inline helper method
caendesilva Apr 25, 2024
5e36dde
Unwrap proxy call
caendesilva Apr 25, 2024
9e01e0a
Formatting
caendesilva Apr 25, 2024
518d452
Use short callback syntax
caendesilva Apr 25, 2024
ed779bb
Refactor and normalize qualifier assembly
caendesilva Apr 25, 2024
f9a80ad
Remove unused import
caendesilva Apr 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 11 additions & 47 deletions packages/framework/src/Console/Commands/RouteListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(' <fg=gray>(%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)
: '<fg=gray>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 "<href=$link>$label</>";
}
};
}
};
protected function makeHeader(array $routes): array
{
return array_map([Hyde::class, 'makeTitle'], array_keys($routes[0]));
}
}
53 changes: 0 additions & 53 deletions packages/framework/src/Support/Internal/RouteList.php

This file was deleted.

72 changes: 57 additions & 15 deletions packages/framework/src/Support/Internal/RouteListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,97 @@
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(' <fg=gray>(%s)</>', (string) $page->__call('typeLabel', []));
}

return $type;
}

protected function styleSourcePath(string $path): string
{
return $this->isPageDiscoverable() ? $path : 'none';
if ($this->getSourcePath($path) === 'none') {
return '<fg=gray>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
{
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 "<href=$link>$label</>";
}

protected function isPageDiscoverable(): bool
{
return $this->route->getSourcePath() && ! $this->route->getPage() instanceof InMemoryPage;
return filled($this->route->getSourcePath()) && ! $this->route->getPage() instanceof InMemoryPage;
}
}
62 changes: 0 additions & 62 deletions packages/framework/tests/Feature/RouteListTest.php

This file was deleted.

Loading