Passed
Branch master (3e058b)
by Allan
02:59
created

Plugin::bootstrapAutoloader()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 27
c 0
b 0
f 0
rs 9.4888
cc 5
nc 1
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Plugin::composePath() 0 9 1
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\ComposerChangelogsProxy;
7
8
require_once 'src/Plugin.php';
9
10
/**
11
 * @SuppressWarnings(PHPMD.ShortVariable)
12
 */
13
class Plugin extends \Vaimo\ComposerChangelogs\Plugin
14
{
15
    public function activate(\Composer\Composer $composer, \Composer\IO\IOInterface $io)
16
    {
17
        $namespacePrefix = implode('\\', array_slice(explode('\\', get_parent_class($this)), 0, 2)) . '\\';
18
        
19
        $autoloadFile = $this->composePath(
20
            $composer->getConfig()->get('vendor-dir'), 
21
            'autoload.php'
22
        );
23
24
        /**
25
         * When running through the initial installation, make sure that installing the proxy
26
         * command (to get the changelog commands) does not result in crashing the whole
27
         * installation process.
28
         */
29
        if (!file_exists($autoloadFile)) {
30
            return;
31
        }
32
        
33
        include $autoloadFile;
34
35
        $this->bootstrapFileTree($composer, $namespacePrefix);
36
37
        parent::activate($composer, $io);
38
    }
39
40
    private function bootstrapFileTree(\Composer\Composer $composer, $namespacePrefix)
41
    {
42
        $composerConfig = $composer->getConfig();
43
        $repositoryManager = $composer->getRepositoryManager();
44
        
45
        $localRepository = $repositoryManager->getLocalRepository();
46
47
        $vendorDir = $composerConfig->get(\Vaimo\ComposerChangelogs\Composer\Config::VENDOR_DIR);
48
49
        $packageResolver = new \Vaimo\ComposerChangelogs\Resolvers\PluginPackageResolver(
50
            [$composer->getPackage()]
51
        );
52
        
53
        $pluginPackage = $packageResolver->resolveForNamespace($localRepository, $namespacePrefix);
54
55
        $this->createSymlink(
56
            realpath('.'),
57
            $this->composePath($vendorDir, $pluginPackage->getName()),
58
            true
59
        );
60
    }
61
62
    private function createSymlink($fromPath, $toPath, $graceful = false)
63
    {
64
        if (is_link($toPath)) {
65
            unlink($toPath);
66
        }
67
        
68
        if ($graceful && (file_exists($toPath) || !file_exists($fromPath))) {
69
            return;
70
        }
71
72
        symlink($fromPath, $toPath);
73
    }
74
    
75
    private function composePath()
76
    {
77
        $pathSegments = array_map(function ($item) {
78
            return rtrim($item, \DIRECTORY_SEPARATOR);
79
        }, func_get_args());
80
81
        return implode(
82
            DIRECTORY_SEPARATOR,
83
            array_filter($pathSegments)
84
        );
85
    }
86
}
87