Passed
Pull Request — master (#31)
by Théo
02:18
created

ComposerOrchestrator::dumpAutoload()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 3
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\Composer;
16
17
use Composer\Console\Application as ComposerApplication;
18
use Humbug\PhpScoper\Autoload\ScoperAutoloadGenerator;
19
use Humbug\PhpScoper\Configuration as PhpScoperConfiguration;
20
use Symfony\Component\Console\Input\ArrayInput;
21
use Symfony\Component\Console\Output\NullOutput;
22
use function KevinGH\Box\FileSystem\dump_file;
23
24
final class ComposerOrchestrator
25
{
26
    private function __construct()
27
    {
28
    }
29
30
    public static function dumpAutoload(?PhpScoperConfiguration $configuration): void
31
    {
32
        // TODO: we are running Composer a first time to assign ComposerApplication#io. However it should be possible
33
        // to do without by patching Composer at the core to construct the application with a NullIO
34
        $composerApplication = new ComposerApplication();
35
        $composerApplication->doRun(new ArrayInput(['--no-plugins' => null]), new NullOutput());
36
37
        $composer = $composerApplication->getComposer(false, true);
38
39
        if (null === $composer) {
40
            return; // No autoload to dump
41
        }
42
43
        $installationManager = $composer->getInstallationManager();
44
        $localRepo = $composer->getRepositoryManager()->getLocalRepository();
45
        $package = $composer->getPackage();
46
        $config = $composer->getConfig();
47
48
        $generator = $composer->getAutoloadGenerator();
49
        $generator->setDevMode(false);
50
        $generator->setClassMapAuthoritative(true);
51
52
        if (null !== $configuration) {
53
            // TODO: make prefix configurable
54
            $autoload = (new ScoperAutoloadGenerator($configuration->getWhitelist()))->dump('_HumbugBox');
55
56
            // TODO: handle custom vendor dir
57
            // TODO: expose the scoper autoload file name via a constant
58
            dump_file('vendor/scoper-autoload.php', $autoload);
59
        }
60
61
        $generator->dump($config, $localRepo, $package, $installationManager, 'composer', true);
62
    }
63
}
64