Skip to content

Commit ec3c401

Browse files
authored
Merge pull request #666 from hydephp/refactor-build-service
Clean up the build service class
2 parents 69e5ba3 + b6bdb57 commit ec3c401

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

packages/framework/src/Framework/Services/BuildService.php

+23-30
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Hyde\Framework\Services;
66

7-
use Closure;
87
use Hyde\Facades\Site;
98
use Hyde\Foundation\RouteCollection;
109
use Hyde\Framework\Actions\StaticPageBuilder;
@@ -13,6 +12,7 @@
1312
use Hyde\Support\Models\Route;
1413
use Illuminate\Console\Concerns\InteractsWithIO;
1514
use Illuminate\Console\OutputStyle;
15+
use Illuminate\Support\Collection;
1616
use Illuminate\Support\Facades\File;
1717

1818
/**
@@ -39,7 +39,7 @@ public function __construct(OutputStyle $output)
3939

4040
public function compileStaticPages(): void
4141
{
42-
$this->getDiscoveredModels()->each(function (string $pageClass) {
42+
$this->getClassNamesForDiscoveredPageModels()->each(function (string $pageClass) {
4343
$this->compilePagesForClass($pageClass);
4444
});
4545
}
@@ -60,51 +60,42 @@ public function transferMediaAssets(): void
6060
{
6161
$this->needsDirectory(Hyde::sitePath('media'));
6262

63-
$collection = DiscoveryService::getMediaAssetFiles();
6463
$this->comment('Transferring Media Assets...');
6564

66-
$this->withProgressBar(
67-
$collection,
68-
function ($filepath) {
69-
copy($filepath, Hyde::sitePath('media/'.basename($filepath)));
70-
}
71-
);
65+
$this->withProgressBar(DiscoveryService::getMediaAssetFiles(), function (string $filepath): void {
66+
copy($filepath, Hyde::sitePath('media/'.basename($filepath)));
67+
});
68+
7269
$this->newLine(2);
7370
}
7471

7572
/**
76-
* @return \Hyde\Foundation\RouteCollection<array-key, class-string<\Hyde\Pages\Concerns\HydePage>>
73+
* @return \Illuminate\Support\Collection<array-key, class-string<\Hyde\Pages\Concerns\HydePage>>
7774
*/
78-
protected function getDiscoveredModels(): RouteCollection
75+
protected function getClassNamesForDiscoveredPageModels(): Collection
7976
{
80-
return $this->router->getRoutes()->map(function (Route $route) {
77+
return $this->router->getRoutes()->map(function (Route $route): string {
8178
return $route->getPageClass();
8279
})->unique();
8380
}
8481

82+
/**
83+
* @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass
84+
*/
8585
protected function compilePagesForClass(string $pageClass): void
8686
{
87-
$this->comment("Creating {$this->getModelPluralName($pageClass)}...");
87+
$this->comment("Creating {$this->getClassPluralName($pageClass)}...");
8888

8989
$collection = $this->router->getRoutes($pageClass);
9090

91-
$this->withProgressBar(
92-
$collection,
93-
$this->compileRoute()
94-
);
91+
$this->withProgressBar($collection, function (Route $route): void {
92+
(new StaticPageBuilder($route->getPage()))->__invoke();
93+
});
9594

9695
$this->newLine(2);
9796
}
9897

99-
/** @psalm-return \Closure(\Hyde\Support\Models\Route):string */
100-
protected function compileRoute(): Closure
101-
{
102-
return function (Route $route) {
103-
return (new StaticPageBuilder($route->getPage()))->__invoke();
104-
};
105-
}
106-
107-
protected function getModelPluralName(string $pageClass): string
98+
protected function getClassPluralName(string $pageClass): string
10899
{
109100
return preg_replace('/([a-z])([A-Z])/', '$1 $2', class_basename($pageClass)).'s';
110101
}
@@ -122,10 +113,7 @@ protected function isItSafeToCleanOutputDirectory(): bool
122113

123114
protected function isOutputDirectoryWhitelisted(): bool
124115
{
125-
return in_array(
126-
basename(Hyde::sitePath()),
127-
config('hyde.safe_output_directories', ['_site', 'docs', 'build'])
128-
);
116+
return in_array(basename(Hyde::sitePath()), $this->safeOutputDirectories());
129117
}
130118

131119
protected function askIfUnsafeDirectoryShouldBeEmptied(): bool
@@ -136,4 +124,9 @@ protected function askIfUnsafeDirectoryShouldBeEmptied(): bool
136124
Site::$outputPath
137125
));
138126
}
127+
128+
protected function safeOutputDirectories(): array
129+
{
130+
return config('hyde.safe_output_directories', ['_site', 'docs', 'build']);
131+
}
139132
}

0 commit comments

Comments
 (0)