1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Contains \DrupalProject\composer\ScriptHandler.
|
6
|
*/
|
7
|
|
8
|
namespace DrupalProject\composer;
|
9
|
|
10
|
use Composer\Script\Event;
|
11
|
use Composer\Semver\Comparator;
|
12
|
use DrupalFinder\DrupalFinder;
|
13
|
use Symfony\Component\Filesystem\Filesystem;
|
14
|
|
15
|
class ScriptHandler {
|
16
|
|
17
|
public static function createRequiredFiles(Event $event) {
|
18
|
$fs = new Filesystem();
|
19
|
$drupalFinder = new DrupalFinder();
|
20
|
$drupalFinder->locateRoot(getcwd());
|
21
|
$drupalRoot = $drupalFinder->getDrupalRoot();
|
22
|
|
23
|
$dirs = [
|
24
|
'sites/all/modules',
|
25
|
'profiles',
|
26
|
'sites/all/themes',
|
27
|
];
|
28
|
|
29
|
// Required for unit testing
|
30
|
foreach ($dirs as $dir) {
|
31
|
if (!$fs->exists($drupalRoot . '/'. $dir)) {
|
32
|
$fs->mkdir($drupalRoot . '/'. $dir);
|
33
|
$fs->touch($drupalRoot . '/'. $dir . '/.gitkeep');
|
34
|
}
|
35
|
}
|
36
|
|
37
|
// Prepare the settings file for installation
|
38
|
if (!$fs->exists($drupalRoot . '/sites/default/settings.php') && $fs->exists($drupalRoot . '/sites/default/default.settings.php')) {
|
39
|
$fs->copy($drupalRoot . '/sites/default/default.settings.php', $drupalRoot . '/sites/default/settings.php');
|
40
|
$fs->chmod($drupalRoot . '/sites/default/settings.php', 0666);
|
41
|
$event->getIO()->write("Created a sites/default/settings.php file with chmod 0666");
|
42
|
}
|
43
|
|
44
|
// Create the files directory with chmod 0777
|
45
|
if (!$fs->exists($drupalRoot . '/sites/default/files')) {
|
46
|
$oldmask = umask(0);
|
47
|
$fs->mkdir($drupalRoot . '/sites/default/files', 0777);
|
48
|
umask($oldmask);
|
49
|
$event->getIO()->write("Created a sites/default/files directory with chmod 0777");
|
50
|
}
|
51
|
}
|
52
|
|
53
|
/**
|
54
|
* Remove project-internal files after create project.
|
55
|
*/
|
56
|
public static function removeInternalFiles(Event $event) {
|
57
|
$fs = new Filesystem();
|
58
|
|
59
|
// List of files to be removed.
|
60
|
$files = [
|
61
|
'.travis.yml',
|
62
|
'LICENSE',
|
63
|
'README.md',
|
64
|
'phpunit.xml.dist',
|
65
|
];
|
66
|
|
67
|
foreach ($files as $file) {
|
68
|
if ($fs->exists($file)) {
|
69
|
$fs->remove($file);
|
70
|
}
|
71
|
}
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* Checks if the installed version of Composer is compatible.
|
76
|
*
|
77
|
* Composer 1.0.0 and higher consider a `composer install` without having a
|
78
|
* lock file present as equal to `composer update`. We do not ship with a lock
|
79
|
* file to avoid merge conflicts downstream, meaning that if a project is
|
80
|
* installed with an older version of Composer the scaffolding of Drupal will
|
81
|
* not be triggered. We check this here instead of in drupal-scaffold to be
|
82
|
* able to give immediate feedback to the end user, rather than failing the
|
83
|
* installation after going through the lengthy process of compiling and
|
84
|
* downloading the Composer dependencies.
|
85
|
*
|
86
|
* @see https://github.com/composer/composer/pull/5035
|
87
|
*/
|
88
|
public static function checkComposerVersion(Event $event) {
|
89
|
$composer = $event->getComposer();
|
90
|
$io = $event->getIO();
|
91
|
|
92
|
$version = $composer::VERSION;
|
93
|
|
94
|
// The dev-channel of composer uses the git revision as version number,
|
95
|
// try to the branch alias instead.
|
96
|
if (preg_match('/^[0-9a-f]{40}$/i', $version)) {
|
97
|
$version = $composer::BRANCH_ALIAS_VERSION;
|
98
|
}
|
99
|
|
100
|
// If Composer is installed through git we have no easy way to determine if
|
101
|
// it is new enough, just display a warning.
|
102
|
if ($version === '@package_version@' || $version === '@package_branch_alias_version@') {
|
103
|
$io->writeError('<warning>You are running a development version of Composer. If you experience problems, please update Composer to the latest stable version.</warning>');
|
104
|
}
|
105
|
elseif (Comparator::lessThan($version, '1.0.0')) {
|
106
|
$io->writeError('<error>Drupal-project requires Composer version 1.0.0 or higher. Please update your Composer before continuing</error>.');
|
107
|
exit(1);
|
108
|
}
|
109
|
}
|
110
|
|
111
|
}
|