Completed
Push — master ( 5a339d...a327df )
by Andrii
02:52
created

Plugin   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 71.43%

Importance

Changes 13
Bugs 2 Features 1
Metric Value
wmc 20
c 13
b 2
f 1
lcom 1
cbo 4
dl 0
loc 187
ccs 40
cts 56
cp 0.7143
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A onPostUpdate() 0 5 1
A setPackages() 0 4 1
A activate() 0 10 2
A getSubscribedEvents() 0 11 1
A onPostInstall() 0 10 2
A getPackages() 0 9 2
A scanPackages() 0 10 4
A loadPackages() 0 7 2
A runAction() 0 9 2
A getVendorDir() 0 12 3
1
<?php
2
3
/*
4
 * Composer plugin for bower/npm assets
5
 *
6
 * @link      https://github.com/hiqdev/composer-asset-plugin
7
 * @package   composer-asset-plugin
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\composerassetplugin;
13
14
use Composer\Composer;
15
use Composer\EventDispatcher\EventSubscriberInterface;
16
use Composer\IO\IOInterface;
17
use Composer\Json\JsonFile;
18
use Composer\Package\PackageInterface;
19
use Composer\Plugin\PluginInterface;
20
use Composer\Script\Event;
21
use Composer\Script\ScriptEvents;
22
23
/**
24
 * Plugin class.
25
 *
26
 * @author Andrii Vasyliev <[email protected]>
27
 */
28
class Plugin implements PluginInterface, EventSubscriberInterface
29
{
30
    /**
31
     * @var string the filename of a lock file. Defaults to `composer-asset-plugin.lock`
32
     */
33
    public $lockFile = 'composer-asset-plugin.lock';
34
35
    /**
36
     * @var Composer instance
37
     */
38
    protected $composer;
39
40
    /**
41
     * @var IOInterface
42
     */
43
    public $io;
44
45
    /**
46
     * List of the available package managers/
47
     * Initialized at activate.
48
     * @var array|PackageManager[]
49
     * @see activate
50
     */
51
    protected $managers = [
52
        'bower' => 'hiqdev\composerassetplugin\Bower',
53
        'npm' => 'hiqdev\composerassetplugin\Npm',
54
    ];
55
56
    /**
57
     * @var PackageInterface[] the array of active composer packages
58
     */
59
    protected $packages;
60
61
    /**
62
     * @var string absolute path to vendor directory.
63
     */
64
    protected $vendorDir;
65
66
    /**
67
     * Initializes the plugin object with the passed $composer and $io.
68
     * Also initializes package managers.
69
     *
70
     * @param Composer $composer
71
     * @param IOInterface $io
72
     * @void
73
     */
74 3
    public function activate(Composer $composer, IOInterface $io)
75
    {
76 3
        $managers = [];
77 3
        $this->composer = $composer;
78 3
        $this->io = $io;
79 3
        foreach ($this->managers as $name => $class) {
80 3
            $managers[$name] = new $class($this);
81
        }
82 3
        $this->managers = $managers;
83 3
    }
84
85
    /**
86
     * Returns list of events the plugin is subscribed to.
87
     *
88
     * @return array list of events
89
     */
90 1
    public static function getSubscribedEvents()
91
    {
92
        return [
93 1
            ScriptEvents::POST_INSTALL_CMD => [
94
                ['onPostInstall', 0],
95 1
            ],
96 1
            ScriptEvents::POST_UPDATE_CMD => [
97
                ['onPostUpdate', 0],
98
            ],
99
        ];
100
    }
101
102
    /**
103
     * Perform install. Called by composer after install.
104
     *
105
     * @param Event $event
106
     * @void
107
     */
108 1
    public function onPostInstall(Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
109
    {
110 1
        $lockFile = new JsonFile($this->lockFile);
111 1
        if ($lockFile->exists()) {
112
            $this->loadPackages($lockFile);
113
        } else {
114 1
            $this->scanPackages();
115
        }
116 1
        $this->runAction('install');
117 1
    }
118
119
    /**
120
     * Perform update. Called by composer after update.
121
     *
122
     * @param Event $event
123
     */
124
    public function onPostUpdate(Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
125
    {
126
        $this->scanPackages();
127
        $this->runAction('update');
128
    }
129
130
    /**
131
     * Sets [[packages]].
132
     *
133
     * @param PackageInterface[] $packages
134
     */
135 3
    public function setPackages(array $packages)
136
    {
137 3
        $this->packages = $packages;
138 3
    }
139
140
    /**
141
     * Gets [[packages]].
142
     * @return \Composer\Package\PackageInterface[]
143
     */
144 2
    public function getPackages()
145
    {
146 2
        if ($this->packages === null) {
147
            $this->packages = $this->composer->getRepositoryManager()->getLocalRepository()->getCanonicalPackages();
148
            $this->packages[] = $this->composer->getPackage();
149
        }
150
151 2
        return $this->packages;
152
    }
153
154
    /**
155
     * Scan packages from the composer objects.
156
     * @void
157
     */
158 1
    protected function scanPackages()
159
    {
160 1
        foreach ($this->getPackages() as $package) {
161
            if ($package instanceof \Composer\Package\CompletePackage) {
162
                foreach ($this->managers as $manager) {
163
                    $manager->scanPackage($package);
164
                }
165
            }
166
        }
167 1
    }
168
169
    /**
170
     * Load packages from given lock file.
171
     *
172
     * @param JsonFile $lockFile
173
     * @void
174
     */
175
    protected function loadPackages(JsonFile $lockFile)
176
    {
177
        $lock = $lockFile->read();
178
        foreach ($this->managers as $name => $m) {
179
            $m->setConfig($lock[$name]);
180
        }
181
    }
182
183
    /**
184
     * Install packages after loading/scanning.
185
     * @param string $action
186
     * @void
187
     */
188 1
    protected function runAction($action)
189
    {
190 1
        $dir = getcwd();
191 1
        chdir($this->getVendorDir());
192 1
        foreach ($this->managers as $m) {
193 1
            $m->runAction($action);
194
        }
195 1
        chdir($dir);
196 1
    }
197
198
    /**
199
     * Get absolute path to composer vendor dir.
200
     * @return string
201
     */
202 1
    public function getVendorDir()
203
    {
204 1
        if ($this->vendorDir === null) {
205 1
            $dir = $this->composer->getConfig()->get('vendor-dir');
206 1
            if ($dir[0] !== '/') {
207
                $dir = dirname(dirname(__DIR__)) . $dir;
208
            }
209 1
            $this->vendorDir = $dir;
210
        }
211
212 1
        return $this->vendorDir;
213
    }
214
}
215