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

RequirementsDumper::dumpRequirementsConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
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 const PHP_EOL;
19
use function array_column;
20
use function array_map;
21
use function implode;
22
use function KevinGH\Box\FileSystem\file_contents;
23
use function KevinGH\Box\FileSystem\filename;
24
use function sprintf;
25
use function str_replace;
26
use Symfony\Component\Console\Terminal;
27
use function var_export;
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
    private const CLASSED_USED = [
70
        Checker::class,
71
        IO::class,
72
        Printer::class,
73
        Requirement::class,
74
        RequirementCollection::class,
75
        Terminal::class,
76
    ];
77
78
    /**
79
     * @return string[][]
80
     */
81
    public static function dump(string $composerJson): array
82
    {
83
        $filesWithContents = [
84
            self::dumpRequirementsConfig($composerJson),
85
        ];
86
87
        foreach (self::CLASSED_USED as $class) {
88
            $filesWithContents[] = [
89
                self::retrieveFileShortName($class),
90
                self::retrieveClassFileContents($class),
91
            ];
92
        }
93
94
        $filesWithContents[] = self::dumpCheckScript(...array_column($filesWithContents, 0));
95
96
        return $filesWithContents;
97
    }
98
99
    private static function dumpRequirementsConfig(string $composerJson): array
100
    {
101
        $config = AppRequirementsFactory::create($composerJson);
102
103
        return [
104
            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...
105
            str_replace(
106
                '__CONFIG__',
107
                var_export($config, true),
108
                self::REQUIREMENTS_CONFIG_TEMPLATE
109
            ),
110
        ];
111
    }
112
113
    private static function dumpCheckScript(string ...$files): array
114
    {
115
        $autoloads = array_map(
116
            function (string $file): string {
117
                return sprintf(
118
                    'require_once __DIR__."/%s";',
119
                    $file
120
                );
121
            },
122
            $files
123
        );
124
125
        $autoloadStmt = implode(PHP_EOL, $autoloads);
126
127
        return [
128
            self::CHECK_FILE_NAME,
129
            str_replace(
130
                '//__AUTOLOAD__',
131
                $autoloadStmt,
132
                self::REQUIREMENTS_CHECKER_TEMPLATE
133
            ),
134
        ];
135
    }
136
137
    private static function retrieveClassFileContents(string $class): string
138
    {
139
        return file_contents((new ReflectionClass($class))->getFileName());
140
    }
141
142
    private static function retrieveFileShortName(string $class): string
143
    {
144
        return filename(
145
            (new ReflectionClass($class))->getFileName()
146
        );
147
    }
148
}
149