forked from magento/magento-cloud-patches
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApplier.php
180 lines (158 loc) · 4.4 KB
/
Applier.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\CloudPatches\Patch;
use Magento\CloudPatches\Composer\MagentoVersion;
use Magento\CloudPatches\Filesystem\Filesystem;
use Magento\CloudPatches\Patch\Status\StatusPool;
/**
* Applies and reverts patches.
*/
class Applier
{
/**
* @var PatchCommandInterface
*/
private $patchCommand;
/**
* @var GitConverter
*/
private $gitConverter;
/**
* @var MagentoVersion
*/
private $magentoVersion;
/**
* @var Filesystem
*/
private $filesystem;
/**
* @param PatchCommandInterface $patchCommand
* @param GitConverter $gitConverter
* @param MagentoVersion $magentoVersion
* @param Filesystem $filesystem
*/
public function __construct(
PatchCommandInterface $patchCommand,
GitConverter $gitConverter,
MagentoVersion $magentoVersion,
Filesystem $filesystem
) {
$this->patchCommand = $patchCommand;
$this->gitConverter = $gitConverter;
$this->magentoVersion = $magentoVersion;
$this->filesystem = $filesystem;
}
/**
* General apply processing.
*
* @param string $path
* @param string $id
* @return string
*
* @throws ApplierException
*/
public function apply(string $path, string $id): string
{
$content = $this->readContent($path);
try {
$this->patchCommand->apply($content);
} catch (PatchCommandException $exception) {
try {
$this->patchCommand->revertCheck($content);
} catch (PatchCommandException $reverseException) {
throw new ApplierException($exception->getMessage(), $exception->getCode());
}
return sprintf('Patch %s was already applied', $id);
}
return sprintf('Patch %s has been applied', $id);
}
/**
* General revert processing.
*
* @param string $path
* @param string $id
* @return string
*
* @throws ApplierException
*/
public function revert(string $path, string $id): string
{
$content = $this->readContent($path);
try {
$this->patchCommand->revert($content);
} catch (PatchCommandException $exception) {
try {
$this->patchCommand->applyCheck($content);
} catch (PatchCommandException $applyException) {
throw new ApplierException($exception->getMessage(), $exception->getCode());
}
return sprintf('Patch %s wasn\'t applied', $id);
}
return sprintf('Patch %s has been reverted', $id);
}
/**
* Checks patch status.
*
* @param string $patchContent
* @return string
*/
public function status(string $patchContent): string
{
$patchContent = $this->prepareContent($patchContent);
try {
$this->patchCommand->applyCheck($patchContent);
} catch (PatchCommandException $exception) {
try {
$this->patchCommand->revertCheck($patchContent);
} catch (PatchCommandException $reverseException) {
return StatusPool::NA;
}
return StatusPool::APPLIED;
}
return StatusPool::NOT_APPLIED;
}
/**
* Checks if the patch can be applied.
*
* @param string $patchContent
* @return boolean
*/
public function checkApply(string $patchContent): bool
{
$patchContent = $this->prepareContent($patchContent);
try {
$this->patchCommand->applyCheck($patchContent);
} catch (PatchCommandException $exception) {
return false;
}
return true;
}
/**
* Returns patch content.
*
* @param string $path
* @return string
*/
private function readContent(string $path): string
{
$content = $this->filesystem->get($path);
return $this->prepareContent($content);
}
/**
* Prepares patch content.
*
* @param string $content
* @return string
*/
private function prepareContent(string $content): string
{
if ($this->magentoVersion->isGitBased()) {
$content = $this->gitConverter->convert($content);
}
return $content;
}
}