1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiGen\Console\Command; |
4
|
|
|
|
5
|
|
|
use ApiGen\Configuration\Configuration; |
6
|
|
|
use ApiGen\Configuration\ConfigurationOptions; |
7
|
|
|
use ApiGen\Contracts\Generator\GeneratorQueueInterface; |
8
|
|
|
use ApiGen\Contracts\Parser\ParserInterface; |
9
|
|
|
use ApiGen\Contracts\Parser\ParserStorageInterface; |
10
|
|
|
use ApiGen\Theme\ThemeResources; |
11
|
|
|
use ApiGen\Utils\FileSystem; |
12
|
|
|
use ApiGen\Utils\Finder\FinderInterface; |
13
|
|
|
use Nette\DI\Config\Loader; |
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
final class GenerateCommand extends AbstractCommand |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Configuration |
23
|
|
|
*/ |
24
|
|
|
private $configuration; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ParserInterface |
28
|
|
|
*/ |
29
|
|
|
private $parser; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ParserStorageInterface |
33
|
|
|
*/ |
34
|
|
|
private $parserStorage; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var GeneratorQueueInterface |
38
|
|
|
*/ |
39
|
|
|
private $generatorQueue; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var FileSystem |
43
|
|
|
*/ |
44
|
|
|
private $fileSystem; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var ThemeResources |
48
|
|
|
*/ |
49
|
|
|
private $themeResources; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var FinderInterface |
53
|
|
|
*/ |
54
|
|
|
private $finder; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var OutputInterface |
58
|
|
|
*/ |
59
|
|
|
private $output; |
60
|
|
|
|
61
|
|
|
public function __construct( |
62
|
|
|
Configuration $configuration, |
63
|
|
|
ParserInterface $parser, |
64
|
|
|
ParserStorageInterface $parserStorage, |
65
|
|
|
GeneratorQueueInterface $generatorQueue, |
66
|
|
|
FileSystem $fileSystem, |
67
|
|
|
ThemeResources $themeResources, |
68
|
|
|
FinderInterface $finder |
69
|
|
|
) { |
70
|
|
|
parent::__construct(); |
71
|
|
|
|
72
|
|
|
$this->configuration = $configuration; |
73
|
|
|
$this->parser = $parser; |
74
|
|
|
$this->parserStorage = $parserStorage; |
75
|
|
|
$this->generatorQueue = $generatorQueue; |
76
|
|
|
$this->fileSystem = $fileSystem; |
77
|
|
|
$this->themeResources = $themeResources; |
78
|
|
|
$this->finder = $finder; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function configure(): void |
82
|
|
|
{ |
83
|
|
|
$this->setName('generate'); |
84
|
|
|
$this->setDescription('Generate API documentation'); |
85
|
|
|
$this->addArgument( |
86
|
|
|
ConfigurationOptions::SOURCE, |
87
|
|
|
InputArgument::IS_ARRAY | InputOption::VALUE_REQUIRED, |
88
|
|
|
'Dirs or files documentation is generated for.' |
89
|
|
|
); |
90
|
|
|
$this->addOption( |
91
|
|
|
ConfigurationOptions::DESTINATION, |
92
|
|
|
null, |
93
|
|
|
InputOption::VALUE_REQUIRED, |
94
|
|
|
'Target dir for generated documentation.' |
95
|
|
|
); |
96
|
|
|
$this->addOption( |
97
|
|
|
ConfigurationOptions::CONFIG, |
98
|
|
|
null, |
99
|
|
|
InputOption::VALUE_REQUIRED, |
100
|
|
|
'Path to apigen.neon config file.', |
101
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'apigen.neon' |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
106
|
|
|
{ |
107
|
|
|
$this->output = $output; |
108
|
|
|
|
109
|
|
|
$cliOptions = [ |
110
|
|
|
ConfigurationOptions::SOURCE => $input->getArgument(ConfigurationOptions::SOURCE), |
111
|
|
|
] + $input->getOptions(); |
112
|
|
|
|
113
|
|
|
$options = $this->prepareOptions($cliOptions); |
114
|
|
|
$this->scanAndParse($options); |
115
|
|
|
$this->generate($options); |
116
|
|
|
return 0; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param mixed[] $options |
121
|
|
|
*/ |
122
|
|
|
private function scanAndParse(array $options): void |
123
|
|
|
{ |
124
|
|
|
$this->output->writeln('<info>Scanning sources and parsing</info>'); |
125
|
|
|
|
126
|
|
|
$files = $this->finder->find( |
127
|
|
|
$options[ConfigurationOptions::SOURCE], |
128
|
|
|
$options[ConfigurationOptions::EXCLUDE], |
129
|
|
|
$options[ConfigurationOptions::EXTENSIONS] |
130
|
|
|
); |
131
|
|
|
$this->parser->parse($files); |
132
|
|
|
|
133
|
|
|
$stats = $this->parserStorage->getDocumentedStats(); |
134
|
|
|
$this->output->writeln(sprintf( |
135
|
|
|
'Found <comment>%d classes</comment>, <comment>%d constants</comment> and <comment>%d functions</comment>', |
136
|
|
|
$stats['classes'], |
137
|
|
|
$stats['constants'], |
138
|
|
|
$stats['functions'] |
139
|
|
|
)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param mixed[] $options |
144
|
|
|
*/ |
145
|
|
|
private function generate(array $options): void |
146
|
|
|
{ |
147
|
|
|
$this->prepareDestination( |
148
|
|
|
$options[ConfigurationOptions::DESTINATION], |
149
|
|
|
(bool) $options[ConfigurationOptions::FORCE_OVERWRITE] |
150
|
|
|
); |
151
|
|
|
$this->output->writeln('<info>Generating API documentation</info>'); |
152
|
|
|
$this->generatorQueue->run(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param mixed[] $options |
157
|
|
|
* @return mixed[] |
158
|
|
|
*/ |
159
|
|
|
private function prepareOptions(array $options): array |
160
|
|
|
{ |
161
|
|
|
$options = $this->loadOptionsFromConfig($options); |
162
|
|
|
|
163
|
|
|
return $this->configuration->resolveOptions($options); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param mixed[] $options |
168
|
|
|
* @return mixed[] |
169
|
|
|
*/ |
170
|
|
|
private function loadOptionsFromConfig(array $options): array |
171
|
|
|
{ |
172
|
|
|
$configFile = $options[ConfigurationOptions::CONFIG] ?? getcwd() . DIRECTORY_SEPARATOR . 'apigen.neon'; |
173
|
|
|
$configFile = $this->fileSystem->getAbsolutePath($configFile); |
174
|
|
|
|
175
|
|
|
if (file_exists($configFile)) { |
176
|
|
|
$configFileOptions = (new Loader())->load($configFile); |
177
|
|
|
return array_merge($options, $configFileOptions); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $options; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
private function prepareDestination(string $destination, bool $shouldOverwrite = false): void |
184
|
|
|
{ |
185
|
|
|
if ($shouldOverwrite) { |
186
|
|
|
$this->fileSystem->purgeDir($destination); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$this->themeResources->copyToDestination($destination); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|