1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Suitmedia\LighthouseAudit; |
4
|
|
|
|
5
|
|
|
use LogicException; |
6
|
|
|
use Suitmedia\LighthouseAudit\Audit\AuditManager; |
7
|
|
|
use Suitmedia\LighthouseAudit\Audit\Concerns\SanitizeInput; |
8
|
|
|
use Symfony\Component\Console\Command\Command as AbstractCommand; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
final class Command extends AbstractCommand |
15
|
|
|
{ |
16
|
|
|
use SanitizeInput; |
17
|
|
|
|
18
|
|
|
public const DEFAULT_URL_PREFIX = 'http://localhost:8000/'; |
19
|
|
|
public const DEFAULT_MODE = 'mobile'; |
20
|
|
|
public const DEFAULT_PERFORMANCE = '80'; |
21
|
|
|
public const DEFAULT_BEST_PRACTICES = '80'; |
22
|
|
|
public const DEFAULT_ACCESSIBILITY = '80'; |
23
|
|
|
public const DEFAULT_SEO = '80'; |
24
|
|
|
public const DEFAULT_PWA = '0'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Configures the lighthouse-audit command. |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
protected function configure() :void |
32
|
|
|
{ |
33
|
|
|
$this->setName('lighthouse-audit') |
34
|
|
|
->setDefinition([new InputArgument( |
35
|
|
|
'path', |
36
|
|
|
InputArgument::REQUIRED, |
37
|
|
|
'Specify the path of a directory to analyse.' |
38
|
|
|
)]) |
39
|
|
|
->addOption( |
40
|
|
|
'url-prefix', |
41
|
|
|
null, |
42
|
|
|
InputOption::VALUE_OPTIONAL, |
43
|
|
|
'Define the url prefix that should be used when testing.', |
44
|
|
|
self::DEFAULT_URL_PREFIX |
45
|
|
|
) |
46
|
|
|
->addOption( |
47
|
|
|
'mode', |
48
|
|
|
null, |
49
|
|
|
InputOption::VALUE_OPTIONAL, |
50
|
|
|
'Define the mode to run Lighthouse audit. Option: mobile,desktop', |
51
|
|
|
self::DEFAULT_MODE |
52
|
|
|
) |
53
|
|
|
->addOption( |
54
|
|
|
'performance', |
55
|
|
|
null, |
56
|
|
|
InputOption::VALUE_OPTIONAL, |
57
|
|
|
'Define the minimal performance score for audit to pass', |
58
|
|
|
self::DEFAULT_PERFORMANCE |
59
|
|
|
) |
60
|
|
|
->addOption( |
61
|
|
|
'best-practices', |
62
|
|
|
null, |
63
|
|
|
InputOption::VALUE_OPTIONAL, |
64
|
|
|
'Define the minimal best-practices score for audit to pass', |
65
|
|
|
self::DEFAULT_BEST_PRACTICES |
66
|
|
|
) |
67
|
|
|
->addOption( |
68
|
|
|
'accessibility', |
69
|
|
|
null, |
70
|
|
|
InputOption::VALUE_OPTIONAL, |
71
|
|
|
'Define the minimal accessibility score for audit to pass', |
72
|
|
|
self::DEFAULT_ACCESSIBILITY |
73
|
|
|
) |
74
|
|
|
->addOption( |
75
|
|
|
'seo', |
76
|
|
|
null, |
77
|
|
|
InputOption::VALUE_OPTIONAL, |
78
|
|
|
'Define the minimal seo score for audit to pass', |
79
|
|
|
self::DEFAULT_SEO |
80
|
|
|
) |
81
|
|
|
->addOption( |
82
|
|
|
'pwa', |
83
|
|
|
null, |
84
|
|
|
InputOption::VALUE_OPTIONAL, |
85
|
|
|
'Define the minimal pwa score for audit to pass', |
86
|
|
|
self::DEFAULT_PWA |
87
|
|
|
) |
88
|
|
|
->addOption( |
89
|
|
|
'except', |
90
|
|
|
null, |
91
|
|
|
InputOption::VALUE_OPTIONAL, |
92
|
|
|
'Provide a list of filenames that you wish to exclude, separated by commas.'.PHP_EOL |
93
|
|
|
) |
94
|
|
|
->addOption( |
95
|
|
|
'chrome-flags', |
96
|
|
|
null, |
97
|
|
|
InputOption::VALUE_OPTIONAL, |
98
|
|
|
'Custom flags to pass to Chrome (space-delimited). For a full list of flags, '.PHP_EOL.'see http://peter.sh/experiments/chromium-command-line-switches/.'.PHP_EOL |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Executes the current command. |
104
|
|
|
* |
105
|
|
|
* This method is not abstract because you can use this class |
106
|
|
|
* as a concrete class. In this case, instead of defining the |
107
|
|
|
* execute() method, you set the code to execute by passing |
108
|
|
|
* a Closure to the setCode() method. |
109
|
|
|
* |
110
|
|
|
* @param InputInterface $input |
111
|
|
|
* @param OutputInterface $output |
112
|
|
|
* @return int |
113
|
|
|
* @throws LogicException|\Exception |
114
|
|
|
*/ |
115
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) :int |
116
|
|
|
{ |
117
|
|
|
$app = $this->getApplication(); |
118
|
|
|
$excludedFiles = $this->getExcludedFiles($input); |
119
|
|
|
|
120
|
|
|
$output->writeln($app->getLongVersion()); |
121
|
|
|
$output->writeln(''); |
122
|
|
|
|
123
|
|
|
$finder = new HtmlFileFinder($input->getArgument('path')); |
124
|
|
|
$files = $finder->getFiles(); |
125
|
|
|
|
126
|
|
|
foreach ($files as $file) { |
127
|
|
|
if (in_array($file, $excludedFiles, true)) { |
128
|
|
|
continue; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$audit = new AuditManager( |
132
|
|
|
$this->getApplication()->getProcessBuilder(), |
133
|
|
|
$input, |
134
|
|
|
$output, |
135
|
|
|
$file |
136
|
|
|
); |
137
|
|
|
$audit->run(); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return 0; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get the list of files that should be excluded from analysis. |
145
|
|
|
* |
146
|
|
|
* @param InputInterface $input |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
public function getExcludedFiles(InputInterface $input) :array |
150
|
|
|
{ |
151
|
|
|
$except = $input->getOption('except'); |
152
|
|
|
|
153
|
|
|
$files = is_string($except) ? explode(',', $this->trimDoubleQuotes($except)) : []; |
154
|
|
|
|
155
|
|
|
return array_filter(array_map('trim', $files)); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: