Skip to content

Commit

Permalink
Merge pull request #666 from hydephp/refactor-build-service
Browse files Browse the repository at this point in the history
Clean up the build service class
  • Loading branch information
caendesilva authored Nov 16, 2022
2 parents 69e5ba3 + b6bdb57 commit ec3c401
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions packages/framework/src/Framework/Services/BuildService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Hyde\Framework\Services;

use Closure;
use Hyde\Facades\Site;
use Hyde\Foundation\RouteCollection;
use Hyde\Framework\Actions\StaticPageBuilder;
Expand All @@ -13,6 +12,7 @@
use Hyde\Support\Models\Route;
use Illuminate\Console\Concerns\InteractsWithIO;
use Illuminate\Console\OutputStyle;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;

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

public function compileStaticPages(): void
{
$this->getDiscoveredModels()->each(function (string $pageClass) {
$this->getClassNamesForDiscoveredPageModels()->each(function (string $pageClass) {
$this->compilePagesForClass($pageClass);
});
}
Expand All @@ -60,51 +60,42 @@ public function transferMediaAssets(): void
{
$this->needsDirectory(Hyde::sitePath('media'));

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

$this->withProgressBar(
$collection,
function ($filepath) {
copy($filepath, Hyde::sitePath('media/'.basename($filepath)));
}
);
$this->withProgressBar(DiscoveryService::getMediaAssetFiles(), function (string $filepath): void {
copy($filepath, Hyde::sitePath('media/'.basename($filepath)));
});

$this->newLine(2);
}

/**
* @return \Hyde\Foundation\RouteCollection<array-key, class-string<\Hyde\Pages\Concerns\HydePage>>
* @return \Illuminate\Support\Collection<array-key, class-string<\Hyde\Pages\Concerns\HydePage>>
*/
protected function getDiscoveredModels(): RouteCollection
protected function getClassNamesForDiscoveredPageModels(): Collection
{
return $this->router->getRoutes()->map(function (Route $route) {
return $this->router->getRoutes()->map(function (Route $route): string {
return $route->getPageClass();
})->unique();
}

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

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

$this->withProgressBar(
$collection,
$this->compileRoute()
);
$this->withProgressBar($collection, function (Route $route): void {
(new StaticPageBuilder($route->getPage()))->__invoke();
});

$this->newLine(2);
}

/** @psalm-return \Closure(\Hyde\Support\Models\Route):string */
protected function compileRoute(): Closure
{
return function (Route $route) {
return (new StaticPageBuilder($route->getPage()))->__invoke();
};
}

protected function getModelPluralName(string $pageClass): string
protected function getClassPluralName(string $pageClass): string
{
return preg_replace('/([a-z])([A-Z])/', '$1 $2', class_basename($pageClass)).'s';
}
Expand All @@ -122,10 +113,7 @@ protected function isItSafeToCleanOutputDirectory(): bool

protected function isOutputDirectoryWhitelisted(): bool
{
return in_array(
basename(Hyde::sitePath()),
config('hyde.safe_output_directories', ['_site', 'docs', 'build'])
);
return in_array(basename(Hyde::sitePath()), $this->safeOutputDirectories());
}

protected function askIfUnsafeDirectoryShouldBeEmptied(): bool
Expand All @@ -136,4 +124,9 @@ protected function askIfUnsafeDirectoryShouldBeEmptied(): bool
Site::$outputPath
));
}

protected function safeOutputDirectories(): array
{
return config('hyde.safe_output_directories', ['_site', 'docs', 'build']);
}
}

0 comments on commit ec3c401

Please sign in to comment.