forked from danmilon/platformsh-example-drupal8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlatformify.php
executable file
·166 lines (146 loc) · 5.68 KB
/
Platformify.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
#!/usr/bin/php
<?php
/**
* @file Platform-ifying script for Drupal 8.
*
* The purpose of this script is to reapply the Platform.sh customizations for
* Drupal 8, Composer-Edition. It is based on the Drupal-Composer project.
*
* Because Platform.sh requires a read-only file system, the initial snapshot
* that is used for creating a project MUST be able to run without disk
* modification. For that reason, we cannot simply use the Drupal-Composer
* project directly as both it and the normal Drupal installer need to write to
* the settings.php file. The alternative is to check only the web directory
* into Git, including the Platform.sh-specific settings.php and
* settings.platformsh.php files.
*
* The only files in this repository that should be directly updated are:
*
* - This script, if necessary, but probably not.
* - scripts/platformsh/dist.settings.php, which will overwrite the default Drupal
* settings.php file. Users may customize this file for their particular
* sites.
* - scripts/platformsh/dist.settings.platformsh.php, which is common to all
* projects and should generally not be updated. It maps Platform.sh's
* environment variables to Drupal database credentials, $settings, and so
* forth.
* - scripts/platformsh/dist.drush.yml,
* scripts/platformsh/dist.platformsh_drush.inc,
* scripts/platformsh/dist.platformsh_generate_drush_yml.php, and
* scripts/platformsh/dist.drushrc.php, which are all involved in configuring
* Drush.
* - .platform.app.yaml and .platform/*, which provide the Platform.sh-endorsed
* example configuration for a Drupal 8 site.
*
* To update this starter kit when a new release of Drupal, or a new release of
* the Drupal-Composer project, is available, do the following on a separate
* branch:
*
* git remote add dc git@github.com:drupal-composer/drupal-project.git
* sh scripts/platformsh/update_upstream.sh
* git commit -m "Your message here"
*
*/
namespace Platformsh\DrupalExample;
/**
* The Platformification script for Drupal.
*/
class Platformify {
/**
* Makes necessary changes to the repository to keep it Platform.sh-friendly.
*/
public static function run() {
static::addPatches();
static::useDrupalOrgRepository();
static::composerUpdate();
static::copySettingsFiles();
static::copyDrushFiles();
}
/**
* Execute Composer itself.
*/
protected static function composerUpdate() {
shell_exec(sprintf('cd %s && composer update', escapeshellarg(static::getProjectRoot())));
}
/**
* Copy the Platformified settings files into place.
*/
protected static function copySettingsFiles() {
copy(static::getProjectRoot() . '/scripts/platformsh/dist.settings.php', static::getProjectRoot() . '/web/sites/default/settings.php');
copy(static::getProjectRoot() . '/scripts/platformsh/dist.settings.platformsh.php', static::getProjectRoot() . '/web/sites/default/settings.platformsh.php');
}
/**
* Copy Drush configuration files into place.
*/
protected static function copyDrushFiles() {
copy(static::getProjectRoot() . '/scripts/platformsh/dist.drushrc.php', static::getProjectRoot() . '/web/sites/default/drushrc.php');
copy(static::getProjectRoot() . '/scripts/platformsh/dist.drush.yml', static::getProjectRoot() . '/drush/drush.yml');
copy(static::getProjectRoot() . '/scripts/platformsh/dist.platformsh_drush.inc', static::getProjectRoot() . '/drush/platformsh_drush.inc');
copy(static::getProjectRoot() . '/scripts/platformsh/dist.platformsh_generate_drush_yml.php', static::getProjectRoot() . '/drush/platformsh_generate_drush_yml.php');
}
/**
* Adds necessary patches to the composer.json file.
*
* Never apply a patch here that is not already submitted upstream on
* Drupal.org.
*/
protected static function addPatches() {
// No patches are currently required.
}
/**
* Switch the Drupal endpoint to the Drupal.org one, not 3rd party.
*
* @todo Remove this method once upstream is using the official repositories.
*/
protected static function useDrupalOrgRepository() {
static::updateComposerJson(function (array $composer) {
$composer['repositories'][0]['url'] = 'https://packages.drupal.org/8';
unset($composer['conflict']);
$composer['replace']['drupal/drupal'] = '*';
return $composer;
});
}
/**
* Returns the path to the docroot of this project.
*
* @return string
* The path to the docroot.
*/
protected static function getDocRoot() {
return static::getProjectRoot() . '/web';
}
/**
* Determines the root directory of the current project.
*
* The root directory is defined as "where composer.json is". This function
* is mainly to work around guesswork around where the script is run from vs.
* what the "current working directory" may be as a result.
*
* @return string
* The root directory of the current project.
*/
protected static function getProjectRoot() {
static $dir;
if (empty($dir)) {
$dir = getcwd();
while (!file_exists($dir . '/composer.json')) {
$dir = realpath($dir . '/..');
}
}
return $dir;
}
/**
* Makes specified modifications to the composer.json file.
*
* @param callable $updates
* A callable that takes an array-ified composer.json file and returns
* a modified version of it.
*/
protected static function updateComposerJson(callable $updates) {
$root = static::getProjectRoot();
$composer = json_decode(file_get_contents($root . '/composer.json'), TRUE);
$composer = $updates($composer);
file_put_contents($root . '/composer.json', json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
}
Platformify::run();