|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Hyde\Console\Commands; |
| 6 | + |
| 7 | +use Exception; |
| 8 | +use Hyde\Console\Concerns\Command; |
| 9 | +use Hyde\Foundation\Facades\PageCollection; |
| 10 | +use Hyde\Framework\Features\BuildTasks\BuildTask; |
| 11 | +use Hyde\Framework\Services\BuildService; |
| 12 | +use Hyde\Framework\Services\RebuildService; |
| 13 | +use Hyde\Hyde; |
| 14 | +use Illuminate\Console\OutputStyle; |
| 15 | +use function dirname; |
| 16 | +use function file_exists; |
| 17 | +use function in_array; |
| 18 | +use function str_replace; |
| 19 | +use function unslash; |
| 20 | + |
| 21 | +/** |
| 22 | + * Hyde Command to build a single static site file. |
| 23 | + * |
| 24 | + * @see \Hyde\Framework\Testing\Feature\Commands\RebuildStaticSiteCommandTest |
| 25 | + */ |
| 26 | +class RebuildStaticPageCommand extends Command |
| 27 | +{ |
| 28 | + /** @var string */ |
| 29 | + protected $signature = 'rebuild {path : The relative file path (example: _posts/hello-world.md)}'; |
| 30 | + |
| 31 | + /** @var string */ |
| 32 | + protected $description = 'Run the static site builder for a single file'; |
| 33 | + |
| 34 | + public function handle(): int |
| 35 | + { |
| 36 | + if ($this->argument('path') === Hyde::getMediaDirectory()) { |
| 37 | + (new BuildService($this->getOutput()))->transferMediaAssets(); |
| 38 | + |
| 39 | + $this->info('All done!'); |
| 40 | + |
| 41 | + return Command::SUCCESS; |
| 42 | + } |
| 43 | + |
| 44 | + return $this->makeBuildTask($this->output, $this->getNormalizedPathString())->handle() ?? Command::SUCCESS; |
| 45 | + } |
| 46 | + |
| 47 | + protected function getNormalizedPathString(): string |
| 48 | + { |
| 49 | + return str_replace('\\', '/', unslash($this->argument('path'))); |
| 50 | + } |
| 51 | + |
| 52 | + protected function makeBuildTask(OutputStyle $output, string $path): BuildTask |
| 53 | + { |
| 54 | + return new class($output, $path) extends BuildTask |
| 55 | + { |
| 56 | + public static string $description = 'Rebuilding page'; |
| 57 | + |
| 58 | + protected string $path; |
| 59 | + |
| 60 | + public function __construct(OutputStyle $output, string $path) |
| 61 | + { |
| 62 | + parent::__construct($output); |
| 63 | + $this->path = $path; |
| 64 | + } |
| 65 | + |
| 66 | + public function run(): void |
| 67 | + { |
| 68 | + $this->validate(); |
| 69 | + |
| 70 | + (new RebuildService($this->path))->execute(); |
| 71 | + } |
| 72 | + |
| 73 | + public function then(): void |
| 74 | + { |
| 75 | + $this->createdSiteFile(Command::createClickableFilepath( |
| 76 | + PageCollection::getPage($this->path)->getOutputPath() |
| 77 | + ))->withExecutionTime(); |
| 78 | + } |
| 79 | + |
| 80 | + protected function validate(): void |
| 81 | + { |
| 82 | + $directory = Hyde::pathToRelative(dirname($this->path)); |
| 83 | + |
| 84 | + $directories = [ |
| 85 | + Hyde::pathToRelative(Hyde::getBladePagePath()), |
| 86 | + Hyde::pathToRelative(Hyde::getBladePagePath()), |
| 87 | + Hyde::pathToRelative(Hyde::getMarkdownPagePath()), |
| 88 | + Hyde::pathToRelative(Hyde::getMarkdownPostPath()), |
| 89 | + Hyde::pathToRelative(Hyde::getDocumentationPagePath()), |
| 90 | + ]; |
| 91 | + |
| 92 | + if (! in_array($directory, $directories)) { |
| 93 | + throw new Exception("Path [$this->path] is not in a valid source directory.", 400); |
| 94 | + } |
| 95 | + |
| 96 | + if (! file_exists(Hyde::path($this->path))) { |
| 97 | + throw new Exception("File [$this->path] not found.", 404); |
| 98 | + } |
| 99 | + } |
| 100 | + }; |
| 101 | + } |
| 102 | +} |
0 commit comments