-
Notifications
You must be signed in to change notification settings - Fork 22
/
UpdateSettingsCommands.php
233 lines (221 loc) · 7.17 KB
/
UpdateSettingsCommands.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
<?php
namespace Drush\Commands;
use Consolidation\AnnotatedCommand\AnnotationData;
use Consolidation\AnnotatedCommand\CommandData;
use Drupal\Core\Site\Settings;
use Symfony\Component\Filesystem\Filesystem;
/**
* A Drush command file.
*
* In addition to this file, you need a drush.services.yml
* in root of your module, and a composer.json file that provides the name
* of the services file to use.
*
* See these files for an example of injecting Drupal services:
* - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php
* - http://cgit.drupalcode.org/devel/tree/drush.services.yml
*/
class UpdateSettingsCommands extends DrushCommands
{
/**
* Creates in settings.php from default.settings.php if missing.
*
* @command islandora:settings:create-settings-if-missing
* @bootstrap site
* @usage drush islandora:settings:create-settings-if-missing
* Creates settings.php in sites directory if missing.
*/
public function createSettingsIfMissing()
{
$settings_file = $this->getSettingFilePath();
$fs = new Filesystem();
if (!$fs->exists($settings_file)) {
// After drush site-install has been run it is possible for the default
// directory to be non-writable by anyone.
$site_directory = dirname($settings_file);
$prev = fileperms($site_directory);
$fs->chmod($site_directory, 0775);
$fs->copy(DRUPAL_ROOT . '/sites/default/default.settings.php', $settings_file);
$fs->chmod($site_directory, $prev);
}
}
/**
* Set `config_sync_directory` in settings.php
*
* @command islandora:settings:set-config-sync-directory
* @bootstrap site
* @param $path The path to use for the `config_sync_directory` setting
* @usage drush islandora:settings:set-config-sync-directory ../config/sync
* Sets `config_sync_directory` in settings.php.
*/
public function setConfigSyncDirectory($path)
{
$settings['settings']['config_sync_directory'] = (object) [
'value' => $path,
'required' => TRUE,
];
$this->writeSettings($settings);
}
/**
* Set `hash_salt` in settings.php
*
* @command islandora:settings:set-hash-salt
* @bootstrap site
* @param $salt The value of the salt
* @usage drush islandora:settings:set-hash-salt IpWSRBrkTDKAL_ykij_WJJrnqcDv
* Sets `hash_salt` in settings.php, use something like Crypt::randomBytesBase64(55).
*/
public function setHashSalt($salt)
{
$settings['settings']['hash_salt'] = (object) [
'value' => $salt,
'required' => TRUE,
];
$this->writeSettings($settings);
}
/**
* Set `flysystem` in settings.php
*
* @command islandora:settings:set-flystem-fedora-url
* @bootstrap site
* @param $url The root url to Fedora
* @usage drush islandora:settings:set-flystem-fedora-url http://fcrepo.isle-dc.localhost/fcrepo/rest/
* Sets `flysystem` in settings.php.
*/
public function setFlystemFedoraUrl($url)
{
$settings['settings']['flysystem']['fedora']['driver'] = (object) [
'value' => 'fedora',
'required' => TRUE,
];
$settings['settings']['flysystem']['fedora']['config']['root'] = (object) [
'value' => $url,
'required' => TRUE,
];
$this->writeSettings($settings);
}
/**
* Set `database` settings in settings.php
*
* @command islandora:settings:set-database-settings
* @bootstrap site
* @param $database The name of the database
* @param $username The user name to connect with
* @param $password The password of the user
* @param $host The database host
* @param $port The database port
* @param $driver Database driver defaults to 'mysql'
* @param $prefix Table prefix
*
* Sets `database` in settings.php.
*/
public function setDatabaseSettings(
$database,
$username,
$password,
$host,
$port,
$driver = 'mysql',
$prefix = ''
) {
$default_database['database'] = (object) [
'value' => $database,
'required' => TRUE,
];
$default_database['username'] = (object) [
'value' => $username,
'required' => TRUE,
];
$default_database['password'] = (object) [
'value' => $password,
'required' => TRUE,
];
$default_database['host'] = (object) [
'value' => $host,
'required' => TRUE,
];
$default_database['port'] = (object) [
'value' => $port,
'required' => TRUE,
];
$default_database['prefix'] = (object) [
'value' => $prefix,
'required' => TRUE,
];
$default_database['driver'] = (object) [
'value' => $driver,
'required' => TRUE,
];
$default_database['namespace'] = (object) [
'value' => 'Drupal\\Core\\Database\\Driver\\' . $driver,
'required' => TRUE,
];
$settings['databases']['default']['default'] = $default_database;
$this->writeSettings($settings);
}
/**
* Set `trusted_host_patterns` in settings.php
*
* @command islandora:settings:set-trusted-host-patterns
* @bootstrap site
* @param $patterns List of comma, separated patterns.
* @usage drush islandora:settings:set-trusted-host-patterns "^localhost$,^192\\.168\\.00\\.52$,^127\\.0\\.0\\.1$"
* Sets `trusted_host_patterns` in settings.php.
* Be aware that shell escaping can have an affect on the arguments.
*/
public function setTrustedHostPatterns($patterns)
{
$settings['settings']['trusted_host_patterns'] = (object) [
'value' => explode(',', $patterns),
'required' => TRUE,
];
$this->writeSettings($settings);
}
/**
* Set `reverse_proxy` in settings.php
*
* @command islandora:settings:set-reverse-proxy
* @bootstrap site
* @param $reverse_proxy_ips List of comma separated ip adresses for the reverse proxy.
* @usage drush islandora:settings:set-reverse-proxy
* Sets `reverse_proxy` in settings.php.
* Be aware that shell escaping can have an affect on the arguments.
*/
public function setReverseProxySettings($reverse_proxy_ips) {
$settings['settings']['reverse_proxy'] = (object) [
'value' => TRUE,
'required' => TRUE,
];
$settings['settings']['reverse_proxy_addresses'] = (object) [
'value' => explode(',', $reverse_proxy_ips),
'required' => TRUE,
];
$settings['settings']['reverse_proxy_trusted_headers'] = (object) [
'value' => \Symfony\Component\HttpFoundation\Request::HEADER_X_FORWARDED_FOR |
\Symfony\Component\HttpFoundation\Request::HEADER_X_FORWARDED_PROTO |
\Symfony\Component\HttpFoundation\Request::HEADER_X_FORWARDED_PORT,
'required' => TRUE,
];
$this->writeSettings($settings);
}
/**
* Determine which settings file to update.
*/
private function getSettingFilePath()
{
return DRUPAL_ROOT . "/" . \Drush\Drush::bootstrap()->confPath() . "/settings.php";
}
/**
* Determine which settings file to update.
*/
private function writeSettings($settings)
{
require_once DRUPAL_ROOT . '/core/includes/install.inc';
$settings_file = $this->getSettingFilePath();
$fs = new Filesystem();
$fs->chmod($settings_file, 0755);
new Settings([]);
drupal_rewrite_settings($settings, $settings_file);
$fs->chmod($settings_file, 0400);
}
}