Passed
Pull Request — master (#116)
by Théo
02:06
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 function basename;
18
use Composer\Semver\Semver;
19
use const DIRECTORY_SEPARATOR;
20
use ReflectionClass;
21
use Symfony\Component\Console\Terminal;
22
use const PHP_EOL;
23
use function array_column;
24
use function array_map;
25
use function implode;
26
use function KevinGH\Box\FileSystem\file_contents;
27
use function KevinGH\Box\FileSystem\filename;
28
use function sprintf;
29
use function str_replace;
30
use Symfony\Component\Finder\Finder;
31
use Symfony\Component\Finder\SplFileInfo;
32
use function var_export;
33
use function xdebug_break;
34
35
/**
36
 * @private
37
 */
38
final class RequirementsDumper
39
{
40
    public const CHECK_FILE_NAME = 'check_requirements.php';
41
42
    private const REQUIREMENTS_CHECKER_TEMPLATE = <<<'PHP'
43
<?php
44
45
/*
46
 * This file is part of the box project.
47
 *
48
 * (c) Kevin Herrera <[email protected]>
49
 *     Théo Fidry <[email protected]>
50
 *
51
 * This source file is subject to the MIT license that is bundled
52
 * with this source code in the file LICENSE.
53
 */
54
55
namespace KevinGH\Box\RequirementChecker;
56
57
require 'bin/check-requirements.php';
58
59
PHP;
60
61
    private const REQUIREMENTS_CONFIG_TEMPLATE = <<<'PHP'
62
<?php
63
64
return __CONFIG__;
65
PHP;
66
67
    private const REQUIRMEMENT_CHECKER_PATH = __DIR__.'/../../.requirement-checker';
68
69
    private const FILES_USED = [
70
//        __DIR__.'/Checker.php',
71
//        __DIR__.'/IO.php',
72
//        __DIR__.'/Printer.php',
73
//        __DIR__.'/../../vendor/composer/semver/src/Semver.php',
74
//        __DIR__.'/../../vendor/composer/semver/src/VersionParser.php',
75
//        __DIR__.'/../../vendor/composer/semver/src/Constraint/ConstraintInterface.php',
76
//        __DIR__.'/../../vendor/composer/semver/src/Constraint/EmptyConstraint.php',
77
//        __DIR__.'/../../vendor/composer/semver/src/Constraint/MultiConstraint.php',
78
//        __DIR__.'/../../vendor/composer/semver/src/Constraint/Constraint.php',
79
//        __DIR__.'/Requirement.php',
80
//        __DIR__.'/RequirementCollection.php',
81
//        __DIR__.'/../../vendor/symfony/console/Terminal.php',
82
        __DIR__.'/../check-requirements.phar',
83
    ];
84
85
    /**
86
     * @return string[][]
87
     */
88
    public static function dump(array $composerLockDecodedContents): array
89
    {
90
        $filesWithContents = [
91
            self::dumpRequirementsConfig($composerLockDecodedContents),
92
        ];
93
94
        /** @var SplFileInfo[] $requirementCheckerFiles */
95
        $requirementCheckerFiles = Finder::create()
96
            ->files()
97
            ->in(self::REQUIRMEMENT_CHECKER_PATH)
98
        ;
99
100
        foreach ($requirementCheckerFiles as $file) {
101
            $x = $file->getPathname();
0 ignored issues
show
Unused Code introduced by
The assignment to $x is dead and can be removed.
Loading history...
102
103
            $filesWithContents[] = [
104
                $file->getRelativePathname(),
105
                file_contents($file->getPathname()),
106
            ];
107
        }
108
109
        $filesWithContents[] = self::dumpCheckScript(...array_column($filesWithContents, 0));
110
111
        return $filesWithContents;
112
    }
113
114
    private static function dumpRequirementsConfig(array $composerLockDecodedContents): array
115
    {
116
        $config = AppRequirementsFactory::create($composerLockDecodedContents);
117
118
        return [
119
            '.requirements.php',
120
            str_replace(
121
                '__CONFIG__',
122
                var_export($config, true),
123
                self::REQUIREMENTS_CONFIG_TEMPLATE
124
            ),
125
        ];
126
    }
127
128
    private static function dumpCheckScript(string ...$files): array
129
    {
130
        $autoloads = array_map(
131
            function (string $file): string {
132
                return sprintf(
133
                    'require_once __DIR__."/%s";',
134
                    $file
135
                );
136
            },
137
            $files
138
        );
139
140
        $autoloadStmt = implode(PHP_EOL, $autoloads);
0 ignored issues
show
Unused Code introduced by
The assignment to $autoloadStmt is dead and can be removed.
Loading history...
141
142
        $autoloadStmt = '';
143
144
        return [
145
            self::CHECK_FILE_NAME,
146
            str_replace(
147
                '//__AUTOLOAD__',
148
                $autoloadStmt,
149
                self::REQUIREMENTS_CHECKER_TEMPLATE
150
            ),
151
        ];
152
    }
153
}
154