/
AbandonmentRecommendation.php
161 lines (139 loc) · 5.07 KB
/
AbandonmentRecommendation.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
<?php
declare(strict_types = 1);
namespace Acquia\Cli\Command\App\From\Recommendation;
use Acquia\Cli\Command\App\From\Safety\ArrayValidationTrait;
use Acquia\Cli\Command\App\From\SourceSite\ExtensionInterface;
use Closure;
use Exception;
use LogicException;
/**
* Represents a recommendation *not* to require a package.
*/
class AbandonmentRecommendation implements RecommendationInterface, NormalizableInterface {
use ArrayValidationTrait;
/**
* An anonymous function that determines if this recommendation is applicable.
*/
protected \Closure $evaluateExtension;
/**
* The original decoded definition.
*
* @var array<mixed>
*/
protected array $definition;
/**
* An array of extensions to which this recommendation applied.
*
* @var \Acquia\Cli\Command\App\From\SourceSite\ExtensionInterface[]
*/
protected array $appliedTo = [];
/**
* AbandonmentRecommendation constructor.
*
* @param \Closure $extension_evaluator
* An anonymous function that determines if this recommendation applies to
* an extension.
* @param array $definition
* The original decoded definition.
*/
protected function __construct(Closure $extension_evaluator, array $definition) {
$this->evaluateExtension = $extension_evaluator;
$this->definition = $definition;
}
/**
* Creates a new recommendation.
*
* @param mixed $definition
* A static recommendation definition. This must be an array. However, other
* value types are accepted because this method performs validation on the
* given value.
* @return \Acquia\Cli\Command\App\From\Recommendation\RecommendationInterface
* A new AbandonmentRecommendation object if the given definition is valid or
* a new NoRecommendation object otherwise.
*/
public static function createFromDefinition(mixed $definition): RecommendationInterface {
// phpcs:disable SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys
$validator = static::schema([
'package' => 'is_null',
'note' => 'is_string',
'replaces' => static::schema([
'name' => 'is_string',
]),
'vetted' => 'is_bool',
]);
// phpcs:enable
try {
$validated = $validator($definition);
}
catch (Exception $e) {
// Under any circumstance where the given recommendation configuration is
// invalid, we still want the rest of the script to proceed. I.e. it's
// better to produce a valid composer.json with a missing recommendation
// than to fail to create one at all.
return new NoRecommendation();
}
return new AbandonmentRecommendation(Closure::fromCallable(function (ExtensionInterface $extension) use ($validated): bool {
return $extension->getName() === $validated['replaces']['name'];
}), $validated);
}
public function applies(ExtensionInterface $extension): bool {
if (($this->evaluateExtension)($extension)) {
array_push($this->appliedTo, $extension);
return TRUE;
}
return FALSE;
}
public function getPackageName(): string {
throw new LogicException(sprintf('It is nonsensical to call the %s() method on a % class instance.', __FUNCTION__, __CLASS__));
}
public function getVersionConstraint(): string {
throw new LogicException(sprintf('It is nonsensical to call the %s() method on a %s class instance.', __FUNCTION__, __CLASS__));
}
public function hasModulesToInstall(): bool {
throw new LogicException(sprintf('It is nonsensical to call the %s() method on a %s class instance.', __FUNCTION__, __CLASS__));
}
/**
* {@inheritdoc}
*/
public function getModulesToInstall(): array {
throw new LogicException(sprintf('It is nonsensical to call the %s() method on a %s class instance.', __FUNCTION__, __CLASS__));
}
public function hasPatches(): bool {
throw new LogicException(sprintf('It is nonsensical to call the %s() method on a %s class instance.', __FUNCTION__, __CLASS__));
}
public function isVetted(): bool {
throw new LogicException(sprintf('It is nonsensical to call the %s() method on a %s class instance.', __FUNCTION__, __CLASS__));
}
/**
* {@inheritdoc}
*/
public function getPatches(): array {
throw new LogicException(sprintf('It is nonsensical to call the %s() method on a %s class instance.', __FUNCTION__, __CLASS__));
}
/**
* {@inheritDoc}
*/
public function normalize(): array {
// phpcs:disable SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys
$normalized = [
'type' => 'abandonmentRecommendation',
'id' => "abandon:{$this->definition['replaces']['name']}",
'attributes' => [
'note' => $this->definition['note'],
],
];
$recommended_for = [
'data' => array_map(function (ExtensionInterface $extension) {
return [
'type' => $extension->isModule() ? 'module' : 'theme',
'id' => $extension->getName(),
];
}, $this->appliedTo),
];
// phpcs:enable
if (!empty($recommended_for['data'])) {
$normalized['relationships']['recommendedFor'] = $recommended_for;
}
return $normalized;
}
}