1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\EntityFieldSetter; |
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 SetFieldCommand extends AbstractCommand |
13
|
|
|
{ |
14
|
|
|
public const OPT_ENTITY = 'entity'; |
15
|
|
|
public const OPT_ENTITY_SHORT = 'o'; |
16
|
|
|
|
17
|
|
|
public const OPT_FIELD = 'field'; |
18
|
|
|
public const OPT_FIELD_SHORT = 't'; |
19
|
|
|
/** |
20
|
|
|
* @var \EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\EntityFieldSetter |
21
|
|
|
*/ |
22
|
|
|
protected $entityFieldSetter; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* SetFieldCommand constructor. |
26
|
|
|
* |
27
|
|
|
* @param \EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\EntityFieldSetter $entityFieldSetter |
28
|
|
|
* @param NamespaceHelper $namespaceHelper |
29
|
|
|
* @param null|string $name |
30
|
|
|
* |
31
|
|
|
* @throws DoctrineStaticMetaException |
32
|
|
|
*/ |
33
|
2 |
|
public function __construct( |
34
|
|
|
EntityFieldSetter $entityFieldSetter, |
35
|
|
|
NamespaceHelper $namespaceHelper, |
36
|
|
|
?string $name = null |
37
|
|
|
) { |
38
|
2 |
|
parent::__construct($namespaceHelper, $name); |
39
|
2 |
|
$this->entityFieldSetter = $entityFieldSetter; |
40
|
2 |
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @throws DoctrineStaticMetaException |
45
|
|
|
*/ |
46
|
2 |
|
public function configure(): void |
47
|
|
|
{ |
48
|
|
|
try { |
49
|
2 |
|
$this->setName(AbstractCommand::COMMAND_PREFIX.'set:field') |
50
|
2 |
|
->setDefinition( |
51
|
|
|
[ |
52
|
2 |
|
new InputOption( |
53
|
2 |
|
self::OPT_ENTITY, |
54
|
2 |
|
self::OPT_ENTITY_SHORT, |
55
|
2 |
|
InputOption::VALUE_REQUIRED, |
56
|
2 |
|
'Entity Fully Qualified Name' |
57
|
|
|
), |
58
|
2 |
|
new InputOption( |
59
|
2 |
|
self::OPT_FIELD, |
60
|
2 |
|
self::OPT_FIELD_SHORT, |
61
|
2 |
|
InputOption::VALUE_REQUIRED, |
62
|
2 |
|
'Field Fully Qualified Name' |
63
|
|
|
), |
64
|
2 |
|
$this->getProjectRootPathOption(), |
65
|
2 |
|
$this->getProjectRootNamespaceOption(), |
66
|
2 |
|
$this->getSrcSubfolderOption(), |
67
|
|
|
] |
68
|
2 |
|
)->setDescription( |
69
|
2 |
|
'Set an Entity as having a Field' |
70
|
|
|
); |
71
|
|
|
} catch (\Exception $e) { |
72
|
|
|
throw new DoctrineStaticMetaException( |
73
|
|
|
'Exception in '.__METHOD__.': '.$e->getMessage(), |
74
|
|
|
$e->getCode(), |
75
|
|
|
$e |
76
|
|
|
); |
77
|
|
|
} |
78
|
2 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param InputInterface $input |
82
|
|
|
* @param OutputInterface $output |
83
|
|
|
* |
84
|
|
|
* @return int|null|void |
85
|
|
|
* @throws DoctrineStaticMetaException |
86
|
|
|
*/ |
87
|
1 |
|
public function execute(InputInterface $input, OutputInterface $output) |
88
|
|
|
{ |
89
|
|
|
try { |
90
|
1 |
|
$output->writeln( |
91
|
|
|
'<comment>Setting Entity ' |
92
|
1 |
|
.$input->getOption(static::OPT_ENTITY) |
93
|
1 |
|
.' has Field '.$input->getOption(static::OPT_FIELD) |
94
|
1 |
|
.'</comment>' |
95
|
|
|
); |
96
|
1 |
|
$this->checkOptions($input); |
97
|
1 |
|
$this->entityFieldSetter |
98
|
1 |
|
->setEntityHasField( |
99
|
1 |
|
$input->getOption(static::OPT_ENTITY), |
100
|
1 |
|
$input->getOption(static::OPT_FIELD) |
101
|
|
|
); |
102
|
1 |
|
$output->writeln('<info>completed</info>'); |
103
|
|
|
} catch (\Exception $e) { |
104
|
|
|
throw new DoctrineStaticMetaException('Exception in '.__METHOD__.': '.$e->getMessage(), $e->getCode(), $e); |
105
|
|
|
} |
106
|
1 |
|
} |
107
|
|
|
} |
108
|
|
|
|