-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathFoxy.php
232 lines (204 loc) · 6.11 KB
/
Foxy.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
<?php
/*
* This file is part of the Foxy 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 Foxy;
use Composer\Composer;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Composer\Util\Filesystem;
use Composer\Util\ProcessExecutor;
use Foxy\Asset\AssetManagerFinder;
use Foxy\Asset\AssetManagerInterface;
use Foxy\Config\Config;
use Foxy\Config\ConfigBuilder;
use Foxy\Exception\RuntimeException;
use Foxy\Fallback\AssetFallback;
use Foxy\Fallback\ComposerFallback;
use Foxy\Solver\Solver;
use Foxy\Solver\SolverInterface;
use Foxy\Util\ComposerUtil;
use Foxy\Util\ConsoleUtil;
/**
* Composer plugin.
*
* @author François Pluchino <[email protected]>
*/
class Foxy implements PluginInterface, EventSubscriberInterface
{
const REQUIRED_COMPOSER_VERSION = '^1.5.0|^2.0.0';
/**
* @var Config
*/
protected $config;
/**
* @var AssetManagerInterface
*/
protected $assetManager;
/**
* @var AssetFallback
*/
protected $assetFallback;
/**
* @var ComposerFallback
*/
protected $composerFallback;
/**
* @var SolverInterface
*/
protected $solver;
/**
* @var bool
*/
protected $initialized = false;
/**
* The list of the classes of asset managers.
*/
private static $assetManagers = array(
'Foxy\Asset\NpmManager',
'Foxy\Asset\PnpmManager',
'Foxy\Asset\YarnManager',
);
/**
* The default values of config.
*/
private static $defaultConfig = array(
'enabled' => true,
'manager' => null,
'manager-version' => array(
'npm' => '>=5.0.0',
'pnpm' => '>=7.0.0',
'yarn' => '>=1.0.0',
),
'manager-bin' => null,
'manager-options' => null,
'manager-install-options' => null,
'manager-update-options' => null,
'manager-timeout' => null,
'composer-asset-dir' => null,
'run-asset-manager' => true,
'fallback-asset' => true,
'fallback-composer' => true,
'enable-packages' => array(),
);
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
ComposerUtil::getInitEventName() => array(
array('init', 100),
),
PackageEvents::POST_PACKAGE_INSTALL => array(
array('initOnInstall', 100),
),
ScriptEvents::POST_INSTALL_CMD => array(
array('solveAssets', 100),
),
ScriptEvents::POST_UPDATE_CMD => array(
array('solveAssets', 100),
),
);
}
/**
* {@inheritdoc}
*/
public function activate(Composer $composer, IOInterface $io)
{
ComposerUtil::validateVersion(static::REQUIRED_COMPOSER_VERSION, Composer::VERSION);
$input = ConsoleUtil::getInput($io);
$executor = new ProcessExecutor($io);
$fs = new Filesystem($executor);
$this->config = ConfigBuilder::build($composer, self::$defaultConfig, $io);
$this->assetManager = $this->getAssetManager($io, $this->config, $executor, $fs);
$this->assetFallback = new AssetFallback($io, $this->config, $this->assetManager->getPackageName(), $fs);
$this->composerFallback = new ComposerFallback($composer, $io, $this->config, $input, $fs);
$this->solver = new Solver($this->assetManager, $this->config, $fs, $this->composerFallback);
$this->assetManager->setFallback($this->assetFallback);
}
public function deactivate(Composer $composer, IOInterface $io)
{
// Do nothing
}
public function uninstall(Composer $composer, IOInterface $io)
{
// Do nothing
}
/**
* Init the plugin just after the first installation.
*
* @param PackageEvent $event The package event
*/
public function initOnInstall(PackageEvent $event)
{
$operation = $event->getOperation();
if ($operation instanceof InstallOperation && 'foxy/foxy' === $operation->getPackage()->getName()) {
$this->init();
}
}
/**
* Init the plugin.
*/
public function init()
{
if (!$this->initialized) {
$this->initialized = true;
$this->assetFallback->save();
$this->composerFallback->save();
if ($this->config->get('enabled')) {
$this->assetManager->validate();
}
}
}
/**
* Set the solver.
*
* @param SolverInterface $solver The solver
*/
public function setSolver(SolverInterface $solver)
{
$this->solver = $solver;
}
/**
* Solve the assets.
*
* @param Event $event The composer script event
*/
public function solveAssets(Event $event)
{
$this->solver->setUpdatable(false !== strpos($event->getName(), 'update'));
$this->solver->solve($event->getComposer(), $event->getIO());
}
/**
* Get the asset manager.
*
* @param IOInterface $io The IO
* @param Config $config The config
* @param ProcessExecutor $executor The process executor
* @param Filesystem $fs The composer filesystem
*
* @return AssetManagerInterface
*
* @throws RuntimeException When the asset manager is not found
*/
protected function getAssetManager(IOInterface $io, Config $config, ProcessExecutor $executor, Filesystem $fs)
{
$amf = new AssetManagerFinder();
foreach (self::$assetManagers as $class) {
$amf->addManager(new $class($io, $config, $executor, $fs));
}
return $amf->findManager($config->get('manager'));
}
}