|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Console\Commands; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Framework\Concerns\TracksExecutionTime; |
|
8
|
|
|
use Hyde\Framework\Services\ValidationService; |
|
9
|
|
|
use LaravelZero\Framework\Commands\Command; |
|
10
|
|
|
|
|
11
|
|
|
use function number_format; |
|
12
|
|
|
use function microtime; |
|
13
|
|
|
use function sprintf; |
|
14
|
|
|
use function sizeof; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Run a series of tests to validate your setup and help you optimize your site. |
|
18
|
|
|
*/ |
|
19
|
|
|
class ValidateCommand extends Command |
|
20
|
|
|
{ |
|
21
|
|
|
use TracksExecutionTime; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
protected $signature = 'validate'; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
protected $description = 'Test and validate your project to optimize your site'; |
|
28
|
|
|
|
|
29
|
|
|
/** @var string */ |
|
30
|
|
|
protected $help = 'Run a series of tests to validate your setup and help you optimize your site'; |
|
31
|
|
|
|
|
32
|
|
|
protected ValidationService $service; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct() |
|
35
|
|
|
{ |
|
36
|
|
|
parent::__construct(); |
|
37
|
|
|
|
|
38
|
|
|
$this->service = new ValidationService(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function handle(): int |
|
42
|
|
|
{ |
|
43
|
|
|
$this->startClock(); |
|
44
|
|
|
|
|
45
|
|
|
$this->info('Running validation tests!'); |
|
46
|
|
|
|
|
47
|
|
|
$this->newLine(); |
|
48
|
|
|
|
|
49
|
|
|
foreach (ValidationService::checks() as $check) { |
|
50
|
|
|
$this->runCheck($check); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$this->info("All done! {$this->timeTotal()}"); |
|
54
|
|
|
|
|
55
|
|
|
return Command::SUCCESS; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function runCheck(string $check): void |
|
59
|
|
|
{ |
|
60
|
|
|
$timeStart = microtime(true); |
|
61
|
|
|
$result = $this->service->run($check); |
|
62
|
|
|
|
|
63
|
|
|
$this->line($result->formattedMessage($this->getCheckTime($timeStart))); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
$this->newline(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function getCheckTime(float $timeStart): string |
|
69
|
|
|
{ |
|
70
|
|
|
return number_format((microtime(true) - $timeStart) * 1000, 2); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function timeTotal(): string |
|
74
|
|
|
{ |
|
75
|
|
|
return sprintf("<fg=gray>Ran %s checks in {$this->getExecutionTimeString()}</>", |
|
76
|
|
|
sizeof(ValidationService::checks()) |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|