/
AbstractModuleInstaller.php
602 lines (517 loc) · 18.1 KB
/
AbstractModuleInstaller.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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
<?php
/**
* This file is part of contao-community-alliance/composer-plugin.
*
* (c) 2013 Contao Community Alliance
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package contao-community-alliance/composer-plugin
* @author Andreas Schempp <andreas.schempp@terminal42.ch>
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @author Kamil Kuzminski <kamil.kuzminski@codefog.pl>
* @copyright 2013-2016 Contao Community Alliance
* @license https://github.com/contao-community-alliance/composer-plugin/blob/master/LICENSE LGPL-3.0+
* @link http://c-c-a.org
* @filesource
*/
namespace ContaoCommunityAlliance\Composer\Plugin\Installer;
use Composer\Composer;
use Composer\Installer\LibraryInstaller;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Repository\InstalledRepositoryInterface;
use Composer\Util\Filesystem;
use ContaoCommunityAlliance\Composer\Plugin\RunonceManager;
use ContaoCommunityAlliance\Composer\Plugin\UserFilesLocator;
/**
* AbstractModuleInstaller is the parent class that handles file copying and symlinking.
*/
abstract class AbstractModuleInstaller extends LibraryInstaller
{
const DUPLICATE_IGNORE = 1;
const DUPLICATE_OVERWRITE = 2;
const DUPLICATE_FAIL = 3;
const INVALID_IGNORE = 1;
const INVALID_OVERWRITE = 2;
const INVALID_FAIL = 3;
/**
* The run once manager in use.
*
* @var RunonceManager
*/
protected $runonceManager;
/**
* Constructor.
*
* @param RunonceManager $runonceManager The run once manager to use.
*
* @param IOInterface $inputOutput The input/output abstraction to use.
*
* @param Composer $composer The composer instance.
*
* @param string $type The typename this installer is responsible for.
*
* @param Filesystem $filesystem The file system instance.
*/
public function __construct(
RunonceManager $runonceManager,
IOInterface $inputOutput,
Composer $composer,
$type,
Filesystem $filesystem = null
) {
parent::__construct($inputOutput, $composer, $type, $filesystem);
$this->runonceManager = $runonceManager;
}
/**
* Add symlinks for Contao sources after installing a package.
*
* {@inheritdoc}
*/
public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
{
$this->logVerbose(sprintf('Installing Contao sources for %s', $package->getName()));
parent::install($repo, $package);
$this->addSymlinks($package, $this->getContaoRoot(), $this->getSources($package));
$this->addCopies($package, $this->getFilesRoot(), $this->getUserFiles($package), self::DUPLICATE_IGNORE);
$this->addRunonces($package, $this->getRunonces($package));
$this->logVerbose('');
}
/**
* Remove symlinks for Contao sources before update, then add them again afterwards.
*
* {@inheritdoc}
*
* @throws \InvalidArgumentException When the requested package is not installed.
*/
public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
{
if (!$repo->hasPackage($initial)) {
throw new \InvalidArgumentException('Package is not installed: '.$initial);
}
$this->logVerbose(sprintf('Updating Contao sources for %s', $initial->getName()));
$contaoRoot = $this->getContaoRoot();
$this->removeSymlinks($initial, $contaoRoot, $this->getSources($initial));
parent::update($repo, $initial, $target);
$this->addSymlinks($target, $contaoRoot, $this->getSources($target));
$this->addCopies($target, $this->getFilesRoot(), $this->getUserFiles($target), self::DUPLICATE_IGNORE);
$this->addRunonces($target, $this->getRunonces($target));
$this->logVerbose('');
}
/**
* Remove symlinks for Contao sources before uninstalling a package.
*
* {@inheritdoc}
*
* @throws \InvalidArgumentException When the requested package is not installed.
*/
public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package)
{
if (!$repo->hasPackage($package)) {
throw new \InvalidArgumentException('Package is not installed: '.$package);
}
$this->logVerbose(sprintf('Removing Contao sources for %s', $package->getName()));
$this->removeSymlinks($package, $this->getContaoRoot(), $this->getSources($package));
parent::uninstall($repo, $package);
$this->logVerbose('');
}
/**
* Gets installation files from the Contao package.
*
* @param PackageInterface $package The package to extract the sources from.
*
* @return array
*/
abstract protected function getSources(PackageInterface $package);
/**
* Gets user files (TL_FILES) from the Contao package.
*
* @param PackageInterface $package The package to extract the user files from.
*
* @return array
*/
abstract protected function getUserFiles(PackageInterface $package);
/**
* Gets runonce files from the Contao package.
*
* @param PackageInterface $package The package to extract the run once files from.
*
* @return array
*/
abstract protected function getRunonces(PackageInterface $package);
/**
* Gets the Contao root (parent folder of vendor dir).
*
* @return string
*/
protected function getContaoRoot()
{
$this->initializeVendorDir();
return dirname($this->vendorDir);
}
/**
* Gets the user files root folder (e.g. TL_ROOT/files).
*
* @return string
*/
protected function getFilesRoot()
{
$locator = new UserFilesLocator($this->getContaoRoot());
return $locator->locate();
}
/**
* Creates symlinks for a map of relative file paths.
* Key is the relative path to composer package, whereas "value" is relative to Contao root.
*
* @param PackageInterface $package The package being processed.
*
* @param string $targetRoot The target directory.
*
* @param array $pathMap The path mapping.
*
* @param int $mode The mode how to handle duplicate files.
*
* @return void
*
* @throws \RuntimeException When the symlink could not be created.
*/
protected function addSymlinks(PackageInterface $package, $targetRoot, array $pathMap, $mode = self::DUPLICATE_FAIL)
{
if (empty($pathMap)) {
return;
}
$packageRoot = $this->getInstallPath($package);
$actions = [];
// Check the file map first and make sure nothing exists.
foreach ($pathMap as $sourcePath => $targetPath) {
$source = $this->filesystem->normalizePath($packageRoot . ($sourcePath ? ('/'.$sourcePath) : ''));
$target = $this->filesystem->normalizePath($targetRoot . '/' . $targetPath);
if ($this->canAddSymlink($source, $target, $mode)) {
$actions[$source] = $target;
}
}
// Only actually create the links if the checks are successful to prevent orphans.
foreach ($actions as $source => $target) {
$this->logSymlink($source, $target);
$this->filesystem->ensureDirectoryExists(dirname($target));
if (!$this->filesystem->relativeSymlink($source, $target)) {
throw new \RuntimeException('Failed to create symlink ' . $target);
}
}
}
/**
* Removes symlinks from a map of relative file paths.
* Key is the relative path to composer package, whereas "value" is relative to Contao root.
*
* @param PackageInterface $package The package being processed.
*
* @param string $targetRoot The target directory.
*
* @param array $pathMap The path mapping.
*
* @param int $mode The mode how to handle duplicate files.
*
* @return void
*/
protected function removeSymlinks(
PackageInterface $package,
$targetRoot,
array $pathMap,
$mode = self::INVALID_FAIL
) {
if (empty($pathMap)) {
return;
}
$packageRoot = $this->getInstallPath($package);
$actions = [];
// Check the file map first and make sure we only remove our own symlinks.
foreach ($pathMap as $sourcePath => $targetPath) {
$source = $this->filesystem->normalizePath($packageRoot . ($sourcePath ? ('/'.$sourcePath) : ''));
$target = $this->filesystem->normalizePath($targetRoot . '/' . $targetPath);
if ($this->canRemoveSymlink($source, $target, $mode)) {
$actions[] = $target;
}
}
// Remove the symlinks if everything is ok.
foreach ($actions as $target) {
$this->logRemove($target);
$this->filesystem->unlink($target);
$this->removeEmptyDirectories(dirname($target), $targetRoot);
}
}
/**
* Creates copies for a map of relative file paths.
* Key is the relative path to composer package, whereas "value" is relative to Contao root.
*
* @param PackageInterface $package The package being processed.
*
* @param string $targetRoot The target directory.
*
* @param array $pathMap The path mapping.
*
* @param int $mode The mode how to handle duplicate files.
*
* @return void
*
* @throws \RuntimeException When a source path does not exist or is not readable.
*/
protected function addCopies(PackageInterface $package, $targetRoot, array $pathMap, $mode = self::DUPLICATE_FAIL)
{
if (empty($pathMap)) {
return;
}
$packageRoot = $this->getInstallPath($package);
$actions = [];
// Check the file map first and make sure nothing exists.
foreach ($pathMap as $sourcePath => $targetPath) {
$source = $this->filesystem->normalizePath($packageRoot . (empty($sourcePath) ? '' : ('/'.$sourcePath)));
$target = $this->filesystem->normalizePath($targetRoot . '/' . $targetPath);
if (!is_readable($source)) {
throw new \RuntimeException(
sprintf('Installation source "%s" does not exist', $sourcePath)
);
}
if (file_exists($target) && !$this->canAddTarget($target, $mode)) {
continue;
}
$actions[$source] = $target;
}
// Only actually create the links if the checks are successful to prevent orphans.
foreach ($actions as $source => $target) {
$this->logCopy($source, $target);
$this->filesystem->ensureDirectoryExists(dirname($target));
copy($source, $target);
}
}
/**
* Removes copies from a map of relative file paths.
* Key is the relative path to composer package, whereas "value" is relative to Contao root.
*
* @param string $targetRoot The target directory.
*
* @param array $pathMap The path mapping.
*
* @return void
*/
protected function removeCopies($targetRoot, array $pathMap)
{
if (empty($pathMap)) {
return;
}
$actions = [];
// Check the file map first and make sure we only remove our own symlinks.
foreach ($pathMap as $targetPath) {
$target = $this->filesystem->normalizePath($targetRoot . '/' . $targetPath);
if (!file_exists($target)) {
continue;
}
$actions[] = $target;
}
// Remove the symlinks if everything is ok.
foreach ($actions as $target) {
$this->logRemove($target);
$this->filesystem->unlink($target);
$this->removeEmptyDirectories(dirname($target), $targetRoot);
}
}
/**
* Adds runonce files of a package to the RunonceManager instance.
*
* @param PackageInterface $package The package being processed.
*
* @param array $files The file names of all runonce files.
*
* @return void
*/
protected function addRunonces(PackageInterface $package, array $files)
{
$rootDir = $this->getInstallPath($package);
foreach ($files as $file) {
$this->runonceManager->addFile($this->filesystem->normalizePath($rootDir . '/' . $file));
}
}
/**
* Clean up empty directories.
*
* @param string $pathname The path to remove if empty.
*
* @param string $root The path of the root installation.
*
* @return bool
*/
private function removeEmptyDirectories($pathname, $root)
{
if (is_dir($pathname)
&& $pathname !== $root
&& $this->filesystem->isDirEmpty($pathname)
) {
$this->filesystem->removeDirectory($pathname);
if (!$this->removeEmptyDirectories(dirname($pathname), $root)) {
if ($this->io->isVeryVerbose()) {
$this->io->writeError(sprintf(' - Removing empty directory "%s"', $pathname));
}
}
return true;
}
return false;
}
/**
* Check if the source exists, is readable and shall get symlink'ed to the target.
*
* @param string $source The source path.
*
* @param string $target The target path.
*
* @param int $mode The duplicate file handling mode.
*
* @return bool
*
* @throws \RuntimeException When the source is not readable.
*/
public function canAddSymlink($source, $target, $mode)
{
if (!is_readable($source)) {
throw new \RuntimeException(
sprintf('Installation source "%s" does not exist or is not readable', $source)
);
}
if (file_exists($target)) {
// Target link already exists and is correct, do nothing
if (is_link($target) && $source === readlink($target)) {
return false;
}
if (!$this->canAddTarget($target, $mode)) {
return false;
}
}
return true;
}
/**
* Check if the target exists, is a symlink and the symlink points to the target and therefore shall get removed.
*
* @param string $source The source path.
*
* @param string $target The target path.
*
* @param int $mode The invalid file handling mode.
*
* @return bool
*
* @throws \RuntimeException When a file entry is not a symlink to the expected target and mode is INVALID_FAIL.
*/
public function canRemoveSymlink($source, $target, $mode)
{
if (!file_exists($target)) {
return false;
}
if (!is_link($target)
|| $this->filesystem->normalizePath($source) !== $this->filesystem->normalizePath(realpath($target))
) {
if (self::INVALID_IGNORE === $mode) {
return false;
}
if (self::INVALID_FAIL === $mode) {
throw new \RuntimeException(
sprintf(
'"%s" is not a link to "%s" (expected "%s" but got "%s")',
$target,
$source,
$this->filesystem->normalizePath($source),
$this->filesystem->normalizePath(realpath($target))
)
);
}
}
return true;
}
/**
* Checks if the target file should be added based on the given mode.
*
* @param string $target The target path.
*
* @param int $mode The overwrite mode.
*
* @return bool
*
* @throws \RuntimeException If target exists and can not or must not be removed.
*/
private function canAddTarget($target, $mode)
{
// Mode is set to ignore existing targets
if ($mode === self::DUPLICATE_IGNORE) {
return false;
}
// Error if we're not allowed to overwrite or can't remove the existing target
if ($mode !== self::DUPLICATE_OVERWRITE || !$this->filesystem->remove($target)) {
throw new \RuntimeException(sprintf('Installation target "%s" already exists', $target));
}
return true;
}
/**
* Log the creation of a symlink.
*
* @param string $source The source path.
*
* @param string $target The target path.
*
* @return void
*/
private function logSymlink($source, $target)
{
$this->logVeryVerbose(sprintf(' - Linking "%s" to "%s"', $source, $target));
}
/**
* Log the copy'ing of a file.
*
* @param string $source The source path.
*
* @param string $target The target path.
*
* @return void
*/
private function logCopy($source, $target)
{
$this->logVeryVerbose(sprintf(' - Copying "%s" to "%s"', $source, $target));
}
/**
* Log the removal of a symlink or file.
*
* @param string $target The target path.
*
* @return void
*/
private function logRemove($target)
{
$this->logVeryVerbose(sprintf(' - Removing "%s"', $target));
}
/**
* Log a message if verbose.
*
* @param string $message The message to log.
*
* @return void
*/
private function logVerbose($message)
{
if ($this->io->isVerbose()) {
$this->io->writeError($message);
}
}
/**
* Log a message if very verbose.
*
* @param string $message The message to log.
*
* @return void
*/
private function logVeryVerbose($message)
{
if ($this->io->isVeryVerbose()) {
$this->io->writeError($message);
}
}
}