Passed
Push — master ( 826a3d...cef8d3 )
by Allan
02:36 queued 10s
created

Plugin::composePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatchesProxy;
7
8
require_once 'src/Plugin.php';
9
10
class Plugin extends \Vaimo\ComposerPatches\Plugin
11
{
12
    public function activate(\Composer\Composer $composer, \Composer\IO\IOInterface $cliIO)
13
    {
14
        $namespacePrefix = implode('\\', array_slice(explode('\\', get_parent_class($this)), 0, 2)) . '\\';
15
        
16
        $autoloadFile = $this->composePath(
17
            $composer->getConfig()->get('vendor-dir'),
18
            'autoload.php'
19
        );
20
21
        /**
22
         * When running through the initial installation, make sure that installing the proxy
23
         * command (to get the changelog commands) does not result in crashing the whole
24
         * installation process.
25
         */
26
        if (!file_exists($autoloadFile)) {
27
            return;
28
        }
29
30
        include $autoloadFile;
31
        
32
        $this->bootstrapFileTree($composer, $namespacePrefix);
33
34
        parent::activate($composer, $cliIO);
35
    }
36
37
    private function bootstrapFileTree(\Composer\Composer $composer, $namespacePrefix)
38
    {
39
        $composerConfig = $composer->getConfig();
40
        $repositoryManager = $composer->getRepositoryManager();
41
        
42
        $localRepository = $repositoryManager->getLocalRepository();
43
44
        $vendorDir = $composerConfig->get(\Vaimo\ComposerPatches\Composer\ConfigKeys::VENDOR_DIR);
45
46
        $packageResolver = new \Vaimo\ComposerPatches\Composer\Plugin\PackageResolver(
47
            [$composer->getPackage()]
48
        );
49
        
50
        $pluginPackage = $packageResolver->resolveForNamespace($localRepository, $namespacePrefix);
51
52
        $this->createSymlink(
53
            realpath('.'),
54
            $this->composePath($vendorDir, $pluginPackage->getName()),
55
            true
56
        );
57
    }
58
59
    private function createSymlink($fromPath, $toPath, $graceful = false)
60
    {
61
        if (is_link($toPath)) {
62
            unlink($toPath);
63
        }
64
        
65
        if ($graceful && (file_exists($toPath) || !file_exists($fromPath))) {
66
            return;
67
        }
68
69
        symlink($fromPath, $toPath);
70
    }
71
    
72
    private function composePath()
73
    {
74
        $pathSegments = array_map(function ($item) {
75
            return rtrim($item, \DIRECTORY_SEPARATOR);
76
        }, func_get_args());
77
78
        return implode(
79
            DIRECTORY_SEPARATOR,
80
            array_filter($pathSegments)
81
        );
82
    }
83
}
84