-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathInstaller.php
209 lines (185 loc) · 6.94 KB
/
Installer.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
<?php
/*
* This file is part of Component Installer.
*
* (c) Rob Loach (http://robloach.net)
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace ComponentInstaller;
use Composer\Installer\LibraryInstaller;
use Composer\Script\Event;
use Composer\Package\PackageInterface;
use Composer\Package\AliasPackage;
/**
* Component Installer for Composer.
*/
class Installer extends LibraryInstaller
{
private static $defaultProcesses = array(
// Copy the assets to the Components directory.
"ComponentInstaller\\Process\\CopyProcess",
// Build the require.js file.
"ComponentInstaller\\Process\\RequireJsProcess",
// Build the require.css file.
"ComponentInstaller\\Process\\RequireCssProcess",
// Compile the require-built.js file.
"ComponentInstaller\\Process\\BuildJsProcess",
);
/**
* The location where Components are to be installed.
*/
protected $componentDir;
/**
* {@inheritDoc}
*
* Components are supported by all packages. This checks wheteher or not the
* entire package is a "component", as well as injects the script to act
* on components embedded in packages that are not just "component" types.
*/
public function supports($packageType)
{
// Components are supported by all package types. We will just act on
// the root package's scripts if available.
$rootPackage = isset($this->composer) ? $this->composer->getPackage() : null;
if (isset($rootPackage)) {
// Ensure we get the root package rather than its alias.
while ($rootPackage instanceof AliasPackage) {
$rootPackage = $rootPackage->getAliasOf();
}
// Make sure the root package can override the available scripts.
if (method_exists($rootPackage, 'setScripts')) {
$scripts = $rootPackage->getScripts();
// Act on the "post-autoload-dump" command so that we can act on all
// the installed packages.
$scripts['post-autoload-dump']['component-installer'] = 'ComponentInstaller\\Installer::postAutoloadDump';
$rootPackage->setScripts($scripts);
}
}
// State support for "component" package types.
return $packageType == 'component';
}
/**
* Gets the destination Component directory.
*
* @param PackageInterface $package
* @return string
* The path to where the final Component should be installed.
*/
public function getComponentPath(PackageInterface $package)
{
// Parse the pretty name for the vendor and package name.
$name = $prettyName = $package->getPrettyName();
if (strpos($prettyName, '/') !== false) {
list($vendor, $name) = explode('/', $prettyName);
unset($vendor);
}
// First look for an override in root package's extra, then try the package's extra
$rootPackage = $this->composer->getPackage();
$rootExtras = $rootPackage ? $rootPackage->getExtra() : array();
$customComponents = isset($rootExtras['component']) ? $rootExtras['component'] : array();
if (isset($customComponents[$prettyName]) && is_array($customComponents[$prettyName])) {
$component = $customComponents[$prettyName];
}
else {
$extra = $package->getExtra();
$component = isset($extra['component']) ? $extra['component'] : array();
}
// Allow the component to define its own name.
if (isset($component['name'])) {
$name = $component['name'];
}
// Find where the package should be located.
return $this->getComponentDir() . DIRECTORY_SEPARATOR . $name;
}
/**
* Initialize the Component directory, as well as the vendor directory.
*/
protected function initializeVendorDir()
{
$this->componentDir = $this->getComponentDir();
$this->filesystem->ensureDirectoryExists($this->componentDir);
parent::initializeVendorDir();
}
/**
* Retrieves the Installer's provided component directory.
*/
public function getComponentDir()
{
$config = $this->composer->getConfig();
return $config->has('component-dir') ? $config->get('component-dir') : 'components';
}
/**
* Remove both the installed code and files from the Component directory.
*
* @param PackageInterface $package
*/
public function removeCode(PackageInterface $package)
{
$this->removeComponent($package);
parent::removeCode($package);
}
/**
* Remove a Component's files from the Component directory.
*
* @param PackageInterface $package
* @return bool
*/
public function removeComponent(PackageInterface $package)
{
$path = $this->getComponentPath($package);
return $this->filesystem->remove($path);
}
/**
* Before installing the Component, be sure its destination is clear first.
*
* @param PackageInterface $package
*/
public function installCode(PackageInterface $package)
{
$this->removeComponent($package);
parent::installCode($package);
}
/**
* Script callback; Acted on after the autoloader is dumped.
*
* @param Event $event
*/
public static function postAutoloadDump(Event $event)
{
// Retrieve basic information about the environment and present a
// message to the user.
$composer = $event->getComposer();
$config = $composer->getConfig();
$io = $event->getIO();
$io->write('<info>Compiling component files</info>');
// Set up all the processes.
$processes = $config->has('component-processes') ?
$config->get('component-processes') :
static::$defaultProcesses;
// Initialize and execute each process in sequence.
foreach ($processes as $process) {
$options = array();
if (is_array($process)) {
$options = isset($process['options']) ? $process['options'] : array();
$class = $process['class'];
}
else {
$class = $process;
}
if(!class_exists($class)){
$io->write("<warning>Process class '$class' not found, skipping this process</warning>");
continue;
}
/** @var \ComponentInstaller\Process\Process $process */
$process = new $class($composer, $io, $options);
// When an error occurs during initialization, end the process.
if (!$process->init()) {
$io->write("<warning>An error occurred while initializing the '$class' process.</warning>");
break;
}
$process->process();
}
}
}