25
25
use Monolog \Logger ;
26
26
use phpDocumentor \FileSystem \Finder \Exclude ;
27
27
use phpDocumentor \FileSystem \FlySystemAdapter ;
28
+ use phpDocumentor \Guides \Cli \Internal \RunCommand ;
28
29
use phpDocumentor \Guides \Cli \Logger \SpyProcessor ;
29
30
use phpDocumentor \Guides \Compiler \CompilerContext ;
30
31
use phpDocumentor \Guides \Event \PostProjectNodeCreated ;
@@ -71,47 +72,13 @@ public function __construct(
71
72
private readonly ClockInterface $ clock ,
72
73
private readonly EventDispatcher $ eventDispatcher ,
73
74
private readonly ProgressBarSubscriber $ progressBarSubscriber ,
75
+ private SettingsBuilder $ settingsBuilder ,
74
76
) {
75
77
parent ::__construct ('run ' );
76
78
77
- $ this ->addArgument (
78
- 'input ' ,
79
- InputArgument::OPTIONAL ,
80
- 'Directory which holds the files to render ' ,
81
- );
82
- $ this ->addOption (
83
- 'output ' ,
84
- null ,
85
- InputOption::VALUE_REQUIRED ,
86
- 'Directory to write rendered files to ' ,
87
- );
88
-
89
- $ this ->addOption (
90
- 'input-file ' ,
91
- null ,
92
- InputOption::VALUE_REQUIRED ,
93
- 'If set, only the specified file is parsed, relative to the directory specified in "input" ' ,
94
- );
95
-
96
- $ this ->addOption (
97
- 'exclude-path ' ,
98
- null ,
99
- InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY ,
100
- 'Paths to exclude, doc files in these directories will not be parsed ' ,
101
- );
79
+ $ this ->settingsBuilder ??= new SettingsBuilder ($ this ->eventDispatcher , $ this ->settingsManager , $ this ->clock );
80
+ $ this ->settingsBuilder ->configureCommand ($ this );
102
81
103
- $ this ->addOption (
104
- 'input-format ' ,
105
- null ,
106
- InputOption::VALUE_REQUIRED ,
107
- 'Format of the input can be "RST", or "Markdown" ' ,
108
- );
109
- $ this ->addOption (
110
- 'output-format ' ,
111
- null ,
112
- InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY ,
113
- 'Format of the input can be "html" and/or "interlink" ' ,
114
- );
115
82
$ this ->addOption (
116
83
'log-path ' ,
117
84
null ,
@@ -132,13 +99,6 @@ public function __construct(
132
99
'If set, returns a non-zero exit code as soon as any errors occur ' ,
133
100
);
134
101
135
- $ this ->addOption (
136
- 'theme ' ,
137
- null ,
138
- InputOption::VALUE_REQUIRED ,
139
- 'The theme used for rendering ' ,
140
- );
141
-
142
102
$ this ->addOption (
143
103
'progress ' ,
144
104
null ,
@@ -159,93 +119,11 @@ public function registerProgressBar(ConsoleOutputInterface $output): void
159
119
$ this ->progressBarSubscriber ->subscribe ($ output , $ this ->eventDispatcher );
160
120
}
161
121
162
- private function getSettingsOverriddenWithInput (InputInterface $ input ): ProjectSettings
163
- {
164
- $ settings = $ this ->settingsManager ->getProjectSettings ();
165
-
166
- if ($ settings ->isShowProgressBar ()) {
167
- $ settings ->setShowProgressBar ($ input ->getOption ('progress ' ));
168
- }
169
-
170
- if ($ input ->getArgument ('input ' )) {
171
- $ settings ->setInput ((string ) $ input ->getArgument ('input ' ));
172
- }
173
-
174
- if ($ input ->getOption ('output ' )) {
175
- $ settings ->setOutput ((string ) $ input ->getOption ('output ' ));
176
- }
177
-
178
- if ($ input ->getOption ('input-file ' )) {
179
- $ inputFile = (string ) $ input ->getOption ('input-file ' );
180
- $ pathInfo = pathinfo ($ inputFile );
181
- $ settings ->setInputFile ($ pathInfo ['filename ' ]);
182
- if (!empty ($ pathInfo ['extension ' ])) {
183
- $ settings ->setInputFormat ($ pathInfo ['extension ' ]);
184
- }
185
- }
186
-
187
- if ($ input ->getOption ('input-format ' )) {
188
- $ settings ->setInputFormat ((string ) $ input ->getOption ('input-format ' ));
189
- }
190
-
191
- if ($ input ->getOption ('log-path ' )) {
192
- $ settings ->setLogPath ((string ) $ input ->getOption ('log-path ' ));
193
- }
194
-
195
- if ($ input ->getOption ('fail-on-error ' )) {
196
- $ settings ->setFailOnError (LogLevel::ERROR );
197
- }
198
-
199
- if ($ input ->getOption ('fail-on-log ' )) {
200
- $ settings ->setFailOnError (LogLevel::WARNING );
201
- }
202
-
203
- if (count ($ input ->getOption ('output-format ' )) > 0 ) {
204
- $ settings ->setOutputFormats ($ input ->getOption ('output-format ' ));
205
- }
206
-
207
- if ($ input ->getOption ('theme ' )) {
208
- $ settings ->setTheme ((string ) $ input ->getOption ('theme ' ));
209
- }
210
-
211
- if (method_exists ($ settings , 'setExcludes ' )) {
212
- /** @var list<string> $excludePaths */
213
- $ excludePaths = (array ) $ input ->getOption ('exclude-path ' );
214
- if ($ excludePaths !== []) {
215
- $ settings ->setExcludes (
216
- $ settings ->getExcludes ()->withPaths ($ excludePaths ),
217
- );
218
- }
219
- }
220
-
221
- return $ settings ;
222
- }
223
-
224
122
protected function execute (InputInterface $ input , OutputInterface $ output ): int
225
123
{
226
- $ settings = $ this ->getSettingsOverriddenWithInput ($ input );
227
- $ inputDir = $ settings ->getInput ();
228
- if (!is_dir ($ inputDir )) {
229
- throw new RuntimeException (sprintf ('Input directory "%s" was not found! ' . "\n" .
230
- 'Run "vendor/bin/guides -h" for information on how to configure this command. ' , $ inputDir ));
231
- }
232
-
233
- $ projectNode = new ProjectNode (
234
- $ settings ->getTitle () === '' ? null : $ settings ->getTitle (),
235
- $ settings ->getVersion () === '' ? null : $ settings ->getVersion (),
236
- $ settings ->getRelease () === '' ? null : $ settings ->getRelease (),
237
- $ settings ->getCopyright () === '' ? null : $ settings ->getCopyright (),
238
- $ this ->clock ->now (),
239
- );
240
-
241
- $ event = new PostProjectNodeCreated ($ projectNode , $ settings );
242
- $ event = $ this ->eventDispatcher ->dispatch ($ event );
243
- assert ($ event instanceof PostProjectNodeCreated);
244
- $ projectNode = $ event ->getProjectNode ();
245
- $ settings = $ event ->getSettings ();
246
-
247
- $ outputDir = $ settings ->getOutput ();
248
- $ sourceFileSystem = FlySystemAdapter::createForPath ($ settings ->getInput ());
124
+ $ this ->settingsBuilder ->overrideWithInput ($ input );
125
+ $ projectNode = $ this ->settingsBuilder ->createProjectNode ();
126
+ $ settings = $ this ->settingsBuilder ->getSettings ();
249
127
250
128
$ logPath = $ settings ->getLogPath ();
251
129
if ($ logPath === 'php://stder ' ) {
@@ -260,57 +138,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
260
138
$ this ->logger ->pushProcessor ($ spyProcessor );
261
139
}
262
140
263
- $ documents = [];
264
-
265
-
266
141
if ($ output instanceof ConsoleOutputInterface && $ settings ->isShowProgressBar ()) {
267
142
$ this ->progressBarSubscriber ->subscribe ($ output , $ this ->eventDispatcher );
268
143
}
269
144
270
- if ($ settings ->getInputFile () === '' ) {
271
- $ documents = $ this ->commandBus ->handle (
272
- new ParseDirectoryCommand (
273
- $ sourceFileSystem ,
274
- '' ,
275
- $ settings ->getInputFormat (),
276
- $ projectNode ,
277
- $ this ->getExclude ($ settings , $ input ),
278
- ),
279
- );
280
- } else {
281
- $ documents [] = $ this ->commandBus ->handle (
282
- new ParseFileCommand (
283
- $ sourceFileSystem ,
284
- '' ,
285
- $ settings ->getInputFile (),
286
- $ settings ->getInputFormat (),
287
- 1 ,
288
- $ projectNode ,
289
- true ,
290
- ),
291
- );
292
- }
293
-
294
- $ this ->themeManager ->useTheme ($ settings ->getTheme ());
295
-
296
- $ documents = $ this ->commandBus ->handle (new CompileDocumentsCommand ($ documents , new CompilerContext ($ projectNode )));
297
-
298
- $ destinationFileSystem = FlySystemAdapter::createForPath ($ outputDir );
145
+ $ documents = $ this ->commandBus ->handle (
146
+ new RunCommand ($ settings , $ projectNode , $ input ),
147
+ );
299
148
300
149
$ outputFormats = $ settings ->getOutputFormats ();
301
-
302
- foreach ($ outputFormats as $ format ) {
303
- $ this ->commandBus ->handle (
304
- new RenderCommand (
305
- $ format ,
306
- $ documents ,
307
- $ sourceFileSystem ,
308
- $ destinationFileSystem ,
309
- $ projectNode ,
310
- ),
311
- );
312
- }
313
-
150
+ $ outputDir = $ settings ->getOutput ();
314
151
if ($ output ->isQuiet () === false ) {
315
152
$ lastFormat = '' ;
316
153
@@ -331,31 +168,4 @@ protected function execute(InputInterface $input, OutputInterface $output): int
331
168
332
169
return Command::SUCCESS ;
333
170
}
334
-
335
- private function getExclude (ProjectSettings $ settings , InputInterface |null $ input = null ): Exclude |SpecificationInterface |null
336
- {
337
- if (method_exists ($ settings , 'getExcludes ' )) {
338
- return $ settings ->getExcludes ();
339
- }
340
-
341
- if ($ input === null ) {
342
- return null ;
343
- }
344
-
345
- if ($ input ->getOption ('exclude-path ' )) {
346
- /** @var string[] $excludedPaths */
347
- $ excludedPaths = (array ) $ input ->getOption ('exclude-path ' );
348
- $ excludedSpecifications = array_map (static fn (string $ path ) => new NotSpecification (new InPath (new Path ($ path ))), $ excludedPaths );
349
- $ excludedSpecification = array_shift ($ excludedSpecifications );
350
- assert ($ excludedSpecification !== null );
351
-
352
- return array_reduce (
353
- $ excludedSpecifications ,
354
- static fn (SpecificationInterface $ carry , SpecificationInterface $ spec ) => new OrSpecification ($ carry , $ spec ),
355
- $ excludedSpecification ,
356
- );
357
- }
358
-
359
- return null ;
360
- }
361
171
}
0 commit comments