forked from magento/magento-cloud-patches
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRevert.php
156 lines (136 loc) · 3.96 KB
/
Revert.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\CloudPatches\Command\Process\Ece;
use Magento\CloudPatches\App\RuntimeException;
use Magento\CloudPatches\Command\Process\Action\RevertAction;
use Magento\CloudPatches\Command\Process\ProcessInterface;
use Magento\CloudPatches\Command\Process\Renderer;
use Magento\CloudPatches\Patch\Applier;
use Magento\CloudPatches\Patch\ApplierException;
use Magento\CloudPatches\Patch\Pool\LocalPool;
use Magento\CloudPatches\Patch\Status\StatusPool;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Reverts all patches (Cloud).
*/
class Revert implements ProcessInterface
{
/**
* @var RevertAction
*/
private $revertAction;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var Applier
*/
private $applier;
/**
* @var LocalPool
*/
private $localPool;
/**
* @var Renderer
*/
private $renderer;
/**
* @var StatusPool
*/
private $statusPool;
/**
* @param RevertAction $revertAction
* @param LoggerInterface $logger
* @param Applier $applier
* @param LocalPool $localPool
* @param Renderer $renderer
* @param StatusPool $statusPool
*/
public function __construct(
RevertAction $revertAction,
LoggerInterface $logger,
Applier $applier,
LocalPool $localPool,
Renderer $renderer,
StatusPool $statusPool
) {
$this->revertAction = $revertAction;
$this->logger = $logger;
$this->applier = $applier;
$this->localPool = $localPool;
$this->renderer = $renderer;
$this->statusPool = $statusPool;
}
/**
* @inheritDoc
*/
public function run(InputInterface $input, OutputInterface $output)
{
$this->logger->notice('Start of reverting all patches');
$this->revertLocalPatches($output);
$this->revertAction->execute($input, $output, []);
$this->logger->notice('End of reverting all patches');
}
/**
* Reverts local custom patches.
* @param OutputInterface $output
* @return void
* @throws RuntimeException
*/
private function revertLocalPatches(OutputInterface $output)
{
$patches = array_filter(
$this->localPool->getList(),
function ($patch) {
return !$this->statusPool->isNotApplied($patch->getId());
}
);
if (empty($patches)) {
return;
}
$output->writeln('<info>Start of reverting hot-fixes</info>');
foreach (array_reverse($patches) as $patch) {
try {
$message = $this->applier->revert($patch->getPath(), $patch->getTitle());
$this->printInfo($output, $message);
} catch (ApplierException $exception) {
$errorMessage = sprintf(
'Reverting patch %s failed.%s',
$patch->getPath(),
PHP_EOL . $exception->getMessage()
);
$this->printError($output, $errorMessage);
}
}
$output->writeln('<info>End of reverting hot-fixes</info>');
}
/**
* Prints and logs info message.
*
* @param OutputInterface $output
* @param string $message
*/
private function printInfo(OutputInterface $output, string $message)
{
$output->writeln('<info>' . $message . '</info>');
$this->logger->info($message);
}
/**
* Prints and logs error message.
*
* @param OutputInterface $output
* @param string $message
*/
private function printError(OutputInterface $output, string $message)
{
$output->writeln('<error>' . $message . '</error>');
$this->logger->error($message);
}
}