Completed
Push — master ( 316e30...579556 )
by Andrii
60:17
created

Plugin::findPackage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 6.2801

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 8
ccs 2
cts 7
cp 0.2857
rs 9.4286
cc 3
eloc 4
nc 3
nop 1
crap 6.2801
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 3
        }
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 1
                ['onPostInstall', 0],
95 1
            ],
96 1
            ScriptEvents::POST_UPDATE_CMD => [
97 1
                ['onPostUpdate', 0],
98 1
            ],
99 1
        ];
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
     * Returns package with given name if exists.
156
     * @param string $name package name
157
     * @return \Composer\Package\PackageInterface|null
158 1
     */
159
    public function findPackage($name)
160 1
    {
161
        foreach ($this->getPackages() as $package) {
162
            if ($name === $package->getName()) {
163
                return $package;
164
            }
165
        }
166 1
    }
167 1
168
    /**
169
     * Scan packages from the composer objects.
170
     * @void
171
     */
172
    protected function scanPackages()
173
    {
174
        foreach ($this->getPackages() as $package) {
175
            if ($package instanceof \Composer\Package\CompletePackageInterface) {
176
                foreach ($this->managers as $manager) {
177
                    $manager->scanPackage($package);
178
                }
179
            }
180
        }
181
    }
182
183
    /**
184
     * Load packages from given lock file.
185
     *
186
     * @param JsonFile $lockFile
187
     * @void
188 1
     */
189
    protected function loadPackages(JsonFile $lockFile)
190 1
    {
191 1
        $lock = $lockFile->read();
192 1
        foreach ($this->managers as $name => $m) {
193 1
            $m->setConfig($lock[$name]);
194
        }
195
    }
196 1
197 1
    /**
198 1
     * Install packages after loading/scanning.
199
     * @param string $action
200
     * @void
201
     */
202
    protected function runAction($action)
203
    {
204 1
        $dir = getcwd();
205
        chdir($this->getVendorDir());
206 1
        foreach ($this->managers as $m) {
207 1
            if ($m->hasDependencies()) {
208 1
                $m->runAction($action);
209
            }
210
        }
211 1
        chdir($dir);
212 1
    }
213
214 1
    /**
215
     * Get absolute path to composer vendor dir.
216
     * @return string
217
     */
218
    public function getVendorDir()
219
    {
220
        if ($this->vendorDir === null) {
221
            $this->vendorDir = $this->composer->getConfig()->get('vendor-dir', '/');
222
        }
223
224
        return $this->vendorDir;
225
    }
226
}
227