1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PostProcessor\FileOverrider; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
7
|
|
|
use Symfony\Component\Console\Helper\Table; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
12
|
|
|
|
13
|
|
|
class OverridesUpdateCommand extends AbstractCommand |
14
|
|
|
{ |
15
|
|
|
public const OPT_OVERRIDE_ACTION = 'action'; |
16
|
|
|
public const OPT_OVERRIDE_ACTION_SHORT = 'a'; |
17
|
|
|
|
18
|
|
|
public const ACTION_TO_PROJECT = 'toProject'; |
19
|
|
|
public const ACTION_FROM_PROJECT = 'fromProject'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var FileOverrider |
23
|
|
|
*/ |
24
|
|
|
protected $fileOverrider; |
25
|
|
|
|
26
|
|
|
public function __construct(FileOverrider $fileOverrider, ?string $name = null) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($name); |
29
|
|
|
$this->fileOverrider = $fileOverrider; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void |
33
|
|
|
{ |
34
|
|
|
$symfonyStyle = new SymfonyStyle($input, $output); |
35
|
|
|
$this->checkOptions($input); |
36
|
|
|
$output->writeln( |
37
|
|
|
'<comment>Updating overrides ' . $input->getOption(self::OPT_OVERRIDE_ACTION) . '</comment>' |
38
|
|
|
); |
39
|
|
|
$this->checkOptions($input); |
40
|
|
|
$this->fileOverrider->setPathToProjectRoot($input->getOption(self::OPT_PROJECT_ROOT_PATH)); |
41
|
|
|
switch ($input->getOption(self::OPT_OVERRIDE_ACTION)) { |
42
|
|
|
case self::ACTION_TO_PROJECT: |
43
|
|
|
$invalidOverrides = $this->fileOverrider->getInvalidOverrides(); |
44
|
|
|
if ([] !== $invalidOverrides) { |
45
|
|
|
$symfonyStyle->error('Some Overrides are Invalid'); |
46
|
|
|
$fixed = $this->renderInvalidOverrides($invalidOverrides, $output, $symfonyStyle); |
47
|
|
|
if (false === $fixed) { |
48
|
|
|
throw new \RuntimeException('Errors in applying overrides'); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
$this->renderTableOfUpdatedFiles($this->fileOverrider->applyOverrides(), $output); |
52
|
|
|
$output->writeln('<info>Overrides have been applied to project</info>'); |
53
|
|
|
|
54
|
|
|
return; |
55
|
|
|
case self::ACTION_FROM_PROJECT: |
56
|
|
|
$this->renderTableOfUpdatedFiles($this->fileOverrider->updateOverrideFiles(), $output); |
57
|
|
|
$output->writeln('<info>Overrides have been updated from the project</info>'); |
58
|
|
|
|
59
|
|
|
return; |
60
|
|
|
default: |
61
|
|
|
throw new \InvalidArgumentException( |
62
|
|
|
' Invalid action ' . $input->getOption(self::OPT_OVERRIDE_ACTION) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function renderInvalidOverrides( |
68
|
|
|
array $invalidOverrides, |
69
|
|
|
OutputInterface $output, |
70
|
|
|
SymfonyStyle $symfonyStyle |
71
|
|
|
): bool { |
72
|
|
|
$return = false; |
73
|
|
|
foreach ($invalidOverrides as $pathToFileInOverrides => $details) { |
74
|
|
|
$return = $this->processInvalidOverride($pathToFileInOverrides, $details, $output, $symfonyStyle); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function processInvalidOverride( |
81
|
|
|
string $relativePathToFileInOverrides, |
82
|
|
|
array $details, |
83
|
|
|
OutputInterface $output, |
84
|
|
|
SymfonyStyle $symfonyStyle |
85
|
|
|
): bool { |
86
|
|
|
$symfonyStyle->title('Working on ' . basename($relativePathToFileInOverrides)); |
87
|
|
|
$output->writeln('<comment>' . $relativePathToFileInOverrides . '</comment>'); |
88
|
|
|
$symfonyStyle->section('Details'); |
89
|
|
|
$table = new Table($output); |
90
|
|
|
$table->setHeaders(['Key', 'Value']); |
91
|
|
|
$table->addRows( |
92
|
|
|
[ |
93
|
|
|
['Project File', $details['projectPath']], |
94
|
|
|
['Override File', $details['overridePath']], |
95
|
|
|
['New MD5', $details['new md5']], |
96
|
|
|
['Diff Size', substr_count($details['diff'], "\n")], |
97
|
|
|
] |
98
|
|
|
); |
99
|
|
|
$table->render(); |
100
|
|
|
$output->writeln('<info>Diff:</info>'); |
101
|
|
|
$output->write($details['diff']); |
102
|
|
|
$output->writeln("\n\n"); |
103
|
|
|
$output->writeln('<info>Fixing this</info>'); |
104
|
|
|
$output->writeln(<<<TEXT |
105
|
|
|
|
106
|
|
|
The suggested fix in this situation is: |
107
|
|
|
|
108
|
|
|
* Rename the current override |
109
|
|
|
* Make a new override from the newly generated file |
110
|
|
|
* Reapply your custom code to the new override |
111
|
|
|
* Finally delete the old override. |
112
|
|
|
|
113
|
|
|
TEXT |
114
|
|
|
); |
115
|
|
|
if (!$symfonyStyle->ask('Would you like to move the current override and make a new one and then diff this?', |
116
|
|
|
true)) { |
117
|
|
|
$output->writeln('<commment>Skipping ' . $relativePathToFileInOverrides . '</commment>'); |
118
|
|
|
|
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$symfonyStyle->section('Recreating Override'); |
123
|
|
|
list($old,) = $this->fileOverrider->recreateOverride($relativePathToFileInOverrides); |
124
|
|
|
|
125
|
|
|
$table = new Table($output); |
126
|
|
|
$table->addRow(['project file', $details['projectPath']]); |
127
|
|
|
$table->render(); |
128
|
|
|
|
129
|
|
|
$table = new Table($output); |
130
|
|
|
$table->addRow(['old override', $old]); |
131
|
|
|
$table->render(); |
132
|
|
|
|
133
|
|
|
$output->writeln(<<<TEXT |
134
|
|
|
|
135
|
|
|
Now we have created a new override from your freshly generated file, you need to manually copy across all the changes from the old override into your project file. |
136
|
|
|
|
137
|
|
|
* Open the project file |
138
|
|
|
|
139
|
|
|
* In PHPStorm, find the old file, right click it and select "compare with editor" |
140
|
|
|
|
141
|
|
|
TEXT |
142
|
|
|
); |
143
|
|
|
$symfonyStyle->caution('You must do this bit really carefully and exactly as instructed!!'); |
144
|
|
|
|
145
|
|
|
while (false === |
146
|
|
|
$symfonyStyle->confirm('Confirm you have now copied all required changes from the old override to the new one?', |
147
|
|
|
false)) { |
148
|
|
|
$symfonyStyle->warning('You must now copy all required changes from the old override to the new one'); |
149
|
|
|
} |
150
|
|
|
$symfonyStyle->section('Now updating override'); |
151
|
|
|
$this->fileOverrider->updateOverrideFiles(); |
152
|
|
|
$symfonyStyle->success("\n\nCompleted override update for $relativePathToFileInOverrides\n\n"); |
153
|
|
|
|
154
|
|
|
return true; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
private function renderTableOfUpdatedFiles(array $files, OutputInterface $output): void |
158
|
|
|
{ |
159
|
|
|
list($updated, $same) = $files; |
160
|
|
|
if ([] !== $updated) { |
161
|
|
|
$output->writeln('Files Updated:'); |
162
|
|
|
$table = new Table($output); |
163
|
|
|
foreach ($updated as $file) { |
164
|
|
|
$table->addRow([$file]); |
165
|
|
|
} |
166
|
|
|
$table->render(); |
167
|
|
|
} |
168
|
|
|
if ([] !== $same) { |
169
|
|
|
$output->writeln('Files Same:'); |
170
|
|
|
$table = new Table($output); |
171
|
|
|
foreach ($same as $file) { |
172
|
|
|
$table->addRow([$file]); |
173
|
|
|
} |
174
|
|
|
$table->render(); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @throws DoctrineStaticMetaException |
180
|
|
|
*/ |
181
|
|
|
protected function configure(): void |
182
|
|
|
{ |
183
|
|
|
try { |
184
|
|
|
$this |
185
|
|
|
->setName(AbstractCommand::COMMAND_PREFIX . 'overrides:update') |
186
|
|
|
->setDefinition( |
187
|
|
|
[ |
188
|
|
|
new InputOption( |
189
|
|
|
self::OPT_OVERRIDE_ACTION, |
190
|
|
|
self::OPT_OVERRIDE_ACTION_SHORT, |
191
|
|
|
InputOption::VALUE_REQUIRED, |
192
|
|
|
'One of [ fromProject, toProject ]' |
193
|
|
|
), |
194
|
|
|
$this->getProjectRootPathOption(), |
195
|
|
|
] |
196
|
|
|
)->setDescription( |
197
|
|
|
'Update project overrides' |
198
|
|
|
); |
199
|
|
|
} catch (\Exception $e) { |
200
|
|
|
throw new DoctrineStaticMetaException( |
201
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
202
|
|
|
$e->getCode(), |
203
|
|
|
$e |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|