|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\EntityGenerator; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\IdTrait; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
class GenerateEntityCommand extends AbstractCommand |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
public const OPT_FQN = 'entity-fully-qualified-name'; |
|
17
|
|
|
public const OPT_FQN_SHORT = 'f'; |
|
18
|
|
|
public const DEFINITION_FQN = 'The fully qualified name of the entity you want to create'; |
|
19
|
|
|
|
|
20
|
|
|
public const OPT_INT_PRIMARY_KEY = 'int-primary-key'; |
|
21
|
|
|
public const OPT_INT_PRIMARY_KEY_SHORT = 'd'; |
|
22
|
|
|
public const DEFINITION_UUID = 'Use an Integer primary key in place of the standard UUID primary key'; |
|
23
|
|
|
|
|
24
|
|
|
public const OPT_ENTITY_SPECIFIC_SAVER = 'entity-specific-saver'; |
|
25
|
|
|
public const OPT_ENTITY_SPECIFIC_SAVER_SHORT = 'c'; |
|
26
|
|
|
public const DEFINITION_ENTITY_SPECIFIC_SAVER = 'Generate an implmentation of SaverInterface just for this entity'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var EntityGenerator |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $entityGenerator; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* GenerateEntityCommand constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param EntityGenerator $relationsGenerator |
|
37
|
|
|
* @param null|string $name |
|
38
|
|
|
* |
|
39
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct( |
|
42
|
|
|
EntityGenerator $relationsGenerator, |
|
43
|
|
|
?string $name = null |
|
44
|
|
|
) { |
|
45
|
|
|
parent::__construct($name); |
|
46
|
|
|
$this->entityGenerator = $relationsGenerator; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @throws DoctrineStaticMetaException |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function configure(): void |
|
53
|
|
|
{ |
|
54
|
|
|
try { |
|
55
|
|
|
$this |
|
56
|
|
|
->setName(AbstractCommand::COMMAND_PREFIX . 'generate:entity') |
|
57
|
|
|
->setDefinition( |
|
58
|
|
|
[ |
|
59
|
|
|
new InputOption( |
|
60
|
|
|
self::OPT_FQN, |
|
61
|
|
|
self::OPT_FQN_SHORT, |
|
62
|
|
|
InputOption::VALUE_REQUIRED, |
|
63
|
|
|
self::DEFINITION_FQN |
|
64
|
|
|
), |
|
65
|
|
|
new InputOption( |
|
66
|
|
|
self::OPT_INT_PRIMARY_KEY, |
|
67
|
|
|
self::OPT_INT_PRIMARY_KEY_SHORT, |
|
68
|
|
|
InputOption::VALUE_NONE, |
|
69
|
|
|
self::DEFINITION_UUID |
|
70
|
|
|
), |
|
71
|
|
|
new InputOption( |
|
72
|
|
|
self::OPT_ENTITY_SPECIFIC_SAVER, |
|
73
|
|
|
self::OPT_ENTITY_SPECIFIC_SAVER_SHORT, |
|
74
|
|
|
InputOption::VALUE_NONE, |
|
75
|
|
|
self::DEFINITION_ENTITY_SPECIFIC_SAVER |
|
76
|
|
|
), |
|
77
|
|
|
$this->getProjectRootPathOption(), |
|
78
|
|
|
$this->getProjectRootNamespaceOption(), |
|
79
|
|
|
$this->getSrcSubfolderOption(), |
|
80
|
|
|
] |
|
81
|
|
|
)->setDescription( |
|
82
|
|
|
'Generate an Entity' |
|
83
|
|
|
); |
|
84
|
|
|
} catch (\Exception $e) { |
|
85
|
|
|
throw new DoctrineStaticMetaException( |
|
86
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
|
87
|
|
|
$e->getCode(), |
|
88
|
|
|
$e |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param InputInterface $input |
|
96
|
|
|
* @param OutputInterface $output |
|
97
|
|
|
* |
|
98
|
|
|
* @throws DoctrineStaticMetaException |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void |
|
101
|
|
|
{ |
|
102
|
|
|
try { |
|
103
|
|
|
$this->checkOptions($input); |
|
104
|
|
|
$output->writeln( |
|
105
|
|
|
'<comment>Starting generation for ' . $input->getOption(self::OPT_FQN) . '</comment>' |
|
106
|
|
|
); |
|
107
|
|
|
$idType = |
|
108
|
|
|
(true !== $input->getOption(self::OPT_INT_PRIMARY_KEY)) ? |
|
109
|
|
|
IdTrait::UUID_FIELD_TRAIT : IdTrait::INTEGER_ID_FIELD_TRAIT; |
|
110
|
|
|
$this->entityGenerator |
|
|
|
|
|
|
111
|
|
|
->setPathToProjectRoot($input->getOption(AbstractCommand::OPT_PROJECT_ROOT_PATH)) |
|
112
|
|
|
->setProjectRootNamespace($input->getOption(AbstractCommand::OPT_PROJECT_ROOT_NAMESPACE)) |
|
113
|
|
|
->setPrimaryKeyType($idType); |
|
114
|
|
|
$this->entityGenerator->generateEntity( |
|
115
|
|
|
$input->getOption(self::OPT_FQN), |
|
116
|
|
|
$input->getOption(self::OPT_ENTITY_SPECIFIC_SAVER) |
|
117
|
|
|
); |
|
118
|
|
|
$output->writeln('<info>completed</info>'); |
|
119
|
|
|
} catch (\Exception $e) { |
|
120
|
|
|
throw new DoctrineStaticMetaException( |
|
121
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
|
122
|
|
|
$e->getCode(), |
|
123
|
|
|
$e |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|