-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathFxpAssetPlugin.php
284 lines (250 loc) · 8.63 KB
/
FxpAssetPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/*
* This file is part of the Fxp Composer Asset Plugin package.
*
* (c) François Pluchino <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Fxp\Composer\AssetPlugin;
use Composer\Composer;
use Composer\DependencyResolver\Pool;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\InstallerEvent;
use Composer\Installer\InstallerEvents;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Repository\RepositoryInterface;
use Composer\Repository\RepositoryManager;
use Fxp\Composer\AssetPlugin\Event\VcsRepositoryEvent;
use Fxp\Composer\AssetPlugin\Installer\AssetInstaller;
use Fxp\Composer\AssetPlugin\Installer\BowerInstaller;
use Fxp\Composer\AssetPlugin\Repository\VcsPackageFilter;
use Fxp\Composer\AssetPlugin\Repository\Util;
/**
* Composer plugin.
*
* @author François Pluchino <[email protected]>
*/
class FxpAssetPlugin implements PluginInterface, EventSubscriberInterface
{
/**
* @var Composer
*/
protected $composer;
/**
* @var RepositoryInterface[]
*/
protected $repos = array();
/**
* @var Pool
*/
protected $pool;
/**
* @var VcsPackageFilter
*/
protected $packageFilter;
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
AssetEvents::ADD_VCS_REPOSITORIES => array(
array('onAddVcsRepositories', 0),
),
InstallerEvents::PRE_DEPENDENCIES_SOLVING => array(
array('onPreDependenciesSolving', 0),
),
);
}
/**
* {@inheritdoc}
*/
public function activate(Composer $composer, IOInterface $io)
{
$this->composer = $composer;
$this->packageFilter = new VcsPackageFilter($composer->getPackage());
$extra = $composer->getPackage()->getExtra();
$rm = $composer->getRepositoryManager();
$this->addRegistryRepositories($rm, $extra);
$this->setVcsTypeRepositories($rm);
if (isset($extra['asset-repositories']) && is_array($extra['asset-repositories'])) {
$this->addRepositories($rm, $extra['asset-repositories']);
}
$this->addInstallers($composer, $io);
}
/**
* Adds vcs repositories in manager from asset dependencies with url version.
*
* @param VcsRepositoryEvent $event
*/
public function onAddVcsRepositories(VcsRepositoryEvent $event)
{
if (null !== $this->composer) {
$rm = $this->composer->getRepositoryManager();
$this->addRepositories($rm, $event->getRepositories(), $this->pool);
}
}
/**
* Add pool in plugin.
*
* @param InstallerEvent $event
*/
public function onPreDependenciesSolving(InstallerEvent $event)
{
$this->pool = $event->getPool();
}
/**
* Adds asset registry repositories.
*
* @param RepositoryManager $rm
* @param array $extra
*/
protected function addRegistryRepositories(RepositoryManager $rm, array $extra)
{
$opts = array_key_exists('asset-registry-options', $extra)
? $extra['asset-registry-options']
: array();
foreach (Assets::getRegistries() as $assetType => $registryClass) {
$config = array(
'repository-manager' => $rm,
'vcs-package-filter' => $this->packageFilter,
'asset-options' => $this->crateAssetOptions($opts, $assetType),
);
$rm->setRepositoryClass($assetType, $registryClass);
$rm->addRepository($rm->createRepository($assetType, $config));
}
}
/**
* Sets vcs type repositories.
*
* @param RepositoryManager $rm
*/
protected function setVcsTypeRepositories(RepositoryManager $rm)
{
foreach (Assets::getTypes() as $assetType) {
foreach (Assets::getVcsRepositoryDrivers() as $driverType => $repositoryClass) {
$rm->setRepositoryClass($assetType . '-' . $driverType, $repositoryClass);
}
}
}
/**
* Adds asset vcs repositories.
*
* @param RepositoryManager $rm
* @param array $repositories
* @param Pool|null $pool
*
* @throws \UnexpectedValueException When config of repository is not an array
* @throws \UnexpectedValueException When the config of repository has not a type defined
* @throws \UnexpectedValueException When the config of repository has an invalid type
*/
protected function addRepositories(RepositoryManager $rm, array $repositories, Pool $pool = null)
{
foreach ($repositories as $index => $repo) {
$this->validateRepositories($index, $repo);
if ('package' === $repo['type']) {
$name = $repo['package']['name'];
} else {
$name = is_int($index) ? preg_replace('{^https?://}i', '', $repo['url']) : $index;
$name = isset($repo['name']) ? $repo['name'] : $name;
$repo['vcs-package-filter'] = $this->packageFilter;
}
Util::addRepository($rm, $this->repos, $name, $repo, $pool);
}
}
/**
* Validates the config of repositories.
*
* @param int|string $index The index
* @param mixed|array $repo The config repo
*
* @throws \UnexpectedValueException
*/
protected function validateRepositories($index, $repo)
{
if (!is_array($repo)) {
throw new \UnexpectedValueException('Repository '.$index.' ('.json_encode($repo).') should be an array, '.gettype($repo).' given');
}
if (!isset($repo['type'])) {
throw new \UnexpectedValueException('Repository '.$index.' ('.json_encode($repo).') must have a type defined');
}
$this->validatePackageRepositories($index, $repo);
$this->validateVcsRepositories($index, $repo);
}
/**
* Validates the config of package repositories.
*
* @param int|string $index The index
* @param mixed|array $repo The config repo
*
* @throws \UnexpectedValueException
*/
protected function validatePackageRepositories($index, $repo)
{
if ('package' !== $repo['type']) {
return;
}
if (!isset($repo['package'])) {
throw new \UnexpectedValueException('Repository '.$index.' ('.json_encode($repo).') must have a package definition"');
}
foreach (array('name', 'type', 'version', 'dist') as $key) {
if (!isset($repo['package'][$key])) {
throw new \UnexpectedValueException('Repository '.$index.' ('.json_encode($repo).') must have the "'.$key.'" key in the package definition"');
}
}
}
/**
* Validates the config of vcs repositories.
*
* @param int|string $index The index
* @param mixed|array $repo The config repo
*
* @throws \UnexpectedValueException
*/
protected function validateVcsRepositories($index, $repo)
{
if ('package' === $repo['type']) {
return;
}
if (false === strpos($repo['type'], '-')) {
throw new \UnexpectedValueException('Repository '.$index.' ('.json_encode($repo).') must have a type defined in this way: "%asset-type%-%type%"');
}
if (!isset($repo['url'])) {
throw new \UnexpectedValueException('Repository '.$index.' ('.json_encode($repo).') must have a url defined');
}
}
/**
* Creates the asset options.
*
* @param array $extra The composer extra section of asset options
* @param string $assetType The asset type
*
* @return array The asset registry options
*/
protected function crateAssetOptions(array $extra, $assetType)
{
$options = array();
foreach ($extra as $key => $value) {
if (0 === strpos($key, $assetType . '-')) {
$key = substr($key, strlen($assetType) + 1);
$options[$key] = $value;
}
}
return $options;
}
/**
* Adds asset installers.
*
* @param Composer $composer
* @param IOInterface $io
*/
protected function addInstallers(Composer $composer, IOInterface $io)
{
$im = $composer->getInstallationManager();
$im->addInstaller(new BowerInstaller($io, $composer, Assets::createType('bower')));
$im->addInstaller(new AssetInstaller($io, $composer, Assets::createType('npm')));
}
}