Passed
Pull Request — master (#116)
by Théo
02:27
created

RequirementsDumper::dumpCheckScript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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