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

ComposerOrchestrator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
B dumpAutoload() 0 30 3
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
        $composerApplication = new ComposerApplication();
33
        $composerApplication->doRun(new ArrayInput([]), new NullOutput());
34
35
        $composer = $composerApplication->getComposer(false);
36
37
        if (null === $composer) {
38
            return; // No autoload to dump
39
        }
40
41
        $installationManager = $composer->getInstallationManager();
42
        $localRepo = $composer->getRepositoryManager()->getLocalRepository();
43
        $package = $composer->getPackage();
44
        $config = $composer->getConfig();
45
46
        $generator = $composer->getAutoloadGenerator();
47
        $generator->setDevMode(false);
48
        $generator->setClassMapAuthoritative(true);
49
50
        if (null !== $configuration) {
51
            // TODO: make prefix configurable
52
            $autoload = (new ScoperAutoloadGenerator($configuration->getWhitelist()))->dump('_HumbugBox');
53
54
            // TODO: handle custom vendor dir
55
            // TODO: expose the scoper autoload file name via a constant
56
            dump_file('vendor/scoper-autoload.php', $autoload);
57
        }
58
59
        $generator->dump($config, $localRepo, $package, $installationManager, 'composer', true);
60
    }
61
}
62