|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the SexyField package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Dion Snoeijen <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare (strict_types = 1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Tardigrades\Command; |
|
15
|
|
|
|
|
16
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
|
17
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
18
|
|
|
use Symfony\Component\Console\Question\Question; |
|
19
|
|
|
use Tardigrades\Entity\FieldType; |
|
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
22
|
|
|
use Tardigrades\SectionField\Service\FieldNotFoundException; |
|
23
|
|
|
use Tardigrades\SectionField\Service\FieldTypeManagerInterface; |
|
24
|
|
|
use Tardigrades\SectionField\Service\FieldTypeNotFoundException; |
|
25
|
|
|
use Tardigrades\SectionField\ValueObject\FullyQualifiedClassName; |
|
26
|
|
|
use Tardigrades\SectionField\ValueObject\Id; |
|
27
|
|
|
|
|
28
|
|
|
class UpdateFieldTypeCommand extends FieldTypeCommand |
|
29
|
|
|
{ |
|
30
|
|
|
/** @var QuestionHelper */ |
|
31
|
|
|
private $questionHelper; |
|
32
|
|
|
|
|
33
|
|
|
/** @var FieldTypeManagerInterface */ |
|
34
|
|
|
private $fieldTypeManager; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct( |
|
37
|
|
|
FieldTypeManagerInterface $fieldTypeManager |
|
38
|
|
|
) { |
|
39
|
|
|
$this->fieldTypeManager = $fieldTypeManager; |
|
40
|
|
|
|
|
41
|
|
|
parent::__construct('sf:update-field-type'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function configure() |
|
45
|
|
|
{ |
|
46
|
|
|
$this |
|
47
|
|
|
->setDescription('Updates a field type.') |
|
48
|
|
|
->setHelp('Update a field type based on new fully qualified class name.') |
|
49
|
|
|
; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
53
|
|
|
{ |
|
54
|
|
|
try { |
|
55
|
|
|
$this->questionHelper = $this->getHelper('question'); |
|
56
|
|
|
|
|
57
|
|
|
$this->showInstalledFieldTypes($input, $output); |
|
58
|
|
|
} catch (FieldTypeNotFoundException $exception) { |
|
59
|
|
|
$output->writeln("Field type not found"); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function showInstalledFieldTypes(InputInterface $input, OutputInterface $output) |
|
64
|
|
|
{ |
|
65
|
|
|
$fieldTypes = $this->fieldTypeManager->readAll(); |
|
66
|
|
|
|
|
67
|
|
|
$this->renderTable($output, $fieldTypes, 'The * column is what can be updated, type is updated automatically.'); |
|
68
|
|
|
$this->updateWhatRecord($input, $output); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function getFieldType(InputInterface $input, OutputInterface $output): FieldType |
|
72
|
|
|
{ |
|
73
|
|
|
$question = new Question('<question>What record do you want to update?</question> (#id): '); |
|
74
|
|
|
$question->setValidator(function ($id) { |
|
75
|
|
|
try { |
|
76
|
|
|
return $this->fieldTypeManager->read(Id::fromInt((int) $id)); |
|
77
|
|
|
} catch (FieldTypeNotFoundException $exception) { |
|
78
|
|
|
return null; |
|
79
|
|
|
} |
|
80
|
|
|
}); |
|
81
|
|
|
|
|
82
|
|
|
$fieldType = $this->questionHelper->ask($input, $output, $question); |
|
83
|
|
|
if (!$fieldType) { |
|
84
|
|
|
throw new FieldTypeNotFoundException(); |
|
85
|
|
|
} |
|
86
|
|
|
return $fieldType; |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function getNamespace( |
|
90
|
|
|
InputInterface $input, |
|
91
|
|
|
OutputInterface $output, |
|
92
|
|
|
FieldType $fieldType |
|
93
|
|
|
): FullyQualifiedClassName { |
|
94
|
|
|
$updateQuestion = new Question( |
|
95
|
|
|
'<question>Give a new fully qualified class name</question> (old: ' . |
|
96
|
|
|
$fieldType->getFullyQualifiedClassName() . |
|
97
|
|
|
'): ' |
|
98
|
|
|
); |
|
99
|
|
|
$updateQuestion->setValidator(function ($fullyQualifiedClassName) { |
|
100
|
|
|
return FullyQualifiedClassName::fromString($fullyQualifiedClassName); |
|
101
|
|
|
}); |
|
102
|
|
|
|
|
103
|
|
|
return $this->questionHelper->ask($input, $output, $updateQuestion); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private function updateWhatRecord(InputInterface $input, OutputInterface $output) |
|
107
|
|
|
{ |
|
108
|
|
|
$fieldType = $this->getFieldType($input, $output); |
|
109
|
|
|
$namespace = $this->getNamespace($input, $output, $fieldType); |
|
110
|
|
|
|
|
111
|
|
|
$output->writeln( |
|
112
|
|
|
'<info>Record with id #' . |
|
113
|
|
|
$fieldType->getId() . |
|
114
|
|
|
' will be updated with namespace: </info>' . |
|
115
|
|
|
(string) $namespace |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
$sure = new ConfirmationQuestion('<comment>Are you sure?</comment> (y/n) ', false); |
|
119
|
|
|
|
|
120
|
|
|
if (!$this->questionHelper->ask($input, $output, $sure)) { |
|
121
|
|
|
$output->writeln('<comment>Cancelled, nothing updated.</comment>'); |
|
122
|
|
|
return; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->updateRecord($input, $output, $fieldType, $namespace); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
private function updateRecord( |
|
129
|
|
|
InputInterface $input, |
|
|
|
|
|
|
130
|
|
|
OutputInterface $output, |
|
131
|
|
|
FieldType $fieldType, |
|
132
|
|
|
FullyQualifiedClassName $fullyQualifiedClassName |
|
133
|
|
|
) { |
|
134
|
|
|
$fieldType->setType($fullyQualifiedClassName->getClassName()); |
|
135
|
|
|
$fieldType->setFullyQualifiedClassName((string) $fullyQualifiedClassName); |
|
136
|
|
|
$this->fieldTypeManager->update(); |
|
137
|
|
|
$this->renderTable( |
|
138
|
|
|
$output, |
|
139
|
|
|
[$fieldType], |
|
140
|
|
|
'The * column is what can be updated, type is updated automatically.' |
|
141
|
|
|
); |
|
142
|
|
|
|
|
143
|
|
|
$output->writeln('<info>FieldTypeInterface Updated!</info>'); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|