forked from fxpio/composer-asset-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFxpAssetPlugin.php
234 lines (208 loc) · 7.22 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
<?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\CommandEvent;
use Composer\Plugin\PluginEvents;
use Composer\Plugin\PluginInterface;
use Composer\Repository\InstalledFilesystemRepository;
use Composer\Repository\RepositoryInterface;
use Composer\Repository\RepositoryManager;
use Fxp\Composer\AssetPlugin\Event\VcsRepositoryEvent;
use Fxp\Composer\AssetPlugin\Repository\VcsPackageFilter;
use Fxp\Composer\AssetPlugin\Repository\Util;
use Fxp\Composer\AssetPlugin\Util\AssetPlugin;
/**
* Composer plugin.
*
* @author François Pluchino <[email protected]>
*/
class FxpAssetPlugin implements PluginInterface, EventSubscriberInterface
{
/**
* @var Composer
*/
protected $composer;
/**
* @var IOInterface
*/
protected $io;
/**
* @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),
),
PluginEvents::COMMAND => array(
array('onPluginCommand', 0),
),
InstallerEvents::PRE_DEPENDENCIES_SOLVING => array(
array('onPreDependenciesSolving', 0),
),
);
}
/**
* {@inheritDoc}
*/
public function activate(Composer $composer, IOInterface $io)
{
/* @var InstalledFilesystemRepository $installedRepository */
$installedRepository = $composer->getRepositoryManager()->getLocalRepository();
$this->composer = $composer;
$this->io = $io;
$this->packageFilter = new VcsPackageFilter($composer->getPackage(), $composer->getInstallationManager(), $installedRepository);
$extra = $composer->getPackage()->getExtra();
$rm = $composer->getRepositoryManager();
AssetPlugin::addRegistryRepositories($rm, $this->packageFilter, $extra);
AssetPlugin::setVcsTypeRepositories($rm);
if (isset($extra['asset-repositories']) && is_array($extra['asset-repositories'])) {
$this->addRepositories($rm, $extra['asset-repositories']);
}
AssetPlugin::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);
}
}
/**
* Disable the package filter for all command, but for install and update command.
*
* @param CommandEvent $event
*/
public function onPluginCommand(CommandEvent $event)
{
if (!in_array($event->getCommandName(), array('install', 'update'))) {
$this->packageFilter->setEnabled(false);
}
}
/**
* Add pool in plugin.
*
* @param InstallerEvent $event
*/
public function onPreDependenciesSolving(InstallerEvent $event)
{
$this->pool = $event->getPool();
}
/**
* 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($this->io, $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');
}
}
}