Completed
Push — master ( 6b2da6...16609d )
by Théo
06:42
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 Humbug\PhpScoper\Configuration as PhpScoperConfiguration;
21
use InvalidArgumentException;
22
use function KevinGH\Box\FileSystem\dump_file;
23
use function KevinGH\Box\FileSystem\file_contents;
24
use function preg_match;
25
use function preg_replace;
26
27
final class ComposerOrchestrator
28
{
29
    private function __construct()
30
    {
31
    }
32
33
    public static function dumpAutoload(?PhpScoperConfiguration $phpScoperConfig): void
34
    {
35
        try {
36
            $composer = Factory::create(new NullIO(), null, true);
37
        } catch (InvalidArgumentException $exception) {
38
            if (1 !== preg_match('//', 'could not find a composer\.json file')) {
39
                throw $exception;
40
            }
41
42
            return; // No autoload to dump
43
        }
44
45
        $installationManager = $composer->getInstallationManager();
46
        $localRepository = $composer->getRepositoryManager()->getLocalRepository();
47
        $package = $composer->getPackage();
48
        $composerConfig = $composer->getConfig();
49
50
        $generator = $composer->getAutoloadGenerator();
51
        $generator->setDevMode(false);
52
        $generator->setClassMapAuthoritative(true);
53
54
        $generator->dump($composerConfig, $localRepository, $package, $installationManager, 'composer', true);
55
56
        if (null !== $phpScoperConfig) {
57
            $autoloadFile = $composerConfig->get('vendor-dir').'/autoload.php';
58
59
            $autoloadContents = self::generateAutoloadStatements(
60
                $phpScoperConfig,
61
                file_contents($autoloadFile)
62
            );
63
64
            dump_file($autoloadFile, $autoloadContents);
65
        }
66
    }
67
68
    private static function generateAutoloadStatements(PhpScoperConfiguration $config, string $autoload): string
69
    {
70
        // TODO: make prefix configurable: https://github.com/humbug/php-scoper/issues/178
71
        $whitelistStatements = (new ScoperAutoloadGenerator($config->getWhitelist()))->dump('_HumbugBox');
72
73
        $whitelistStatements = preg_replace(
74
            '/(\\$loader \= .*)|(return \\$loader;)/',
75
            '',
76
            str_replace('<?php', '', $whitelistStatements)
77
        );
78
79
        return preg_replace(
80
            '/return (ComposerAutoloaderInit.+::getLoader\(\));/',
81
            <<<PHP
82
\$loader = \$1;
83
84
$whitelistStatements
85
86
return \$loader;
87
PHP
88
            ,
89
            $autoload
90
        );
91
    }
92
}
93