Completed
Pull Request — master (#116)
by Théo
02:32
created

RequirementsDumper::dump()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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