1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Console\Infrastructure\Command; |
6
|
|
|
|
7
|
|
|
use Gacela\Console\ConsoleFacade; |
8
|
|
|
use Gacela\Console\Domain\FilenameSanitizer\FilenameSanitizer; |
9
|
|
|
use Gacela\Framework\DocBlockResolverAwareTrait; |
10
|
|
|
use Symfony\Component\Console\Command\Command; |
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @method ConsoleFacade getFacade() |
18
|
|
|
*/ |
19
|
|
|
final class MakeFileCommand extends Command |
20
|
|
|
{ |
21
|
|
|
use DocBlockResolverAwareTrait; |
22
|
|
|
|
23
|
8 |
|
protected function configure(): void |
24
|
|
|
{ |
25
|
8 |
|
$this->setName('make:file') |
26
|
8 |
|
->setDescription('Generate a ' . $this->getExpectedFilenames()) |
27
|
8 |
|
->addArgument('path', InputArgument::REQUIRED, 'The file path. For example "App/TestModule/TestSubModule"') |
28
|
8 |
|
->addArgument('filenames', InputArgument::REQUIRED | InputArgument::IS_ARRAY, $this->getExpectedFilenames()) |
29
|
8 |
|
->addOption('short-name', 's', InputOption::VALUE_NONE, 'Remove module prefix to the class name'); |
30
|
|
|
} |
31
|
|
|
|
32
|
8 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
33
|
|
|
{ |
34
|
|
|
/** @var list<string> $inputFileNames */ |
35
|
8 |
|
$inputFileNames = $input->getArgument('filenames'); |
36
|
|
|
|
37
|
8 |
|
$filenames = array_map( |
38
|
8 |
|
fn (string $raw): string => $this->getFacade()->sanitizeFilename($raw), |
39
|
|
|
$inputFileNames |
|
|
|
|
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
/** @var string $path */ |
43
|
8 |
|
$path = $input->getArgument('path'); |
44
|
8 |
|
$commandArguments = $this->getFacade()->parseArguments($path); |
45
|
8 |
|
$shortName = (bool)$input->getOption('short-name'); |
46
|
|
|
|
47
|
8 |
|
foreach ($filenames as $filename) { |
48
|
8 |
|
$absolutePath = $this->getFacade()->generateFileContent( |
49
|
|
|
$commandArguments, |
50
|
|
|
$filename, |
51
|
|
|
$shortName |
52
|
|
|
); |
53
|
8 |
|
$output->writeln("> Path '{$absolutePath}' created successfully"); |
54
|
|
|
} |
55
|
|
|
|
56
|
8 |
|
return self::SUCCESS; |
57
|
|
|
} |
58
|
|
|
|
59
|
8 |
|
private function getExpectedFilenames(): string |
60
|
|
|
{ |
61
|
8 |
|
return implode(', ', FilenameSanitizer::EXPECTED_FILENAMES); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|