1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
10
|
|
|
use Symfony\Component\Console\Command\Command; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
|
14
|
|
|
abstract class AbstractCommand extends Command |
15
|
|
|
{ |
16
|
|
|
public const COMMAND_PREFIX = 'dsm:'; |
17
|
|
|
|
18
|
|
|
public const OPT_PROJECT_ROOT_NAMESPACE = 'project-root-namespace'; |
19
|
|
|
public const OPT_PROJECT_ROOT_NAMESPACE_SHORT = 'r'; |
20
|
|
|
public const DEFINITION_PROJECT_ROOT_NAMESPACE = 'The root namespace for the project for which you are building' |
21
|
|
|
. ' entities. The entities root namespace is suffixed to the ' |
22
|
|
|
. 'end of this'; |
23
|
|
|
public const DEFAULT_PROJECT_ROOT_NAMESPACE_METHOD = 'getProjectRootNamespace'; |
24
|
|
|
|
25
|
|
|
public const OPT_PROJECT_ROOT_PATH = 'project-root-path'; |
26
|
|
|
public const OPT_PROJECT_ROOT_PATH_SHORT = 'p'; |
27
|
|
|
public const DEFINITION_PROJECT_ROOT_PATH = 'the filesystem path to the folder for the project. ' |
28
|
|
|
. 'This would be the folder that generally has a subfolder `src` ' |
29
|
|
|
. 'and a sub folder `tests`'; |
30
|
|
|
public const DEFAULT_PROJECT_ROOT_PATH_METHOD = 'getProjectRootPath'; |
31
|
|
|
|
32
|
|
|
public const OPT_SRC_SUBFOLDER = 'src-sub-folder'; |
33
|
|
|
public const OPT_SRC_SUBFOLDER_SHORT = 's'; |
34
|
|
|
public const DEFINITION_SRC_SUBFOLDER = 'The name of the subdfolder that contains sources. ' |
35
|
|
|
. 'Generally this is `src` which is the default'; |
36
|
|
|
public const DEFAULT_SRC_SUBFOLDER = 'src'; |
37
|
|
|
|
38
|
|
|
public const OPT_TEST_SUBFOLDER = 'test-sub-folder'; |
39
|
|
|
public const OPT_TEST_SUBFOLDER_SHORT = 't'; |
40
|
|
|
public const DEFINITION_TEST_SUBFOLDER = 'The name of the subdfolder that contains tests. ' |
41
|
|
|
. 'Generally this is `tests` which is the default'; |
42
|
|
|
public const DEFAULT_TEST_SUBFOLDER = 'tests'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var NamespaceHelper |
46
|
|
|
*/ |
47
|
|
|
protected $namespaceHelper; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* AbstractCommand constructor. |
51
|
|
|
* |
52
|
|
|
* @param NamespaceHelper $namespaceHelper |
53
|
|
|
* @param null|string $name |
54
|
|
|
* |
55
|
|
|
* @throws DoctrineStaticMetaException |
56
|
12 |
|
*/ |
57
|
|
|
public function __construct(NamespaceHelper $namespaceHelper, ?string $name = null) |
58
|
12 |
|
{ |
59
|
|
|
$this->namespaceHelper = $namespaceHelper; |
60
|
12 |
|
try { |
61
|
|
|
parent::__construct($name); |
62
|
|
|
} catch (\Exception $e) { |
63
|
|
|
throw new DoctrineStaticMetaException( |
64
|
|
|
'Exception in ' . __METHOD__, |
65
|
|
|
$e->getCode(), |
66
|
|
|
$e |
67
|
|
|
); |
68
|
12 |
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return EntityManagerInterface |
74
|
|
|
* @throws DoctrineStaticMetaException |
75
|
1 |
|
*/ |
76
|
|
|
protected function getEntityManager(): EntityManagerInterface |
77
|
|
|
{ |
78
|
1 |
|
try { |
79
|
|
|
return $this->getHelper('em')->getEntityManager(); |
80
|
|
|
} catch (\Exception $e) { |
81
|
|
|
throw new DoctrineStaticMetaException( |
82
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
83
|
|
|
$e->getCode(), |
84
|
|
|
$e |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param InputInterface $input |
91
|
|
|
* |
92
|
|
|
* @throws DoctrineStaticMetaException |
93
|
11 |
|
*/ |
94
|
|
|
protected function checkOptions(InputInterface $input): void |
95
|
11 |
|
{ |
96
|
11 |
|
$errors = []; |
97
|
11 |
|
$options = $this->getDefinition()->getOptions(); |
98
|
11 |
|
foreach ($options as $option) { |
99
|
11 |
|
$name = $option->getName(); |
100
|
11 |
|
$value = $input->getOption($name); |
101
|
11 |
|
$this->checkOptionRequired($option, $value, $name, $errors); |
102
|
1 |
|
if (\is_array($value)) { |
103
|
|
|
foreach ($value as $v) { |
104
|
|
|
$this->checkValueForEquals($v, $name, $errors); |
105
|
1 |
|
} |
106
|
|
|
continue; |
107
|
11 |
|
} |
108
|
|
|
$this->checkValueForEquals($value, $name, $errors); |
109
|
11 |
|
} |
110
|
|
|
if (count($errors) > 0) { |
111
|
|
|
throw new DoctrineStaticMetaException(implode("\n\n", $errors)); |
112
|
11 |
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param InputOption $option |
117
|
|
|
* @param mixed $value |
118
|
|
|
* @param string $name |
119
|
|
|
* @param array $errors |
120
|
11 |
|
*/ |
121
|
|
|
protected function checkOptionRequired(InputOption $option, $value, string $name, array &$errors): void |
122
|
11 |
|
{ |
123
|
11 |
|
if ($option->isValueRequired() && ( |
124
|
11 |
|
$value === null |
125
|
11 |
|
|| $value === '' |
126
|
|
|
|| ($option->isArray() && $value === []) |
127
|
|
|
) |
128
|
|
|
) { |
129
|
|
|
$errors[] = sprintf('The required option --%s is not set or is empty', $name); |
130
|
11 |
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param mixed $value |
135
|
|
|
* @param string $name |
136
|
|
|
* @param array $errors |
137
|
11 |
|
*/ |
138
|
|
|
protected function checkValueForEquals($value, string $name, array &$errors): void |
139
|
11 |
|
{ |
140
|
|
|
if (\is_string($value) && '' !== $value && \ts\stringStartsWith($value, '=')) { |
141
|
|
|
$errors[] = 'Value for ' . $name . ' is ' . $value |
142
|
|
|
. ' and starts with =, if use short options, you should not use an = sign'; |
143
|
11 |
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Getter for self::DEFAULT_PROJECT_ROOT_PATH |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
* @throws DoctrineStaticMetaException |
151
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
152
|
11 |
|
*/ |
153
|
|
|
protected function getProjectRootPath(): string |
154
|
|
|
{ |
155
|
11 |
|
try { |
156
|
|
|
return Config::getProjectRootDirectory(); |
157
|
|
|
} catch (\Exception $e) { |
158
|
|
|
throw new DoctrineStaticMetaException( |
159
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
160
|
|
|
$e->getCode(), |
161
|
|
|
$e |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $dirForNamespace |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
* @throws DoctrineStaticMetaException |
171
|
11 |
|
*/ |
172
|
|
|
protected function getProjectRootNamespace(string $dirForNamespace = 'src'): string |
173
|
11 |
|
{ |
174
|
|
|
return (new NamespaceHelper())->getProjectRootNamespaceFromComposerJson($dirForNamespace); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return InputOption |
179
|
|
|
* @throws DoctrineStaticMetaException |
180
|
11 |
|
*/ |
181
|
|
|
protected function getProjectRootPathOption(): InputOption |
182
|
|
|
{ |
183
|
11 |
|
try { |
184
|
11 |
|
return new InputOption( |
185
|
11 |
|
self::OPT_PROJECT_ROOT_PATH, |
186
|
11 |
|
self::OPT_PROJECT_ROOT_PATH_SHORT, |
187
|
11 |
|
InputOption::VALUE_OPTIONAL, |
188
|
11 |
|
self::DEFINITION_PROJECT_ROOT_PATH, |
189
|
|
|
$this->{self::DEFAULT_PROJECT_ROOT_PATH_METHOD}() |
190
|
|
|
); |
191
|
|
|
} catch (\Exception $e) { |
192
|
|
|
throw new DoctrineStaticMetaException('Exception getting option', $e->getCode(), $e); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return InputOption |
198
|
|
|
* @throws DoctrineStaticMetaException |
199
|
11 |
|
*/ |
200
|
|
|
protected function getProjectRootNamespaceOption(): InputOption |
201
|
|
|
{ |
202
|
11 |
|
try { |
203
|
11 |
|
return new InputOption( |
204
|
11 |
|
self::OPT_PROJECT_ROOT_NAMESPACE, |
205
|
11 |
|
self::OPT_PROJECT_ROOT_NAMESPACE_SHORT, |
206
|
11 |
|
InputOption::VALUE_REQUIRED, |
207
|
11 |
|
self::DEFINITION_PROJECT_ROOT_NAMESPACE, |
208
|
|
|
$this->{self::DEFAULT_PROJECT_ROOT_NAMESPACE_METHOD}() |
209
|
|
|
); |
210
|
|
|
} catch (\Exception $e) { |
211
|
|
|
throw new DoctrineStaticMetaException('Exception getting option', $e->getCode(), $e); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return InputOption |
218
|
|
|
* @throws DoctrineStaticMetaException |
219
|
9 |
|
*/ |
220
|
|
|
protected function getSrcSubfolderOption(): InputOption |
221
|
|
|
{ |
222
|
9 |
|
try { |
223
|
9 |
|
return new InputOption( |
224
|
9 |
|
self::OPT_SRC_SUBFOLDER, |
225
|
9 |
|
self::OPT_SRC_SUBFOLDER_SHORT, |
226
|
9 |
|
InputOption::VALUE_REQUIRED, |
227
|
9 |
|
self::DEFINITION_SRC_SUBFOLDER, |
228
|
|
|
self::DEFAULT_SRC_SUBFOLDER |
229
|
|
|
); |
230
|
|
|
} catch (\Exception $e) { |
231
|
|
|
throw new DoctrineStaticMetaException('Exception getting option', $e->getCode(), $e); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @return InputOption |
237
|
|
|
* @throws DoctrineStaticMetaException |
238
|
5 |
|
*/ |
239
|
|
|
protected function getTestSubFolderOption(): InputOption |
240
|
|
|
{ |
241
|
5 |
|
try { |
242
|
5 |
|
return new InputOption( |
243
|
5 |
|
self::OPT_TEST_SUBFOLDER, |
244
|
5 |
|
self::OPT_TEST_SUBFOLDER_SHORT, |
245
|
5 |
|
InputOption::VALUE_REQUIRED, |
246
|
5 |
|
self::DEFINITION_TEST_SUBFOLDER, |
247
|
|
|
self::DEFAULT_TEST_SUBFOLDER |
248
|
|
|
); |
249
|
|
|
} catch (\Exception $e) { |
250
|
|
|
throw new DoctrineStaticMetaException('Exception getting option', $e->getCode(), $e); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|