|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Console\Commands\Upgrade\v7\Steps; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Console\Commands\Upgrade\Step; |
|
6
|
|
|
use Backpack\CRUD\app\Console\Commands\Upgrade\StepResult; |
|
7
|
|
|
use Backpack\CRUD\app\Console\Commands\Upgrade\StepStatus; |
|
8
|
|
|
use Backpack\CRUD\app\Console\Commands\Upgrade\v7\UpgradeCommandConfig; |
|
9
|
|
|
|
|
10
|
|
|
class EnsureBackpackCrudRequirementStep extends Step |
|
11
|
|
|
{ |
|
12
|
|
|
private ?string $currentConstraint = null; |
|
13
|
|
|
|
|
14
|
|
|
private bool $missingRequirement = false; |
|
15
|
|
|
|
|
16
|
|
|
public function title(): string |
|
17
|
|
|
{ |
|
18
|
|
|
return 'Update composer requirement for backpack/crud'; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function run(): StepResult |
|
22
|
|
|
{ |
|
23
|
|
|
$this->currentConstraint = null; |
|
24
|
|
|
$this->missingRequirement = false; |
|
25
|
|
|
|
|
26
|
|
|
$constraint = $this->context()->composerRequirement('backpack/crud'); |
|
27
|
|
|
$this->currentConstraint = $constraint; |
|
28
|
|
|
|
|
29
|
|
|
if ($constraint === null) { |
|
30
|
|
|
$this->missingRequirement = true; |
|
31
|
|
|
|
|
32
|
|
|
return StepResult::failure('The composer.json file does not declare backpack/crud.'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$requiredMajor = $this->extractFirstInteger($constraint); |
|
36
|
|
|
$targetConstraint = $this->targetConstraint(); |
|
37
|
|
|
$targetMajor = $this->targetMajor(); |
|
38
|
|
|
|
|
39
|
|
|
if ($requiredMajor === null) { |
|
40
|
|
|
return StepResult::failure( |
|
41
|
|
|
sprintf('Update composer.json to require backpack/crud:%s (or newer).', $targetConstraint), |
|
42
|
|
|
["Current constraint: {$constraint}"] |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if ($targetMajor !== null && $requiredMajor < $targetMajor) { |
|
47
|
|
|
return StepResult::failure( |
|
48
|
|
|
sprintf('Update composer.json to require backpack/crud:%s (or newer).', $targetConstraint), |
|
49
|
|
|
["Current constraint: {$constraint}"] |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$installedMajor = $this->context()->packageMajorVersion('backpack/crud'); |
|
54
|
|
|
$installedPretty = $this->context()->installedPackagePrettyVersion('backpack/crud'); |
|
55
|
|
|
|
|
56
|
|
|
$comparisonMajor = $targetMajor ?? $requiredMajor; |
|
57
|
|
|
|
|
58
|
|
|
if ($comparisonMajor !== null && $installedMajor !== null && $installedMajor < $comparisonMajor) { |
|
59
|
|
|
return StepResult::warning( |
|
60
|
|
|
sprintf( |
|
61
|
|
|
'Composer requirement is updated, but a Backpack version satisfying %s is not installed yet. Run composer update.', |
|
62
|
|
|
$targetConstraint |
|
63
|
|
|
), |
|
64
|
|
|
["Installed version: {$installedPretty}"] |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return StepResult::success("Composer.json requires backpack/crud {$constraint}."); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function canFix(StepResult $result): bool |
|
72
|
|
|
{ |
|
73
|
|
|
if ($result->status !== StepStatus::Failed) { |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ($this->missingRequirement) { |
|
78
|
|
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ($this->currentConstraint === null) { |
|
82
|
|
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$requiredMajor = $this->extractFirstInteger($this->currentConstraint); |
|
86
|
|
|
$targetMajor = $this->targetMajor(); |
|
87
|
|
|
|
|
88
|
|
|
if ($targetMajor === null) { |
|
89
|
|
|
return $requiredMajor === null; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $requiredMajor === null || $requiredMajor < $targetMajor; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function fixMessage(StepResult $result): string |
|
96
|
|
|
{ |
|
97
|
|
|
return sprintf( |
|
98
|
|
|
'We can update composer.json to require backpack/crud:%s automatically. Apply this change now?', |
|
99
|
|
|
$this->targetConstraint() |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function fix(StepResult $result): StepResult |
|
104
|
|
|
{ |
|
105
|
|
|
$targetConstraint = $this->targetConstraint(); |
|
106
|
|
|
|
|
107
|
|
|
$section = $this->context()->composerRequirementSection('backpack/crud') ?? 'require'; |
|
108
|
|
|
|
|
109
|
|
|
$updated = $this->context()->updateComposerJson(function (array &$composer) use ($section, $targetConstraint) { |
|
110
|
|
|
$composer[$section] = $composer[$section] ?? []; |
|
111
|
|
|
$composer[$section]['backpack/crud'] = $targetConstraint; |
|
112
|
|
|
}); |
|
113
|
|
|
|
|
114
|
|
|
if (! $updated) { |
|
115
|
|
|
return StepResult::failure('Could not update composer.json automatically.'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return StepResult::success("Set backpack/crud requirement to {$targetConstraint} in composer.json."); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
private function targetConstraint(): string |
|
122
|
|
|
{ |
|
123
|
|
|
return UpgradeCommandConfig::backpackCrudRequirement(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
private function targetMajor(): ?int |
|
127
|
|
|
{ |
|
128
|
|
|
$constraintMajor = $this->extractFirstInteger($this->targetConstraint()); |
|
129
|
|
|
|
|
130
|
|
|
if ($constraintMajor !== null) { |
|
131
|
|
|
return $constraintMajor; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $this->extractFirstInteger($this->context()->targetVersion()); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|