1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\FieldGenerator; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
class GenerateFieldCommand extends AbstractCommand |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
public const OPT_FQN = 'field-fully-qualified-name'; |
16
|
|
|
public const OPT_FQN_SHORT = 'f'; |
17
|
|
|
public const DEFINITION_NAME = 'The fully qualified name of the property you want to generate'; |
18
|
|
|
|
19
|
|
|
public const OPT_TYPE = 'field-property-doctrine-type'; |
20
|
|
|
public const OPT_TYPE_SHORT = 'y'; |
21
|
|
|
public const DEFINITION_TYPE = 'The data type of the property you want to generate'; |
22
|
|
|
|
23
|
|
|
public const OPT_DEFAULT_VALUE = 'default'; |
24
|
|
|
public const OPT_DEFAULT_VALUE_SHORT = 'd'; |
25
|
|
|
public const DEFINITION_DEFAULT_VALUE = 'The default value, defaults to null ' |
26
|
|
|
. '(which also marks the field as nullable)'; |
27
|
|
|
|
28
|
|
|
public const OPT_IS_UNIQUE = 'is-unique'; |
29
|
|
|
public const OPT_IS_UNIQUE_SHORT = 'u'; |
30
|
|
|
public const DEFINITION_IS_UNIQUE = 'This field is unique, duplicates are not allowed'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var FieldGenerator |
34
|
|
|
*/ |
35
|
|
|
protected $fieldGenerator; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* GenerateEntityCommand constructor. |
39
|
|
|
* |
40
|
|
|
* @param \EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\FieldGenerator $fieldSetterGenerator |
41
|
|
|
* @param null|string $name |
42
|
|
|
* |
43
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
FieldGenerator $fieldSetterGenerator, |
47
|
|
|
?string $name = null |
48
|
|
|
) { |
49
|
|
|
parent::__construct($name); |
50
|
|
|
$this->fieldGenerator = $fieldSetterGenerator; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @throws DoctrineStaticMetaException |
55
|
|
|
*/ |
56
|
|
|
protected function configure(): void |
57
|
|
|
{ |
58
|
|
|
try { |
59
|
|
|
$this |
60
|
|
|
->setName(AbstractCommand::COMMAND_PREFIX . 'generate:field') |
61
|
|
|
->setDefinition( |
62
|
|
|
[ |
63
|
|
|
new InputOption( |
64
|
|
|
self::OPT_FQN, |
65
|
|
|
self::OPT_FQN_SHORT, |
66
|
|
|
InputOption::VALUE_REQUIRED, |
67
|
|
|
self::DEFINITION_NAME |
68
|
|
|
), |
69
|
|
|
new InputOption( |
70
|
|
|
self::OPT_TYPE, |
71
|
|
|
self::OPT_TYPE_SHORT, |
72
|
|
|
InputOption::VALUE_REQUIRED, |
73
|
|
|
self::DEFINITION_TYPE |
74
|
|
|
), |
75
|
|
|
new InputOption( |
76
|
|
|
self::OPT_DEFAULT_VALUE, |
77
|
|
|
self::OPT_DEFAULT_VALUE_SHORT, |
78
|
|
|
InputOption::VALUE_OPTIONAL, |
79
|
|
|
self::DEFINITION_DEFAULT_VALUE, |
80
|
|
|
null |
81
|
|
|
), |
82
|
|
|
new InputOption( |
83
|
|
|
self::OPT_IS_UNIQUE, |
84
|
|
|
self::OPT_IS_UNIQUE_SHORT, |
85
|
|
|
InputOption::VALUE_NONE, |
86
|
|
|
self::DEFINITION_IS_UNIQUE |
87
|
|
|
), |
88
|
|
|
$this->getProjectRootPathOption(), |
89
|
|
|
$this->getProjectRootNamespaceOption(), |
90
|
|
|
$this->getSrcSubfolderOption(), |
91
|
|
|
$this->getTestSubFolderOption(), |
92
|
|
|
] |
93
|
|
|
)->setDescription( |
94
|
|
|
'Generate a field' |
95
|
|
|
); |
96
|
|
|
} catch (\Exception $e) { |
97
|
|
|
throw new DoctrineStaticMetaException( |
98
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
99
|
|
|
$e->getCode(), |
100
|
|
|
$e |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param InputInterface $input |
108
|
|
|
* @param OutputInterface $output |
109
|
|
|
* |
110
|
|
|
* @throws DoctrineStaticMetaException |
111
|
|
|
*/ |
112
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void |
113
|
|
|
{ |
114
|
|
|
try { |
115
|
|
|
$this->checkOptions($input); |
116
|
|
|
|
117
|
|
|
$output->writeln( |
118
|
|
|
'<comment>Starting generation for ' . $input->getOption(self::OPT_FQN) . '</comment>' |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
$this->fieldGenerator |
122
|
|
|
->setPathToProjectRoot($input->getOption(AbstractCommand::OPT_PROJECT_ROOT_PATH)) |
123
|
|
|
->setProjectRootNamespace($input->getOption(AbstractCommand::OPT_PROJECT_ROOT_NAMESPACE)) |
124
|
|
|
->setTestSubFolderName($input->getOption(AbstractCommand::OPT_TEST_SUBFOLDER)); |
125
|
|
|
|
126
|
|
|
$this->fieldGenerator->generateField( |
127
|
|
|
$input->getOption(self::OPT_FQN), |
128
|
|
|
$input->getOption(self::OPT_TYPE), |
129
|
|
|
null, |
130
|
|
|
$input->getOption(self::OPT_DEFAULT_VALUE) ?? null, |
131
|
|
|
$input->getOption(self::OPT_IS_UNIQUE) |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
$output->writeln('<info>completed</info>'); |
135
|
|
|
} catch (\Exception $e) { |
136
|
|
|
throw new DoctrineStaticMetaException( |
137
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
138
|
|
|
$e->getCode(), |
139
|
|
|
$e |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|