|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\RelationsGenerator; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
class SetRelationCommand extends AbstractCommand |
|
12
|
|
|
{ |
|
13
|
|
|
public const OPT_ENTITY1 = 'entity1'; |
|
14
|
|
|
public const OPT_ENTITY1_SHORT = 'm'; |
|
15
|
|
|
|
|
16
|
|
|
public const OPT_HAS_TYPE = 'hasType'; |
|
17
|
|
|
public const OPT_HAS_TYPE_SHORT = 't'; |
|
18
|
|
|
|
|
19
|
|
|
public const OPT_ENTITY2 = 'entity2'; |
|
20
|
|
|
public const OPT_ENTITY2_SHORT = 'i'; |
|
21
|
|
|
|
|
22
|
|
|
public const OPT_REQUIRED_RELATION = 'required-relation'; |
|
23
|
|
|
public const OPT_REQUIRED_RELATION_SHORT = 'w'; |
|
24
|
|
|
public const DEFAULT_REQUIRED_RELATION = false; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var RelationsGenerator |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $relationsGenerator; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* SetRelationCommand constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param RelationsGenerator $relationsGenerator |
|
35
|
|
|
* @param null|string $name |
|
36
|
|
|
* |
|
37
|
|
|
* @throws DoctrineStaticMetaException |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct( |
|
40
|
|
|
RelationsGenerator $relationsGenerator, |
|
41
|
|
|
?string $name = null |
|
42
|
|
|
) { |
|
43
|
|
|
parent::__construct($name); |
|
44
|
|
|
$this->relationsGenerator = $relationsGenerator; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @throws DoctrineStaticMetaException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function configure(): void |
|
51
|
|
|
{ |
|
52
|
|
|
try { |
|
53
|
|
|
$this->setName(AbstractCommand::COMMAND_PREFIX . 'set:relation') |
|
54
|
|
|
->setDefinition( |
|
55
|
|
|
[ |
|
56
|
|
|
new InputOption( |
|
57
|
|
|
self::OPT_ENTITY1, |
|
58
|
|
|
self::OPT_ENTITY1_SHORT, |
|
59
|
|
|
InputOption::VALUE_REQUIRED, |
|
60
|
|
|
'First entity in relation' |
|
61
|
|
|
), |
|
62
|
|
|
new InputOption( |
|
63
|
|
|
self::OPT_HAS_TYPE, |
|
64
|
|
|
self::OPT_HAS_TYPE_SHORT, |
|
65
|
|
|
InputOption::VALUE_REQUIRED, |
|
66
|
|
|
'What type of relation is it? ' |
|
67
|
|
|
. 'Must be one of ' . RelationsGenerator::class . '::RELATION_TYPES' |
|
68
|
|
|
), |
|
69
|
|
|
new InputOption( |
|
70
|
|
|
self::OPT_ENTITY2, |
|
71
|
|
|
self::OPT_ENTITY2_SHORT, |
|
72
|
|
|
InputOption::VALUE_REQUIRED, |
|
73
|
|
|
'Second entity in relation' |
|
74
|
|
|
), |
|
75
|
|
|
new InputOption( |
|
76
|
|
|
self::OPT_REQUIRED_RELATION, |
|
77
|
|
|
self::OPT_REQUIRED_RELATION_SHORT, |
|
78
|
|
|
InputOption::VALUE_OPTIONAL, |
|
79
|
|
|
'Is the relation required (i.e. not nullable)?', |
|
80
|
|
|
self::DEFAULT_REQUIRED_RELATION |
|
81
|
|
|
), |
|
82
|
|
|
$this->getProjectRootPathOption(), |
|
83
|
|
|
$this->getProjectRootNamespaceOption(), |
|
84
|
|
|
$this->getSrcSubfolderOption(), |
|
85
|
|
|
] |
|
86
|
|
|
)->setDescription( |
|
87
|
|
|
'Set a relation between 2 entities. The relation must be one of ' |
|
88
|
|
|
. RelationsGenerator::class . '::RELATION_TYPES' |
|
89
|
|
|
); |
|
90
|
|
|
} catch (\Exception $e) { |
|
91
|
|
|
throw new DoctrineStaticMetaException( |
|
92
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
|
93
|
|
|
$e->getCode(), |
|
94
|
|
|
$e |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param InputInterface $input |
|
101
|
|
|
* @param OutputInterface $output |
|
102
|
|
|
* |
|
103
|
|
|
* @return int|null|void |
|
104
|
|
|
* @throws DoctrineStaticMetaException |
|
105
|
|
|
*/ |
|
106
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
|
107
|
|
|
{ |
|
108
|
|
|
try { |
|
109
|
|
|
$output->writeln( |
|
110
|
|
|
'<comment>Setting relation: ' |
|
111
|
|
|
. $input->getOption(static::OPT_ENTITY1) |
|
112
|
|
|
. ' ' . $input->getOption(static::OPT_HAS_TYPE) |
|
113
|
|
|
. ' ' . $input->getOption(static::OPT_ENTITY2) |
|
114
|
|
|
. '</comment>' |
|
115
|
|
|
); |
|
116
|
|
|
$this->checkOptions($input); |
|
117
|
|
|
$hasType = $input->getOption(static::OPT_HAS_TYPE); |
|
118
|
|
|
if (!\in_array($hasType, RelationsGenerator::HAS_TYPES, true)) { |
|
119
|
|
|
$hasType = RelationsGenerator::PREFIX_OWNING . $hasType; |
|
120
|
|
|
if (!\in_array($hasType, RelationsGenerator::HAS_TYPES, true)) { |
|
121
|
|
|
throw new DoctrineStaticMetaException( |
|
122
|
|
|
'Invalid hasType ' . $input->getOption(static::OPT_HAS_TYPE) |
|
123
|
|
|
. ' Must be one of ' . print_r(RelationsGenerator::HAS_TYPES, true) |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
$this->relationsGenerator |
|
128
|
|
|
->setPathToProjectRoot($input->getOption(AbstractCommand::OPT_PROJECT_ROOT_PATH)) |
|
129
|
|
|
->setProjectRootNamespace($input->getOption(AbstractCommand::OPT_PROJECT_ROOT_NAMESPACE)); |
|
130
|
|
|
|
|
131
|
|
|
$this->relationsGenerator->setEntityHasRelationToEntity( |
|
132
|
|
|
$input->getOption(static::OPT_ENTITY1), |
|
133
|
|
|
$hasType, |
|
134
|
|
|
$input->getOption(static::OPT_ENTITY2), |
|
135
|
|
|
\in_array($input->getOption(self::OPT_REQUIRED_RELATION), ['1', 1, 'true', true], true) |
|
136
|
|
|
); |
|
137
|
|
|
$output->writeln('<info>completed</info>'); |
|
138
|
|
|
} catch (\Exception $e) { |
|
139
|
|
|
throw new DoctrineStaticMetaException( |
|
140
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
|
141
|
|
|
$e->getCode(), |
|
142
|
|
|
$e |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|