1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Commands; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Hyde\Framework\Hyde; |
7
|
|
|
use Hyde\Framework\Services\BuildService; |
8
|
|
|
use Hyde\Framework\Services\DiscoveryService; |
9
|
|
|
use Hyde\Framework\Services\RebuildService; |
10
|
|
|
use LaravelZero\Framework\Commands\Command; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Hyde Command to build a single static site file. |
14
|
|
|
* |
15
|
|
|
* @see \Hyde\Framework\Testing\Feature\Commands\HydeRebuildStaticSiteCommandTest |
16
|
|
|
*/ |
17
|
|
|
class HydeRebuildStaticSiteCommand extends Command |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The signature of the command. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $signature = 'rebuild |
25
|
|
|
{path : The relative file path (example: _posts/hello-world.md)}'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The description of the command. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Run the static site builder for a single file'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The source path. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public string $path; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Execute the console command. |
43
|
|
|
* |
44
|
|
|
* @return int |
45
|
|
|
*/ |
46
|
|
|
public function handle(): int |
47
|
|
|
{ |
48
|
|
|
$time_start = microtime(true); |
49
|
|
|
|
50
|
|
|
if ($this->argument('path') === '_media') { |
51
|
|
|
(new BuildService($this->getOutput()))->transferMediaAssets(); |
52
|
|
|
|
53
|
|
|
return 0; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->path = $this->sanitizePathString($this->argument('path')); |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
$this->validate(); |
60
|
|
|
} catch (Exception $exception) { |
61
|
|
|
return $this->handleException($exception); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
(new RebuildService($this->path))->execute(); |
65
|
|
|
|
66
|
|
|
$time_end = microtime(true); |
67
|
|
|
$execution_time = ($time_end - $time_start); |
68
|
|
|
|
69
|
|
|
$this->info(sprintf( |
70
|
|
|
'Created %s in %s seconds. (%sms)', |
71
|
|
|
DiscoveryService::createClickableFilepath(Hyde::pages()->getPage($this->path)->getOutputPath()), |
72
|
|
|
number_format( |
73
|
|
|
$execution_time, |
74
|
|
|
2 |
75
|
|
|
), |
76
|
|
|
number_format(($execution_time * 1000), 2) |
77
|
|
|
)); |
78
|
|
|
|
79
|
|
|
return 0; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Perform a basic sanitation to strip trailing characters. |
84
|
|
|
* |
85
|
|
|
* @param string $path |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function sanitizePathString(string $path): string |
89
|
|
|
{ |
90
|
|
|
return str_replace('\\', '/', trim($path, '.\\/')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Validate the path to catch common errors. |
95
|
|
|
* |
96
|
|
|
* @throws Exception |
97
|
|
|
*/ |
98
|
|
|
public function validate(): void |
99
|
|
|
{ |
100
|
|
|
if (! ( |
101
|
|
|
str_starts_with($this->path, Hyde::pathToRelative(Hyde::getDocumentationPagePath())) || |
102
|
|
|
str_starts_with($this->path, Hyde::pathToRelative(Hyde::getMarkdownPostPath())) || |
103
|
|
|
str_starts_with($this->path, Hyde::pathToRelative(Hyde::getBladePagePath())) || |
104
|
|
|
str_starts_with($this->path, Hyde::pathToRelative(Hyde::getMarkdownPostPath())) |
105
|
|
|
)) { |
106
|
|
|
throw new Exception("Path [$this->path] is not in a valid source directory.", 400); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (! file_exists(Hyde::path($this->path))) { |
110
|
|
|
throw new Exception("File [$this->path] not found.", 404); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Output the contents of an exception. |
116
|
|
|
* |
117
|
|
|
* @param Exception $exception |
118
|
|
|
* @return int Error code |
119
|
|
|
*/ |
120
|
|
|
public function handleException(Exception $exception): int |
121
|
|
|
{ |
122
|
|
|
$this->error('Something went wrong!'); |
123
|
|
|
$this->warn($exception->getMessage()); |
124
|
|
|
|
125
|
|
|
return (int) $exception->getCode(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|