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