-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
FxpAssetPlugin.php
129 lines (111 loc) · 3.9 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
<?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\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 Fxp\Composer\AssetPlugin\Config\Config;
use Fxp\Composer\AssetPlugin\Config\ConfigBuilder;
use Fxp\Composer\AssetPlugin\Repository\AssetRepositoryManager;
use Fxp\Composer\AssetPlugin\Repository\ResolutionManager;
use Fxp\Composer\AssetPlugin\Repository\VcsPackageFilter;
use Fxp\Composer\AssetPlugin\Util\AssetPlugin;
/**
* Composer plugin.
*
* @author François Pluchino <[email protected]>
*/
class FxpAssetPlugin implements PluginInterface, EventSubscriberInterface
{
/**
* @var Config
*/
protected $config;
/**
* @var Composer
*/
protected $composer;
/**
* @var IOInterface
*/
protected $io;
/**
* @var VcsPackageFilter
*/
protected $packageFilter;
/**
* @var AssetRepositoryManager
*/
protected $assetRepositoryManager;
public static function getSubscribedEvents()
{
return array(
PluginEvents::COMMAND => array(
array('onPluginCommand', 0),
),
InstallerEvents::PRE_DEPENDENCIES_SOLVING => array(
array('onPreDependenciesSolving', 0),
),
);
}
public function activate(Composer $composer, IOInterface $io)
{
$this->config = ConfigBuilder::build($composer, $io);
if ($this->config->get('enabled', true)) {
/** @var InstalledFilesystemRepository $installedRepository */
$installedRepository = $composer->getRepositoryManager()->getLocalRepository();
$this->composer = $composer;
$this->io = $io;
$this->packageFilter = new VcsPackageFilter($this->config, $composer->getPackage(), $composer->getInstallationManager(), $installedRepository);
$this->assetRepositoryManager = new AssetRepositoryManager($io, $composer->getRepositoryManager(), $this->config, $this->packageFilter);
$this->assetRepositoryManager->setResolutionManager(new ResolutionManager($this->config->getArray('resolutions')));
AssetPlugin::addRegistryRepositories($this->assetRepositoryManager, $this->packageFilter, $this->config);
AssetPlugin::setVcsTypeRepositories($composer->getRepositoryManager());
$this->assetRepositoryManager->addRepositories($this->config->getArray('repositories'));
AssetPlugin::addInstallers($this->config, $composer, $io);
}
}
/**
* Disable the package filter for all command, but for install and update command.
*/
public function onPluginCommand(CommandEvent $event)
{
if ($this->config->get('enabled', true)) {
ConfigBuilder::validate($this->io, $this->composer->getPackage(), $event->getCommandName());
if (!\in_array($event->getCommandName(), array('install', 'update'), true)) {
$this->packageFilter->setEnabled(false);
}
}
}
/**
* Add pool in asset repository manager.
*/
public function onPreDependenciesSolving(InstallerEvent $event)
{
if ($this->config->get('enabled', true)) {
$this->assetRepositoryManager->setPool($event->getPool());
}
}
/**
* Get the plugin config.
*
* @return Config
*/
public function getConfig()
{
return $this->config;
}
}