|
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
|
|
|
$io = 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
|
|
|
$io->error('Some Overrides are Invalid'); |
|
46
|
|
|
$fixed = $this->renderInvalidOverrides($invalidOverrides, $input, $output, $io); |
|
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
|
|
|
InputInterface $input, |
|
70
|
|
|
OutputInterface $output, |
|
71
|
|
|
SymfonyStyle $io |
|
72
|
|
|
): bool { |
|
73
|
|
|
$return = false; |
|
74
|
|
|
foreach ($invalidOverrides as $pathToFileInOverrides => $details) { |
|
75
|
|
|
$return = $this->processInvalidOverride($pathToFileInOverrides, $details, $input, $output, $io); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $return; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function processInvalidOverride( |
|
82
|
|
|
string $relativePathToFileInOverrides, |
|
83
|
|
|
array $details, |
|
84
|
|
|
InputInterface $input, |
|
|
|
|
|
|
85
|
|
|
OutputInterface $output, |
|
86
|
|
|
SymfonyStyle $io |
|
87
|
|
|
): bool { |
|
88
|
|
|
$io->title('Working on ' . basename($relativePathToFileInOverrides)); |
|
89
|
|
|
$output->writeln('<comment>' . $relativePathToFileInOverrides . '</comment>'); |
|
90
|
|
|
$io->section('Details'); |
|
91
|
|
|
$table = new Table($output); |
|
92
|
|
|
$table->setHeaders(['Key', 'Value']); |
|
93
|
|
|
$table->addRows( |
|
94
|
|
|
[ |
|
95
|
|
|
['Project File', $details['projectPath']], |
|
96
|
|
|
['Override File', $details['overridePath']], |
|
97
|
|
|
['New MD5', $details['new md5']], |
|
98
|
|
|
['Diff Size', substr_count($details['diff'], "\n")], |
|
99
|
|
|
] |
|
100
|
|
|
); |
|
101
|
|
|
$table->render(); |
|
102
|
|
|
$output->writeln('<info>Diff:</info>'); |
|
103
|
|
|
$output->write($details['diff']); |
|
104
|
|
|
$output->writeln("\n\n"); |
|
105
|
|
|
$output->writeln('<info>Fixing this</info>'); |
|
106
|
|
|
$output->writeln(<<<TEXT |
|
107
|
|
|
|
|
108
|
|
|
The suggested fix in this situation is: |
|
109
|
|
|
|
|
110
|
|
|
* Rename the current override |
|
111
|
|
|
* Make a new override from the newly generated file |
|
112
|
|
|
* Reapply your custom code to the new override |
|
113
|
|
|
* Finally delete the old override. |
|
114
|
|
|
|
|
115
|
|
|
TEXT |
|
116
|
|
|
); |
|
117
|
|
|
if (!$io->ask('Would you like to move the current override and make a new one and then diff this?', true)) { |
|
118
|
|
|
$output->writeln('<commment>Skipping ' . $relativePathToFileInOverrides . '</commment>'); |
|
119
|
|
|
|
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$io->section('Recreating Override'); |
|
124
|
|
|
list($old,) = $this->fileOverrider->recreateOverride($relativePathToFileInOverrides); |
|
125
|
|
|
|
|
126
|
|
|
$table = new Table($output); |
|
127
|
|
|
$table->addRow(['project file', $details['projectPath']]); |
|
128
|
|
|
$table->render(); |
|
129
|
|
|
|
|
130
|
|
|
$table = new Table($output); |
|
131
|
|
|
$table->addRow(['old override', $old]); |
|
132
|
|
|
$table->render(); |
|
133
|
|
|
|
|
134
|
|
|
$output->writeln(<<<TEXT |
|
135
|
|
|
|
|
136
|
|
|
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. |
|
137
|
|
|
|
|
138
|
|
|
* Open the project file |
|
139
|
|
|
|
|
140
|
|
|
* In PHPStorm, find the old file, right click it and select "compare with editor" |
|
141
|
|
|
|
|
142
|
|
|
TEXT |
|
143
|
|
|
); |
|
144
|
|
|
$io->caution('You must do this bit really carefully and exactly as instructed!!'); |
|
145
|
|
|
|
|
146
|
|
|
while (false === |
|
147
|
|
|
$io->confirm('Confirm you have now copied all required changes from the old override to the new one?', |
|
148
|
|
|
false)) { |
|
149
|
|
|
$io->warning('You must now copy all required changes from the old override to the new one'); |
|
150
|
|
|
} |
|
151
|
|
|
$io->section('Now updating override'); |
|
152
|
|
|
$this->fileOverrider->updateOverrideFiles(); |
|
153
|
|
|
$io->success("\n\nCompleted override update for $relativePathToFileInOverrides\n\n"); |
|
154
|
|
|
|
|
155
|
|
|
return true; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
private function renderTableOfUpdatedFiles(array $files, OutputInterface $output): void |
|
159
|
|
|
{ |
|
160
|
|
|
list($updated, $same) = $files; |
|
161
|
|
|
if ([] !== $updated) { |
|
162
|
|
|
$output->writeln('Files Updated:'); |
|
163
|
|
|
$table = new Table($output); |
|
164
|
|
|
foreach ($updated as $file) { |
|
165
|
|
|
$table->addRow([$file]); |
|
166
|
|
|
} |
|
167
|
|
|
$table->render(); |
|
168
|
|
|
} |
|
169
|
|
|
if ([] !== $same) { |
|
170
|
|
|
$output->writeln('Files Same:'); |
|
171
|
|
|
$table = new Table($output); |
|
172
|
|
|
foreach ($same as $file) { |
|
173
|
|
|
$table->addRow([$file]); |
|
174
|
|
|
} |
|
175
|
|
|
$table->render(); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @throws DoctrineStaticMetaException |
|
181
|
|
|
*/ |
|
182
|
|
|
protected function configure(): void |
|
183
|
|
|
{ |
|
184
|
|
|
try { |
|
185
|
|
|
$this |
|
186
|
|
|
->setName(AbstractCommand::COMMAND_PREFIX . 'overrides:update') |
|
187
|
|
|
->setDefinition( |
|
188
|
|
|
[ |
|
189
|
|
|
new InputOption( |
|
190
|
|
|
self::OPT_OVERRIDE_ACTION, |
|
191
|
|
|
self::OPT_OVERRIDE_ACTION_SHORT, |
|
192
|
|
|
InputOption::VALUE_REQUIRED, |
|
193
|
|
|
'One of [ fromProject, toProject ]' |
|
194
|
|
|
), |
|
195
|
|
|
$this->getProjectRootPathOption(), |
|
196
|
|
|
] |
|
197
|
|
|
)->setDescription( |
|
198
|
|
|
'Update project overrides' |
|
199
|
|
|
); |
|
200
|
|
|
} catch (\Exception $e) { |
|
201
|
|
|
throw new DoctrineStaticMetaException( |
|
202
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
|
203
|
|
|
$e->getCode(), |
|
204
|
|
|
$e |
|
205
|
|
|
); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.