Passed
Push — master ( 99b058...aba4a6 )
by Allan
07:10 queued 11s
created

Plugin::resetPackages()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches;
7
8
use Composer\EventDispatcher\ScriptExecutionException;
9
10
/**
11
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
12
 */
13
class Plugin implements
14
    \Composer\Plugin\PluginInterface,
15
    \Composer\EventDispatcher\EventSubscriberInterface,
16
    \Composer\Plugin\Capable
17
{
18
    /**
19
     * @var \Vaimo\ComposerPatches\Package\OperationAnalyser
20
     */
21
    private $operationAnalyser;
22
23
    /**
24
     * @var \Vaimo\ComposerPatches\Strategies\BootstrapStrategy
25
     */
26
    private $bootstrapStrategy;
27
28
    /**
29
     * @var \Vaimo\ComposerPatches\Factories\BootstrapFactory
30
     */
31
    private $bootstrapFactory;
32
33
    /**
34
     * @var \Vaimo\ComposerPatches\Bootstrap
35
     */
36
    private $bootstrap;
37
38
    /**
39
     * @var \Vaimo\ComposerPatches\Repository\Lock\Sanitizer
40
     */
41
    private $lockSanitizer;
42
43
    /**
44
     * @var string[]
45
     */
46
    private $capabilitiesConfig = array();
47
48
    public function activate(\Composer\Composer $composer, \Composer\IO\IOInterface $appIO)
49
    {
50
        $contextFactory = new \Vaimo\ComposerPatches\Factories\ComposerContextFactory($composer);
51
        $composerContext = $contextFactory->create();
52
        
53
        $configFactory = new \Vaimo\ComposerPatches\Factories\ConfigFactory($composerContext);
54
55
        $this->operationAnalyser = new \Vaimo\ComposerPatches\Package\OperationAnalyser();
56
        $this->bootstrapFactory = new \Vaimo\ComposerPatches\Factories\BootstrapFactory($composerContext, $appIO);
57
        $this->lockSanitizer = new \Vaimo\ComposerPatches\Repository\Lock\Sanitizer($appIO);
58
        $this->bootstrapStrategy = new \Vaimo\ComposerPatches\Strategies\BootstrapStrategy($composerContext);
59
        
60
        $this->bootstrap = $this->bootstrapFactory->create($configFactory);
61
62
        $pluginBootstrap = new \Vaimo\ComposerPatches\Composer\Plugin\Bootstrap($composer, $composerContext);
63
        $pluginBootstrap->preloadPluginClasses();
64
65
        if (!interface_exists('\Composer\Plugin\Capability\CommandProvider')
66
            || !$this->bootstrapStrategy->shouldAllow()
67
        ) {
68
            return;
69
        }
70
71
        $this->capabilitiesConfig = array(
72
            'Composer\Plugin\Capability\CommandProvider' => '\Vaimo\ComposerPatches\Composer\CommandsProvider',
73
        );
74
    }
75
76
    public function getCapabilities()
77
    {
78
        return $this->capabilitiesConfig;
79
    }
80
81
    public static function getSubscribedEvents()
82
    {
83
        return array(
84
            \Composer\Script\ScriptEvents::PRE_AUTOLOAD_DUMP => 'postInstall',
85
            \Composer\Installer\PackageEvents::PRE_PACKAGE_UNINSTALL => 'resetPackages'
86
        );
87
    }
88
89
    public function postInstall(\Composer\Script\Event $event)
90
    {
91
        if (!$this->bootstrap) {
92
            $this->lockSanitizer->sanitize();
93
94
            return;
95
        }
96
97
        if (!$this->bootstrapStrategy->shouldAllow()) {
98
            $this->lockSanitizer->sanitize();
99
100
            return;
101
        }
102
103
        $runtimeUtils = new \Vaimo\ComposerPatches\Utils\RuntimeUtils();
104
105
        $lockSanitizer = $this->lockSanitizer;
106
        $bootstrap = $this->bootstrap;
107
108
        $result = $runtimeUtils->executeWithPostAction(
109
            function () use ($bootstrap, $event) {
110
                return $bootstrap->applyPatches($event->isDevMode());
111
            },
112
            function () use ($event, $lockSanitizer) {
113
                $repository = $event->getComposer()->getRepositoryManager()->getLocalRepository();
114
115
                $repository->write($event->isDevMode(), $event->getComposer()->getInstallationManager());
116
                $lockSanitizer->sanitize();
117
            }
118
        );
119
120
        if ($result) {
121
            return;
122
        }
123
124
        throw new ScriptExecutionException('Execution halted due to composer-patch failure', 1);
125
    }
126
127
    public function resetPackages(\Composer\Installer\PackageEvent $event)
128
    {
129
        if (!$this->operationAnalyser->isPatcherUninstallOperation($event->getOperation())) {
130
            return;
131
        }
132
133
        if (!getenv(\Vaimo\ComposerPatches\Environment::SKIP_CLEANUP)) {
134
            $this->bootstrap->stripPatches();
135
        }
136
137
        $this->bootstrap = null;
138
    }
139
140
    /**
141
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
142
     */
143
    public function deactivate(\Composer\Composer $composer, \Composer\IO\IOInterface $appIO)
144
    {
145
    }
146
147
    /**
148
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
149
     */
150
    public function uninstall(\Composer\Composer $composer, \Composer\IO\IOInterface $appIO)
151
    {
152
    }
153
}
154