-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathSolver.php
167 lines (143 loc) · 5.12 KB
/
Solver.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
<?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\Solver;
use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Json\JsonFile;
use Composer\Package\PackageInterface;
use Composer\Util\Filesystem;
use Foxy\Asset\AssetManagerInterface;
use Foxy\Config\Config;
use Foxy\Event\GetAssetsEvent;
use Foxy\Event\PostSolveEvent;
use Foxy\Event\PreSolveEvent;
use Foxy\Fallback\FallbackInterface;
use Foxy\FoxyEvents;
use Foxy\Util\AssetUtil;
/**
* Solver of asset dependencies.
*
* @author François Pluchino <[email protected]>
*/
class Solver implements SolverInterface
{
/**
* @var Config
*/
protected $config;
/**
* @var Filesystem
*/
protected $fs;
/**
* @var AssetManagerInterface
*/
protected $assetManager;
/**
* @var null|FallbackInterface
*/
protected $composerFallback;
/**
* Constructor.
*
* @param AssetManagerInterface $assetManager The asset manager
* @param Config $config The config
* @param Filesystem $filesystem The composer filesystem
* @param null|FallbackInterface $composerFallback The composer fallback
*/
public function __construct(
AssetManagerInterface $assetManager,
Config $config,
Filesystem $filesystem,
FallbackInterface $composerFallback = null
) {
$this->config = $config;
$this->fs = $filesystem;
$this->assetManager = $assetManager;
$this->composerFallback = $composerFallback;
}
/**
* {@inheritdoc}
*/
public function setUpdatable($updatable)
{
$this->assetManager->setUpdatable($updatable);
return $this;
}
/**
* {@inheritdoc}
*/
public function solve(Composer $composer, IOInterface $io)
{
if (!$this->config->get('enabled')) {
return;
}
$dispatcher = $composer->getEventDispatcher();
$packages = $composer->getRepositoryManager()->getLocalRepository()->getCanonicalPackages();
$vendorDir = $composer->getConfig()->get('vendor-dir');
$assetDir = $this->config->get('composer-asset-dir', $vendorDir.'/foxy/composer-asset/');
$dispatcher->dispatch(FoxyEvents::PRE_SOLVE, new PreSolveEvent($assetDir, $packages));
$this->fs->remove($assetDir);
$assets = $this->getAssets($composer, $assetDir, $packages);
$this->assetManager->addDependencies($composer->getPackage(), $assets);
$res = $this->assetManager->run();
$dispatcher->dispatch(FoxyEvents::POST_SOLVE, new PostSolveEvent($assetDir, $packages, $res));
if ($res > 0 && $this->composerFallback) {
$this->composerFallback->restore();
throw new \RuntimeException('The asset manager ended with an error');
}
}
/**
* Get the package of asset dependencies.
*
* @param Composer $composer The composer
* @param string $assetDir The asset directory
* @param PackageInterface[] $packages The package dependencies
*
* @return array[]
*/
protected function getAssets(Composer $composer, $assetDir, array $packages)
{
$installationManager = $composer->getInstallationManager();
$configPackages = $this->config->getArray('enable-packages');
$assets = array();
foreach ($packages as $package) {
$filename = AssetUtil::getPath($installationManager, $this->assetManager, $package, $configPackages);
if (null !== $filename) {
list($packageName, $packagePath) = $this->getMockPackagePath($package, $assetDir, $filename);
$assets[$packageName] = $packagePath;
}
}
$assetsEvent = new GetAssetsEvent($assetDir, $packages, $assets);
$composer->getEventDispatcher()->dispatch(FoxyEvents::GET_ASSETS, $assetsEvent);
return $assetsEvent->getAssets();
}
/**
* Get the path of the mock package.
*
* @param PackageInterface $package The package dependency
* @param string $assetDir The asset directory
* @param string $filename The filename of asset package
*
* @return string[] The package name and the relative package path from the current directory
*/
protected function getMockPackagePath(PackageInterface $package, $assetDir, $filename)
{
$packageName = AssetUtil::getName($package);
$packagePath = rtrim($assetDir, '/').'/'.$package->getName();
$newFilename = $packagePath.'/'.basename($filename);
mkdir($packagePath, 0777, true);
copy($filename, $newFilename);
$jsonFile = new JsonFile($newFilename);
$packageValue = AssetUtil::formatPackage($package, $packageName, (array) $jsonFile->read());
$jsonFile->write($packageValue);
return array($packageName, $this->fs->findShortestPath(getcwd(), $newFilename));
}
}