Passed
Push — master ( 18882f...0808ff )
by Théo
02:13
created

ComposerOrchestrator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
eloc 0
nc 1
nop 0
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\Composer;
16
17
use Composer\Factory;
18
use Composer\IO\NullIO;
19
use Humbug\PhpScoper\Autoload\ScoperAutoloadGenerator;
20
use InvalidArgumentException;
21
use function KevinGH\Box\FileSystem\dump_file;
22
use function KevinGH\Box\FileSystem\file_contents;
23
use function preg_match;
24
use function preg_replace;
25
26
/**
27
 * @private
28
 */
29
final class ComposerOrchestrator
30
{
31
    private function __construct()
32
    {
33
    }
34
35
    /**
36
     * @param string[] $whitelist
37
     */
38
    public static function dumpAutoload(array $whitelist, string $prefix): void
39
    {
40
        try {
41
            $composer = Factory::create(new NullIO(), null, true);
42
        } catch (InvalidArgumentException $exception) {
43
            if (1 !== preg_match('//', 'could not find a composer\.json file')) {
44
                throw $exception;
45
            }
46
47
            return; // No autoload to dump
48
        }
49
50
        $installationManager = $composer->getInstallationManager();
51
        $localRepository = $composer->getRepositoryManager()->getLocalRepository();
52
        $package = $composer->getPackage();
53
        $composerConfig = $composer->getConfig();
54
55
        $generator = $composer->getAutoloadGenerator();
56
        $generator->setDevMode(false);
57
        $generator->setClassMapAuthoritative(true);
58
59
        $generator->dump($composerConfig, $localRepository, $package, $installationManager, 'composer', true);
60
61
        if ('' !== $prefix) {
62
            $autoloadFile = $composerConfig->get('vendor-dir').'/autoload.php';
63
64
            $autoloadContents = self::generateAutoloadStatements(
65
                $whitelist,
66
                $prefix,
67
                file_contents($autoloadFile)
68
            );
69
70
            dump_file($autoloadFile, $autoloadContents);
71
        }
72
    }
73
74
    /**
75
     * @param string[] $whitelist
76
     */
77
    private static function generateAutoloadStatements(array $whitelist, string $prefix, string $autoload): string
78
    {
79
        // TODO: make prefix configurable: https://github.com/humbug/php-scoper/issues/178
80
        $whitelistStatements = (new ScoperAutoloadGenerator($whitelist))->dump($prefix);
81
82
        if ([] === $whitelistStatements) {
0 ignored issues
show
introduced by
The condition array() === $whitelistStatements is always false.
Loading history...
83
            return $autoload;
84
        }
85
86
        $whitelistStatements = preg_replace(
87
            '/(\\$loader \= .*)|(return \\$loader;)/',
88
            '',
89
            str_replace('<?php', '', $whitelistStatements)
90
        );
91
92
        return preg_replace(
93
            '/return (ComposerAutoloaderInit.+::getLoader\(\));/',
94
            <<<PHP
95
\$loader = \$1;
96
97
$whitelistStatements
98
99
return \$loader;
100
PHP
101
            ,
102
            $autoload
103
        );
104
    }
105
}
106