|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace DDDGenApp\Input\Console\Commands; |
|
5
|
|
|
|
|
6
|
|
|
use DDDGen\Generator; |
|
7
|
|
|
use DDDGen\VO\FQCN; |
|
8
|
|
|
use DDDGen\VO\Layer; |
|
9
|
|
|
use DDDGen\VO\Primitive; |
|
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
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
16
|
|
|
|
|
17
|
|
|
final class Generate extends Command |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var Generator */ |
|
20
|
|
|
private $generator; |
|
21
|
|
|
|
|
22
|
1 |
|
protected function configure() |
|
23
|
|
|
{ |
|
24
|
|
|
$this |
|
25
|
1 |
|
->setName("generate") |
|
26
|
1 |
|
->setDescription("Generates new primitive from stub files") |
|
27
|
1 |
|
->setHelp("This command allows you to generate new commands, events, queries and other primitives from prepared stub files.") |
|
28
|
1 |
|
->addArgument('layer', InputArgument::REQUIRED, 'Layer of the primitive - app, domain or infrastructure') |
|
29
|
1 |
|
->addArgument('primitive_name', InputArgument::REQUIRED, |
|
30
|
1 |
|
'Name of predefined primitive - command, query, event or other') |
|
31
|
1 |
|
->addArgument('psr4_name', InputArgument::REQUIRED, |
|
32
|
1 |
|
'PSR-4 name of the primitive. This one will be used as namespace as well as path to file') |
|
33
|
1 |
|
->addOption('silent', 'y', InputOption::VALUE_NONE, 'Hide confirmation of generated files') |
|
34
|
1 |
|
->addOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use this path to config file'); |
|
35
|
1 |
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
38
|
|
|
{ |
|
39
|
1 |
|
$this->initGenerator($input, $output); |
|
40
|
|
|
|
|
41
|
1 |
|
$layer = $input->getArgument('layer'); |
|
42
|
1 |
|
$primitive_name = $input->getArgument('primitive_name'); |
|
43
|
1 |
|
$psr4_name = $input->getArgument('psr4_name'); |
|
44
|
1 |
|
$silent = $input->getOption('silent'); |
|
45
|
|
|
|
|
46
|
1 |
|
if (!$silent) { |
|
47
|
|
|
$final_files = $this->generator->generateDry( |
|
48
|
|
|
$layer, |
|
49
|
|
|
$primitive_name, |
|
50
|
|
|
FQCN::fromString($psr4_name) |
|
51
|
|
|
); |
|
52
|
|
|
$src_base = $this->generator->getLayerByName($layer)->getSrcDir(); |
|
53
|
|
|
$tests_base = $this->generator->getLayerByName($layer)->getTestsDir(); |
|
54
|
|
|
|
|
55
|
|
|
$output->writeln("============ BASE PATHES ====================="); |
|
56
|
|
|
$output->writeln("[SRC] = " . $src_base); |
|
57
|
|
|
$output->writeln("[TEST] = " . $tests_base); |
|
58
|
|
|
$output->writeln("============ FINAL FILES ====================="); |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
foreach ($final_files as $file => $stub) { |
|
62
|
|
|
// shorten the path to show |
|
63
|
|
|
if (strstr($file, $src_base)) { |
|
64
|
|
|
$file = str_replace($src_base, "", $file); |
|
65
|
|
|
$output->writeln("[SRC]" . $file); |
|
66
|
|
|
} else { |
|
67
|
|
|
$file = str_replace($tests_base, "", $file); |
|
68
|
|
|
$output->writeln("[TEST]" . $file); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$helper = $this->getHelper('question'); |
|
73
|
|
|
$question = new ConfirmationQuestion("Confirm and create these files? [y/n]: ", false); |
|
74
|
|
|
if (!$helper->ask($input, $output, $question)) { |
|
75
|
|
|
$output->writeln("Cancelled."); |
|
76
|
|
|
|
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
$this->generator->generate( |
|
83
|
|
|
$layer, |
|
84
|
|
|
$primitive_name, |
|
85
|
1 |
|
FQCN::fromString($psr4_name) |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
1 |
|
$output->writeln('Ok, done!'); |
|
89
|
|
|
|
|
90
|
1 |
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
private function initGenerator(InputInterface $input, OutputInterface $output) |
|
93
|
|
|
{ |
|
94
|
1 |
|
$config_path = $input->getOption('config'); |
|
95
|
|
|
|
|
96
|
|
|
// TODO use DI for this initialization |
|
97
|
1 |
|
if (!$config_path) { |
|
98
|
|
|
$config_path = __DIR__ . "/../../../../../config/config.php"; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
$config = require($config_path); |
|
102
|
|
|
|
|
103
|
1 |
|
if (!is_array($config)) { |
|
104
|
|
|
throw new \Exception("It looks like your config file did not returned an array. Did you forget to add return statement?"); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
$primitives = []; |
|
108
|
1 |
|
foreach ($config['primitives'] as $name => $primitive_config) { |
|
109
|
1 |
|
$primitives[] = new Primitive( |
|
110
|
|
|
$name, |
|
111
|
1 |
|
$primitive_config['src']['stubs'], |
|
112
|
1 |
|
$primitive_config['test']['stubs'] |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
$layers = []; |
|
117
|
1 |
|
foreach ($config['layers'] as $layer_name => $layer_config) { |
|
118
|
1 |
|
$layers[] = new Layer( |
|
119
|
|
|
$layer_name, |
|
120
|
1 |
|
new FQCN($layer_config['src']['qcn']), |
|
121
|
1 |
|
$layer_config['src']['dir'], |
|
122
|
1 |
|
new FQCN($layer_config['tests']['qcn']), |
|
123
|
1 |
|
$layer_config['tests']['dir'] |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
$generator = new Generator( |
|
128
|
|
|
$layers, |
|
129
|
|
|
$primitives |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
1 |
|
$this->generator = $generator; |
|
133
|
|
|
} |
|
134
|
|
|
} |