1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\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 = 'd'; |
21
|
|
|
public const DEFINITION_TYPE = 'The data type of the property you want to generate'; |
22
|
|
|
|
23
|
|
|
public const OPT_NOT_NULLABLE = 'not-nullable'; |
24
|
|
|
public const OPT_NOT_NULLABLE_SHORT = 'z'; |
25
|
|
|
public const DEFINITION_NOT_NULLABLE = 'This field will not be nullable'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var FieldGenerator |
29
|
|
|
*/ |
30
|
|
|
protected $fieldGenerator; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* GenerateEntityCommand constructor. |
34
|
|
|
* |
35
|
|
|
* @param FieldGenerator $fieldGenerator |
36
|
|
|
* @param NamespaceHelper $namespaceHelper |
37
|
|
|
* @param null|string $name |
38
|
|
|
* |
39
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
40
|
|
|
*/ |
41
|
|
|
public function __construct( |
42
|
|
|
FieldGenerator $fieldGenerator, |
43
|
|
|
NamespaceHelper $namespaceHelper, |
44
|
|
|
?string $name = null |
45
|
|
|
) { |
46
|
|
|
parent::__construct($namespaceHelper, $name); |
47
|
|
|
$this->fieldGenerator = $fieldGenerator; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @throws DoctrineStaticMetaException |
52
|
|
|
*/ |
53
|
|
|
protected function configure(): void |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
|
|
$this |
57
|
|
|
->setName(AbstractCommand::COMMAND_PREFIX.'generate:field') |
58
|
|
|
->setDefinition( |
59
|
|
|
[ |
60
|
|
|
new InputOption( |
61
|
|
|
self::OPT_FQN, |
62
|
|
|
self::OPT_FQN_SHORT, |
63
|
|
|
InputOption::VALUE_REQUIRED, |
64
|
|
|
self::DEFINITION_NAME |
65
|
|
|
), |
66
|
|
|
new InputOption( |
67
|
|
|
self::OPT_TYPE, |
68
|
|
|
self::OPT_TYPE_SHORT, |
69
|
|
|
InputOption::VALUE_REQUIRED, |
70
|
|
|
self::DEFINITION_TYPE |
71
|
|
|
), |
72
|
|
|
new InputOption( |
73
|
|
|
self::OPT_NOT_NULLABLE, |
74
|
|
|
self::OPT_NOT_NULLABLE_SHORT, |
75
|
|
|
InputOption::VALUE_NONE, |
76
|
|
|
self::DEFINITION_NOT_NULLABLE |
77
|
|
|
), |
78
|
|
|
$this->getProjectRootPathOption(), |
79
|
|
|
$this->getProjectRootNamespaceOption(), |
80
|
|
|
$this->getSrcSubfolderOption(), |
81
|
|
|
$this->getTestSubFolderOption() |
82
|
|
|
] |
83
|
|
|
)->setDescription( |
84
|
|
|
'Generate a field' |
85
|
|
|
); |
86
|
|
|
} catch (\Exception $e) { |
87
|
|
|
throw new DoctrineStaticMetaException('Exception in '.__METHOD__.': '.$e->getMessage(), $e->getCode(), $e); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param InputInterface $input |
94
|
|
|
* @param OutputInterface $output |
95
|
|
|
* |
96
|
|
|
* @throws DoctrineStaticMetaException |
97
|
|
|
*/ |
98
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void |
99
|
|
|
{ |
100
|
|
|
try { |
101
|
|
|
$this->checkOptions($input); |
102
|
|
|
|
103
|
|
|
$output->writeln( |
104
|
|
|
'<comment>Starting generation for '.$input->getOption(self::OPT_FQN).'</comment>' |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$this->fieldGenerator |
108
|
|
|
->setPathToProjectRoot($input->getOption(AbstractCommand::OPT_PROJECT_ROOT_PATH)) |
109
|
|
|
->setProjectRootNamespace($input->getOption(AbstractCommand::OPT_PROJECT_ROOT_NAMESPACE)) |
110
|
|
|
->setTestSubFolderName($input->getOption(AbstractCommand::OPT_TEST_SUBFOLDER)) |
111
|
|
|
->setIsNullable(! (bool)$input->getOption(self::OPT_NOT_NULLABLE)); |
112
|
|
|
|
113
|
|
|
$this->fieldGenerator->generateField( |
114
|
|
|
$input->getOption(self::OPT_FQN), |
115
|
|
|
$input->getOption(self::OPT_TYPE) |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
$output->writeln('<info>completed</info>'); |
119
|
|
|
} catch (\Exception $e) { |
120
|
|
|
throw new DoctrineStaticMetaException('Exception in '.__METHOD__.': '.$e->getMessage(), $e->getCode(), $e); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|