|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Framework\Services; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Hyde; |
|
8
|
|
|
use Hyde\Pages\InMemoryPage; |
|
9
|
|
|
use Hyde\Foundation\Facades\Routes; |
|
10
|
|
|
use Hyde\Foundation\Kernel\RouteCollection; |
|
11
|
|
|
use Hyde\Framework\Actions\StaticPageBuilder; |
|
12
|
|
|
use Hyde\Pages\Concerns\HydePage; |
|
13
|
|
|
use Hyde\Support\Models\Route; |
|
14
|
|
|
use Illuminate\Console\Concerns\InteractsWithIO; |
|
15
|
|
|
use Illuminate\Console\OutputStyle; |
|
16
|
|
|
|
|
17
|
|
|
use function class_basename; |
|
18
|
|
|
use function preg_replace; |
|
19
|
|
|
use function collect; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Moves logic from the build command to a service. |
|
23
|
|
|
* |
|
24
|
|
|
* Handles the build loop which generates the static site. |
|
25
|
|
|
* |
|
26
|
|
|
* @see \Hyde\Console\Commands\BuildSiteCommand |
|
27
|
|
|
*/ |
|
28
|
|
|
class BuildService |
|
29
|
|
|
{ |
|
30
|
|
|
use InteractsWithIO; |
|
31
|
|
|
|
|
32
|
|
|
protected RouteCollection $router; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(OutputStyle $output) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->output = $output; |
|
37
|
|
|
|
|
38
|
|
|
$this->router = Hyde::routes(); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function compileStaticPages(): void |
|
42
|
|
|
{ |
|
43
|
|
|
collect($this->getPageTypes())->each(function (string $pageClass): void { |
|
|
|
|
|
|
44
|
|
|
$this->compilePagesForClass($pageClass); |
|
45
|
|
|
}); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass |
|
|
|
|
|
|
50
|
|
|
*/ |
|
51
|
|
|
protected function compilePagesForClass(string $pageClass): void |
|
52
|
|
|
{ |
|
53
|
|
|
$this->comment("Creating {$this->getClassPluralName($pageClass)}..."); |
|
54
|
|
|
|
|
55
|
|
|
$collection = Routes::getRoutes($pageClass); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
$this->withProgressBar($collection, function (Route $route): void { |
|
58
|
|
|
StaticPageBuilder::handle($route->getPage()); |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
$this->newLine(2); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function getClassPluralName(string $pageClass): string |
|
65
|
|
|
{ |
|
66
|
|
|
if ($pageClass === InMemoryPage::class) { |
|
67
|
|
|
return 'Dynamic Pages'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return preg_replace('/([a-z])([A-Z])/', '$1 $2', class_basename($pageClass)).'s'; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** @return array<class-string<\Hyde\Pages\Concerns\HydePage>> */ |
|
|
|
|
|
|
74
|
|
|
protected function getPageTypes(): array |
|
75
|
|
|
{ |
|
76
|
|
|
return Hyde::pages()->map(function (HydePage $page): string { |
|
|
|
|
|
|
77
|
|
|
if ($page instanceof InMemoryPage) { |
|
78
|
|
|
return InMemoryPage::class; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $page::class; |
|
82
|
|
|
})->unique()->values()->toArray(); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|