|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPSA\Command; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\ParserFactory; |
|
6
|
|
|
use PHPSA\Application; |
|
7
|
|
|
use PHPSA\Compiler; |
|
8
|
|
|
use PHPSA\Context; |
|
9
|
|
|
use PHPSA\Definition\FileParser; |
|
10
|
|
|
use RecursiveDirectoryIterator; |
|
11
|
|
|
use RecursiveIteratorIterator; |
|
12
|
|
|
use SplFileInfo; |
|
13
|
|
|
use FilesystemIterator; |
|
14
|
|
|
use Symfony\Component\Config\FileLocator; |
|
15
|
|
|
use Symfony\Component\Console\Command\Command; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
20
|
|
|
use Webiny\Component\EventManager\EventManager; |
|
21
|
|
|
|
|
22
|
|
|
class CompileCommand extends Command |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
872 |
|
protected function configure() |
|
29
|
|
|
{ |
|
30
|
872 |
|
$this |
|
31
|
872 |
|
->setName('compile') |
|
32
|
872 |
|
->setDescription('Runs compiler on all files in path') |
|
33
|
872 |
|
->addArgument('path', InputArgument::OPTIONAL, 'Path to check file or directory', '.'); |
|
34
|
872 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
40
|
|
|
{ |
|
41
|
|
|
$output->writeln(''); |
|
42
|
|
|
|
|
43
|
|
|
if (extension_loaded('xdebug')) { |
|
44
|
|
|
/** |
|
45
|
|
|
* This will disable only showing stack traces on error conditions. |
|
46
|
|
|
*/ |
|
47
|
|
|
if (function_exists('xdebug_disable')) { |
|
48
|
|
|
xdebug_disable(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$output->writeln('<error>It is highly recommended to disable the XDebug extension before invoking this command.</error>'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, new \PhpParser\Lexer\Emulative([ |
|
55
|
|
|
'usedAttributes' => [ |
|
56
|
|
|
'comments', |
|
57
|
|
|
'startLine', |
|
58
|
|
|
'endLine', |
|
59
|
|
|
'startTokenPos', |
|
60
|
|
|
'endTokenPos' |
|
61
|
|
|
] |
|
62
|
|
|
])); |
|
63
|
|
|
|
|
64
|
|
|
/** @var Application $application */ |
|
65
|
|
|
$application = $this->getApplication(); |
|
66
|
|
|
$application->compiler = new Compiler(); |
|
67
|
|
|
|
|
68
|
|
|
$em = EventManager::getInstance(); |
|
|
|
|
|
|
69
|
|
|
$context = new Context($output, $application, $em); |
|
70
|
|
|
|
|
71
|
|
|
$fileParser = new FileParser( |
|
72
|
|
|
$parser, |
|
73
|
|
|
$this->getCompiler() |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
$path = $input->getArgument('path'); |
|
77
|
|
|
if (is_dir($path)) { |
|
78
|
|
|
$directoryIterator = new RecursiveIteratorIterator( |
|
79
|
|
|
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS) |
|
80
|
|
|
); |
|
81
|
|
|
$output->writeln('Scanning directory <info>' . $path . '</info>'); |
|
82
|
|
|
|
|
83
|
|
|
$count = 0; |
|
84
|
|
|
|
|
85
|
|
|
/** @var SplFileInfo $file */ |
|
86
|
|
|
foreach ($directoryIterator as $file) { |
|
87
|
|
|
if ($file->getExtension() !== 'php') { |
|
88
|
|
|
continue; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$context->debug($file->getPathname()); |
|
92
|
|
|
$count++; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$output->writeln("Found <info>{$count} files</info>"); |
|
96
|
|
|
|
|
97
|
|
|
if ($count > 100) { |
|
98
|
|
|
$output->writeln('<comment>Caution: You are trying to scan a lot of files; this might be slow. For bigger libraries, consider setting up a dedicated platform or using ci.lowl.io.</comment>'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$output->writeln(''); |
|
102
|
|
|
|
|
103
|
|
|
/** @var SplFileInfo $file */ |
|
104
|
|
|
foreach ($directoryIterator as $file) { |
|
105
|
|
|
if ($file->getExtension() !== 'php') { |
|
106
|
|
|
continue; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$fileParser->parserFile($file->getPathname(), $context); |
|
110
|
|
|
} |
|
111
|
|
|
} elseif (is_file($path)) { |
|
112
|
|
|
$fileParser->parserFile($path, $context); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Step 2 Recursive check ... |
|
118
|
|
|
*/ |
|
119
|
|
|
$application->compiler->compile($context); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return Compiler |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function getCompiler() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->getApplication()->compiler; |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.