1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the box project. |
7
|
|
|
* |
8
|
|
|
* (c) Kevin Herrera <[email protected]> |
9
|
|
|
* Théo Fidry <[email protected]> |
10
|
|
|
* |
11
|
|
|
* This source file is subject to the MIT license that is bundled |
12
|
|
|
* with this source code in the file LICENSE. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace KevinGH\Box\RequirementChecker; |
16
|
|
|
|
17
|
|
|
use Composer\Semver\Semver; |
18
|
|
|
use ReflectionClass; |
19
|
|
|
use Symfony\Component\Console\Terminal; |
20
|
|
|
use const PHP_EOL; |
21
|
|
|
use function array_column; |
22
|
|
|
use function array_map; |
23
|
|
|
use function implode; |
24
|
|
|
use function KevinGH\Box\FileSystem\file_contents; |
25
|
|
|
use function KevinGH\Box\FileSystem\filename; |
26
|
|
|
use function sprintf; |
27
|
|
|
use function str_replace; |
28
|
|
|
use function var_export; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @private |
32
|
|
|
*/ |
33
|
|
|
final class RequirementsDumper |
34
|
|
|
{ |
35
|
|
|
public const CHECK_FILE_NAME = 'check_requirements.php'; |
36
|
|
|
|
37
|
|
|
private const REQUIREMENTS_CHECKER_TEMPLATE = <<<'PHP' |
38
|
|
|
<?php |
39
|
|
|
|
40
|
|
|
/* |
41
|
|
|
* This file is part of the box project. |
42
|
|
|
* |
43
|
|
|
* (c) Kevin Herrera <[email protected]> |
44
|
|
|
* Théo Fidry <[email protected]> |
45
|
|
|
* |
46
|
|
|
* This source file is subject to the MIT license that is bundled |
47
|
|
|
* with this source code in the file LICENSE. |
48
|
|
|
*/ |
49
|
|
|
|
50
|
|
|
namespace KevinGH\Box\RequirementChecker; |
51
|
|
|
|
52
|
|
|
//__AUTOLOAD__ |
53
|
|
|
|
54
|
|
|
use KevinGH\Box\RequirementChecker\Checker; |
55
|
|
|
|
56
|
|
|
$checkPassed = Checker::checkRequirements(); |
57
|
|
|
|
58
|
|
|
if (false === $checkPassed) { |
59
|
|
|
exit(1); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
PHP; |
63
|
|
|
|
64
|
|
|
private const REQUIREMENTS_CONFIG_TEMPLATE = <<<'PHP' |
65
|
|
|
<?php |
66
|
|
|
|
67
|
|
|
return __CONFIG__; |
68
|
|
|
PHP; |
69
|
|
|
|
70
|
|
|
private const CLASSED_USED = [ |
71
|
|
|
Checker::class, |
72
|
|
|
IO::class, |
73
|
|
|
Printer::class, |
74
|
|
|
Semver::class, |
75
|
|
|
Requirement::class, |
76
|
|
|
RequirementCollection::class, |
77
|
|
|
Terminal::class, |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string[][] |
82
|
|
|
*/ |
83
|
|
|
public static function dump(array $composerLockDecodedContents): array |
84
|
|
|
{ |
85
|
|
|
$filesWithContents = [ |
86
|
|
|
self::dumpRequirementsConfig($composerLockDecodedContents), |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
foreach (self::CLASSED_USED as $class) { |
90
|
|
|
$filesWithContents[] = [ |
91
|
|
|
self::retrieveFileShortName($class), |
92
|
|
|
self::retrieveClassFileContents($class), |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$filesWithContents[] = self::dumpCheckScript(...array_column($filesWithContents, 0)); |
97
|
|
|
|
98
|
|
|
return $filesWithContents; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private static function dumpRequirementsConfig(array $composerLockDecodedContents): array |
102
|
|
|
{ |
103
|
|
|
$config = AppRequirementsFactory::create($composerLockDecodedContents); |
104
|
|
|
|
105
|
|
|
return [ |
106
|
|
|
Checker::REQUIREMENTS_CONFIG, |
107
|
|
|
str_replace( |
108
|
|
|
'__CONFIG__', |
109
|
|
|
var_export($config, true), |
110
|
|
|
self::REQUIREMENTS_CONFIG_TEMPLATE |
111
|
|
|
), |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private static function dumpCheckScript(string ...$files): array |
116
|
|
|
{ |
117
|
|
|
$autoloads = array_map( |
118
|
|
|
function (string $file): string { |
119
|
|
|
return sprintf( |
120
|
|
|
'require_once __DIR__."/%s";', |
121
|
|
|
$file |
122
|
|
|
); |
123
|
|
|
}, |
124
|
|
|
$files |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$autoloadStmt = implode(PHP_EOL, $autoloads); |
128
|
|
|
|
129
|
|
|
return [ |
130
|
|
|
self::CHECK_FILE_NAME, |
131
|
|
|
str_replace( |
132
|
|
|
'//__AUTOLOAD__', |
133
|
|
|
$autoloadStmt, |
134
|
|
|
self::REQUIREMENTS_CHECKER_TEMPLATE |
135
|
|
|
), |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private static function retrieveClassFileContents(string $class): string |
140
|
|
|
{ |
141
|
|
|
return file_contents((new ReflectionClass($class))->getFileName()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
private static function retrieveFileShortName(string $class): string |
145
|
|
|
{ |
146
|
|
|
return filename( |
147
|
|
|
(new ReflectionClass($class))->getFileName() |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|