Skip to content

Commit

Permalink
Merge pull request #1679 from hydephp/refactor-to-move-internal-code-…
Browse files Browse the repository at this point in the history
…to-internal-namespace

Refactor to massively simplify internal route list helper classes
  • Loading branch information
caendesilva authored Apr 25, 2024
2 parents e54c12c + f9a80ad commit dd83299
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 177 deletions.
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.

0 comments on commit dd83299

Please sign in to comment.