Passed
Branch master (146bbc)
by Allan
06:55
created

Plugin::uninstall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 2
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
        $composerContextFactory = new \Vaimo\ComposerChangelogs\Factories\ComposerContextFactory($composer);
36
        $composerContext = $composerContextFactory->create();
37
        
38
        $this->bootstrapFileTree($composerContext, $namespacePrefix);
39
40
        parent::activate($composer, $io);
41
    }
42
43
    private function bootstrapFileTree(\Vaimo\ComposerChangelogs\Composer\Context $composerContext, $namespacePrefix)
44
    {
45
        $composer = $composerContext->getLocalComposer();
46
        
47
        $composerConfig = $composer->getConfig();
48
        
49
        $vendorDir = $composerConfig->get(\Vaimo\ComposerChangelogs\Composer\Config::VENDOR_DIR);
50
51
        $packageResolver = new \Vaimo\ComposerChangelogs\Resolvers\PluginPackageResolver(
52
            [$composer->getPackage()]
53
        );
54
        
55
        $pluginPackage = $packageResolver->resolveForNamespace(
56
            $composerContext->getActivePackages(), 
57
            $namespacePrefix
58
        );
59
60
        $this->createSymlink(
61
            realpath('.'),
62
            $this->composePath($vendorDir, $pluginPackage->getName()),
63
            true
64
        );
65
    }
66
67
    private function createSymlink($fromPath, $toPath, $graceful = false)
68
    {
69
        if (is_link($toPath)) {
70
            unlink($toPath);
71
        }
72
        
73
        if ($graceful && (file_exists($toPath) || !file_exists($fromPath))) {
74
            return;
75
        }
76
77
        symlink($fromPath, $toPath);
78
    }
79
    
80
    private function composePath()
81
    {
82
        $pathSegments = array_map(function ($item) {
83
            return rtrim($item, \DIRECTORY_SEPARATOR);
84
        }, func_get_args());
85
86
        return implode(
87
            DIRECTORY_SEPARATOR,
88
            array_filter($pathSegments)
89
        );
90
    }
91
92
    /**
93
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
94
     */
95
    public function deactivate(\Composer\Composer $composer, \Composer\IO\IOInterface $appIO)
96
    {
97
    }
98
99
    /**
100
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
101
     */
102
    public function uninstall(\Composer\Composer $composer, \Composer\IO\IOInterface $appIO)
103
    {
104
    }
105
}
106