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