|
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
|
|
|
|
|
37
|
|
|
$targetConstraint = $this->targetConstraint(); |
|
38
|
|
|
|
|
39
|
|
|
if ($requiredMajor === null || $requiredMajor < 7) { |
|
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
|
|
|
$installedMajor = $this->context()->packageMajorVersion('backpack/crud'); |
|
47
|
|
|
$installedPretty = $this->context()->installedPackagePrettyVersion('backpack/crud'); |
|
48
|
|
|
|
|
49
|
|
|
if ($installedMajor !== null && $installedMajor < 7) { |
|
50
|
|
|
return StepResult::warning( |
|
51
|
|
|
'Composer requirement is updated, but Backpack v7 is not installed yet. Run composer update.', |
|
52
|
|
|
["Installed version: {$installedPretty}"] |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return StepResult::success("Composer.json requires backpack/crud {$constraint}."); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
protected function extractFirstInteger(string $value): ?int |
|
60
|
|
|
{ |
|
61
|
|
|
if (preg_match('/(\d+)/', $value, $matches)) { |
|
62
|
|
|
return (int) $matches[1]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function canFix(StepResult $result): bool |
|
69
|
|
|
{ |
|
70
|
|
|
if ($result->status !== StepStatus::Failed) { |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if ($this->missingRequirement) { |
|
75
|
|
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if ($this->currentConstraint === null) { |
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$requiredMajor = $this->extractFirstInteger($this->currentConstraint); |
|
83
|
|
|
|
|
84
|
|
|
return $requiredMajor === null || $requiredMajor < 7; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function fixMessage(StepResult $result): string |
|
88
|
|
|
{ |
|
89
|
|
|
return sprintf( |
|
90
|
|
|
'We can update composer.json to require backpack/crud:%s automatically. Apply this change now?', |
|
91
|
|
|
$this->targetConstraint() |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function fix(StepResult $result): StepResult |
|
96
|
|
|
{ |
|
97
|
|
|
$targetConstraint = $this->targetConstraint(); |
|
98
|
|
|
|
|
99
|
|
|
$section = $this->context()->composerRequirementSection('backpack/crud') ?? 'require'; |
|
100
|
|
|
|
|
101
|
|
|
$updated = $this->context()->updateComposerJson(function (array &$composer) use ($section, $targetConstraint) { |
|
102
|
|
|
$composer[$section] = $composer[$section] ?? []; |
|
103
|
|
|
$composer[$section]['backpack/crud'] = $targetConstraint; |
|
104
|
|
|
}); |
|
105
|
|
|
|
|
106
|
|
|
if (! $updated) { |
|
107
|
|
|
return StepResult::failure('Could not update composer.json automatically.'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return StepResult::success("Set backpack/crud requirement to {$targetConstraint} in composer.json."); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
private function targetConstraint(): string |
|
114
|
|
|
{ |
|
115
|
|
|
return UpgradeCommandConfig::backpackCrudRequirement(); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|