|
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\Plugin\PluginInterface; |
|
19
|
|
|
use Composer\Script\Event; |
|
20
|
|
|
use Composer\Script\ScriptEvents; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Plugin class. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class Plugin implements PluginInterface, EventSubscriberInterface |
|
28
|
|
|
{ |
|
29
|
|
|
public $file = 'composer-asset-plugin.lock'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Composer |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $composer; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var IOInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
public $io; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* List of package managers instances. |
|
43
|
|
|
* Initialized at activate. |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $managers = ['bower', 'npm']; |
|
47
|
|
|
|
|
48
|
|
|
protected $packages; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Initializes the plugin object with passed $composer and $io. |
|
52
|
|
|
* Also initializes package managers. |
|
53
|
|
|
* |
|
54
|
|
|
* @param Composer $composer |
|
55
|
|
|
* @param IOInterface $io |
|
56
|
|
|
*/ |
|
57
|
|
|
public function activate(Composer $composer, IOInterface $io) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->composer = $composer; |
|
60
|
|
|
$this->io = $io; |
|
61
|
|
|
foreach ($this->managers as $m) { |
|
62
|
|
|
$class = 'hiqdev\composerassetplugin\\' . ucfirst($m); |
|
63
|
|
|
$managers[$m] = new $class($this); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
$this->managers = $managers; |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns list of events the plugin wants to listen. |
|
70
|
|
|
* |
|
71
|
|
|
* @return array list of events |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function getSubscribedEvents() |
|
74
|
|
|
{ |
|
75
|
|
|
return [ |
|
76
|
|
|
ScriptEvents::POST_INSTALL_CMD => [ |
|
77
|
|
|
['onPostInstall', 0], |
|
78
|
|
|
], |
|
79
|
|
|
ScriptEvents::POST_UPDATE_CMD => [ |
|
80
|
|
|
['onPostUpdate', 0], |
|
81
|
|
|
], |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Perform install. Called by composer after install. |
|
87
|
|
|
* @param Event $event |
|
88
|
|
|
*/ |
|
89
|
|
|
public function onPostInstall(Event $event) |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
$lockFile = new JsonFile($this->file); |
|
92
|
|
|
if ($lockFile->exists()) { |
|
93
|
|
|
$this->loadPackages($lockFile); |
|
94
|
|
|
} else { |
|
95
|
|
|
$this->scanPackages(); |
|
96
|
|
|
} |
|
97
|
|
|
$this->runAction('install'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Perform update. Called by composer after update. |
|
102
|
|
|
* @param Event $event |
|
103
|
|
|
*/ |
|
104
|
|
|
public function onPostUpdate(Event $event) |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
$this->scanPackages(); |
|
107
|
|
|
$this->runAction('update'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function setPackages(array $packages) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->packages = $packages; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function getPackages() |
|
116
|
|
|
{ |
|
117
|
|
|
if ($this->packages === null) { |
|
118
|
|
|
$this->packages = $this->composer->getRepositoryManager()->getLocalRepository()->getCanonicalPackages(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $this->packages; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Scan packages from the composer object. |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function scanPackages() |
|
128
|
|
|
{ |
|
129
|
|
|
foreach ($this->getPackages() as $package) { |
|
130
|
|
|
if ($package instanceof \Composer\Package\CompletePackage) { |
|
131
|
|
|
foreach ($this->managers as $m) { |
|
132
|
|
|
$m->scanPackage($package); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Load packages from given lock file. |
|
140
|
|
|
* @param JsonFile $lockFile |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function loadPackages(JsonFile $lockFile) |
|
143
|
|
|
{ |
|
144
|
|
|
$lock = $lockFile->read(); |
|
145
|
|
|
foreach ($this->managers as $name => $m) { |
|
146
|
|
|
$m->setConfig($lock[$name]); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Install packages after loading/scanning. |
|
152
|
|
|
* @param string $action |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function runAction($action) |
|
155
|
|
|
{ |
|
156
|
|
|
chdir($this->getVendorDir()); |
|
157
|
|
|
foreach ($this->managers as $m) { |
|
158
|
|
|
$m->runAction($action); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get path to vendor dir from composer. |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getVendorDir() |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->composer->getConfig()->get('vendor-dir'); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.