Completed
Pull Request — master (#116)
by Théo
04:23 queued 01:53
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 ReflectionClass;
18
use Symfony\Requirements\Requirement;
19
use Symfony\Requirements\RequirementCollection;
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
        RequirementCollection::class,
73
        Requirement::class,
74
    ];
75
76
    /**
77
     * @return string[][]
78
     */
79
    public static function dump(string $composerJson): array
80
    {
81
        $filesWithContents = [
82
            self::dumpRequirementsConfig($composerJson),
83
        ];
84
85
        foreach (self::CLASSED_USED as $class) {
86
            $filesWithContents[] = [
87
                self::retrieveFileShortName($class),
88
                self::retrieveClassFileContents($class),
89
            ];
90
        }
91
92
        $filesWithContents[] = self::dumpCheckScript(...array_column($filesWithContents, 0));
93
94
        return $filesWithContents;
95
    }
96
97
    private static function dumpRequirementsConfig(string $composerJson): array
98
    {
99
        $config = AppRequirementsFactory::create($composerJson);
100
101
        return [
102
            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...
103
            str_replace(
104
                '__CONFIG__',
105
                var_export($config, true),
106
                self::REQUIREMENTS_CONFIG_TEMPLATE
107
            ),
108
        ];
109
    }
110
111
    private static function dumpCheckScript(string ...$files): array
112
    {
113
        $autoloads = array_map(
114
            function (string $file): string {
115
                return sprintf(
116
                    'require_once __DIR__."/%s";',
117
                    $file
118
                );
119
            },
120
            $files
121
        );
122
123
        $autoloadStmt = implode(PHP_EOL, $autoloads);
124
125
        return [
126
            self::CHECK_FILE_NAME,
127
            str_replace(
128
                '//__AUTOLOAD__',
129
                $autoloadStmt,
130
                self::REQUIREMENTS_CHECKER_TEMPLATE
131
            ),
132
        ];
133
    }
134
135
    private static function retrieveClassFileContents(string $class): string
136
    {
137
        return file_contents((new ReflectionClass($class))->getFileName());
138
    }
139
140
    private static function retrieveFileShortName(string $class): string
141
    {
142
        return filename(
143
            (new ReflectionClass($class))->getFileName()
144
        );
145
    }
146
}
147