-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathInstaller.php
More file actions
executable file
·443 lines (370 loc) · 10.8 KB
/
Copy pathInstaller.php
File metadata and controls
executable file
·443 lines (370 loc) · 10.8 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
namespace DrupalComposer\DrupalParanoia;
use Composer\Composer;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UninstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\Installer\PackageEvent;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Util\Filesystem as ComposerFilesystem;
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
use Symfony\Component\Finder\Finder;
/**
* Class Installer.
*
* @package DrupalComposer\DrupalParanoia
*/
class Installer {
/**
* Composer object.
*
* @var \Composer\Composer
*/
protected $composer;
/**
* IO object.
*
* @var \Composer\IO\IOInterface
*/
protected $io;
/**
* Asset file types.
*
* @var array
*/
public $assetFileTypes;
/**
* Front controllers.
*
* @var array
*/
public $frontControllers = [
'index.php',
'core/install.php',
'core/rebuild.php',
'core/modules/statistics/statistics.php',
];
/**
* Flag indicating whether is to run the paranoia installation or not.
*
* @var bool
*/
public $isToRunInstallation = FALSE;
/**
* The app directory path relative to the root package.
*
* @var string
*/
public $appDir;
/**
* The web directory path relative to the root package.
*
* @var string
*/
public $webDir;
/**
* List of paths that should not be symlinked or stubbed.
*
* @var array
*/
public $excludes;
/**
* Installer constructor.
*
* @param \Composer\Composer $composer
* The Composer object.
* @param \Composer\IO\IOInterface $io
* The IO object.
*/
public function __construct(Composer $composer, IOInterface $io) {
$this->composer = $composer;
$this->io = $io;
$appDir = $this->getConfig('app-dir');
if (!$appDir) {
throw new \RuntimeException('Please configure app-dir in your composer.json');
}
$webDir = $this->getConfig('web-dir');
if (!$webDir) {
throw new \RuntimeException('Please configure web-dir in your composer.json');
}
$this->appDir = $appDir;
$this->webDir = $webDir;
$this->setAssetFileTypes();
$this->excludes = $this->getConfig('excludes', []);
}
/**
* Returns the value of a plugin config.
*
* @param string $name
* The config name.
* @param mixed $default
* The default value to return if the config does not exist.
*
* @return mixed
* The config value.
*/
public function getConfig($name, $default = NULL) {
$extra = $this->composer->getPackage()->getExtra();
// TODO: Backward compatibility for old configs. Remove on stable version.
$legacyConfigs = [
'drupal-app-dir',
'drupal-web-dir',
'drupal-asset-files',
];
if (in_array("drupal-$name", $legacyConfigs) && isset($extra["drupal-$name"])) {
return $extra["drupal-$name"];
}
if (!isset($extra['drupal-paranoia'])) {
return $default;
}
if (isset($extra['drupal-paranoia'][$name])) {
return $extra['drupal-paranoia'][$name];
}
return $default;
}
/**
* Checks whether a path exists in the excludes list.
*
* @param string $path
* The path.
*
* @return bool
* Returns TRUE if the path is listed, FALSE otherwise.
*/
public function isExcludedPath($path) {
if (!$this->excludes) {
return FALSE;
}
return in_array($path, $this->excludes);
}
/**
* Checks whether the package from the event is Drupal type.
*
* @param \Composer\Installer\PackageEvent $event
* The composer event object.
*
* @return bool
* Returns TRUE in case of success, FALSE otherwise.
*/
public function isDrupalPackage(PackageEvent $event) {
$operation = $event->getOperation();
$package = NULL;
if ($operation instanceof InstallOperation || $operation instanceof UninstallOperation) {
$package = $operation->getPackage();
}
elseif ($operation instanceof UpdateOperation) {
$package = $operation->getTargetPackage();
}
if (!$package) {
return FALSE;
}
if (!$package instanceof PackageInterface) {
return FALSE;
}
$packageType = $package->getType();
if (!$packageType) {
return FALSE;
}
if ($package->getName() == 'drupal-composer/drupal-paranoia') {
return TRUE;
}
/*
* Identify whether is a Drupal related package.
*
* Package types extracted from:
* - https://www.drupal.org/docs/develop/using-composer/using-composer-to-manage-drupal-site-dependencies#managing-contributed
* - https://www.drupal.org/docs/8/creating-custom-modules/add-a-composerjson-file
*
* @TODO: Add support for custom package types: 'oomphinc/composer-installers-extender' and 'davidbarratt/custom-installer'.
*/
if ((bool) preg_match("/(drupal)?-(core|module|theme|library|profile|drush|custom-module|custom-theme)/", $packageType)) {
return TRUE;
}
return FALSE;
}
/**
* Marks installation to be executed after an install or update command.
*
* @param \Composer\Installer\PackageEvent $event
* The PackageEvent object.
*/
public function onPostPackageEvent(PackageEvent $event) {
if ($this->isToRunInstallation) {
/*
* We already know that a Drupal package is being installed/updated,
* we don't need to check the package again, the installation will be
* executed on onPostCmdEvent().
*/
return;
}
// Checks if the package that is being installed is Drupal type.
if ($this->isDrupalPackage($event)) {
/*
* When Drupal packages are installed or updated we need to flag that
* the paranoia installation needs to be executed in order to install
* or remove the new asset files.
*/
$this->isToRunInstallation = TRUE;
}
}
/**
* Post install command event to execute the installation.
*/
public function onPostCmdEvent() {
if ($this->isToRunInstallation) {
$this->install();
}
}
/**
* Build and install the project assets.
*/
public function install() {
$fs = new SymfonyFilesystem();
// Remove the web directory, it will be recreated with new asset files.
if ($fs->exists($this->webDir)) {
$fs->remove($this->webDir);
}
// Ensure that the app and web directories exist.
$fs->mkdir([$this->appDir, $this->webDir]);
// Create the stub files.
foreach ($this->frontControllers as $fileName) {
$this->createStubPhpFile($fileName);
}
// Symlink public files.
$this->createPublicFilesSymlink();
// Create symlinks.
$this->createAssetSymlinks();
$this->io->write("> drupal-paranoia: " . $this->webDir . " folder has been rebuilt.");
}
/**
* Returns a list containing the sites public files path.
*
* @return array
* The path list.
*/
public function getSitesPublicFilesPath() {
$finder = new Finder();
$finder->in($this->appDir . '/sites')
->depth(0);
$directories = [];
/** @var \Symfony\Component\Finder\SplFileInfo $directory */
foreach ($finder->directories() as $directory) {
$publicFilesPath = 'sites/' . $directory->getFilename() . '/files';
if (!is_dir($this->appDir . '/' . $publicFilesPath)) {
continue;
}
$directories[] = $publicFilesPath;
}
return $directories;
}
/**
* Symlink the public files folder.
*/
public function createPublicFilesSymlink() {
$cfs = new ComposerFilesystem();
$publicFilesPaths = $this->getSitesPublicFilesPath();
foreach ($publicFilesPaths as $path) {
$cfs->ensureDirectoryExists($this->webDir . '/' . dirname($path));
$cfs->relativeSymlink(realpath($this->appDir) . '/' . $path, realpath($this->webDir) . '/' . $path);
}
}
/**
* Symlink the assets from the app to web directory.
*/
public function createAssetSymlinks() {
$finder = new Finder();
$finder->ignoreDotFiles(FALSE)->in($this->appDir);
// Add asset files types to the search.
foreach ($this->assetFileTypes as $name) {
$finder->name($name);
}
// Default extensions to exclude from the search.
$finder->notName('*.inc');
$finder->notName('*.install');
$finder->notName('*.module');
$finder->notName('*.phar');
$finder->notName('*.php');
$finder->notName('*.profile');
$finder->notName('*.theme');
// Ignores excluded paths.
foreach ($this->excludes as $excludedPath) {
// If is a file.
if (isset(pathinfo($excludedPath)['extension'])) {
$finder->notName($excludedPath);
continue;
}
// Is a folder.
$finder->exclude($excludedPath);
}
// Ignores sites public files path.
foreach ($this->getSitesPublicFilesPath() as $publicFilesPath) {
$finder->exclude($publicFilesPath);
}
$cfs = new ComposerFilesystem();
foreach ($finder->files() as $file) {
$cfs->ensureDirectoryExists($this->webDir . '/' . $file->getRelativePath());
$cfs->relativeSymlink($file->getRealPath(), realpath($this->webDir) . '/' . $file->getRelativePathname());
}
}
/**
* Create a PHP stub file at web directory.
*
* @param string $path
* The PHP file from the app directory.
*/
public function createStubPhpFile($path) {
if ($this->isExcludedPath($path)) {
return;
}
$appDir = realpath($this->appDir);
$webDir = realpath($this->webDir);
$endPath = (dirname($path) == '.') ? $appDir : $appDir . '/' . dirname($path);
$startPath = (dirname($path) == '.') ? $webDir : $webDir . '/' . dirname($path);
$fs = new SymfonyFilesystem();
$relativePath = $fs->makePathRelative($endPath, $startPath);
$filename = basename($path);
$content = <<<EOF
<?php
chdir('$relativePath');
require './$filename';
use Drupal\Core\DrupalKernel;
return static function () {
return new DrupalKernel('prod', require 'autoload.php');
};
EOF;
$fs->dumpFile($webDir . '/' . $path, $content);
}
/**
* Set the asset file types.
*/
public function setAssetFileTypes() {
$this->assetFileTypes = [
'robots.txt',
'.htaccess',
'*.css',
'*.eot',
'*.ico',
'*.gif',
'*.jpeg',
'*.jpg',
'*.js',
'*.map',
'*.otf',
'*.png',
'*.svg',
'*.ttf',
'*.woff',
'*.woff2',
];
// Allow people to extend the list from a composer extra key.
$extraAssetFiles = $this->getConfig('asset-files');
if ($extraAssetFiles) {
$this->assetFileTypes = array_merge($this->assetFileTypes, $extraAssetFiles);
}
// Allow other plugins to alter the list of files.
$event = new AssetFileTypesEvent($this->assetFileTypes, $this->composer, $this->io);
$this->composer->getEventDispatcher()->dispatch($event->getName(), $event);
$this->assetFileTypes = $event->getAssetFileTypes();
}
}