This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
localsettings.drush.inc
241 lines (205 loc) · 7.71 KB
/
localsettings.drush.inc
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
<?php
use machbarmacher\localsettings\Commands\CommandInterface;
use machbarmacher\localsettings\Commands\Commands;
use machbarmacher\localsettings\Compiler;
use machbarmacher\localsettings\CompilerFactory;
use machbarmacher\localsettings\Project;
use machbarmacher\localsettings\CompileMisc;
/**
* Implements hook_drush_command().
*/
function localsettings_drush_command() {
$items = array();
$common = array(
'outputformat' => array(
'default' => 'key-value',
),
'options' => [
'force' => 'Overwrite existing files, defaults to 1.'
],
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT,
);
$items['localsettings-prepare'] = array(
'description' => '1: Prepare ../localsettings/project.php.',
'aliases' => array('lsp', 'ls1'),
// @todo Check if drupals scaffolding conflicts with htaccess patch.
) + $common;
$items['localsettings-compile'] = array(
'description' => '2: Compile from project definition: sites, aliases, DBs, baseurls, Boxfile.',
'arguments' => array(
'include' => 'File to include. See example.project.php. Defaults to ../localsettings/project.php',
),
'aliases' => array('lsc', 'ls2'),
// For --simulate we don't need a drupal root.
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
) + $common;
$items['localsettings-scaffold'] = array(
'description' => '3: Scaffold settings dispatcher, dev settings, and .gitignore.',
'arguments' => array(
'include' => 'File to include. See example.mmm-project.php. Defaults to ../mmm-project.php',
),
'aliases' => array('lss', 'ls3'),
// For --simulate we don't need a drupal root.
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
) + $common;
$items['localsettings-scaffold']['options']['declaration-name'] = 'Declaration to scaffold, defaults to dev.';
$items['localsettings-activate'] = array(
'description' => '4: Finally include settings dispatcher.',
'arguments' => array(
'site' => 'Site. Defaults to "default".',
),
'aliases' => array('lsa', 'ls4'),
) + $common;
$items['localsettings-pre-update'] = array(
'description' => 'Helper: Before updating, restore htaccess from snippets folder.',
'aliases' => array('lsbu'),
) + $common;
$items['localsettings-post-update'] = array(
'description' => 'Helper: After updating, move htaccess to snippets folder.',
'aliases' => array('lspu'),
) + $common;
$items['localsettings-post-clone'] = array(
'description' => 'Helper: After cloning, symlink settings.local and htaccess.',
'aliases' => array('lspc'),
) + $common;
return $items;
}
function drush_localsettings_compile_validate($include = NULL) {
$localsettings_autoload = _localsettings_autoload();
global $localsettings_compiler_factory;
/** @var CompilerFactory $localsettings_compiler_factory */
$localsettings_compiler_factory = new CompilerFactory($include);
return _localsettings_validate_root_or_simulate()
&& $localsettings_compiler_factory->validate()
&& $localsettings_autoload;
}
function drush_localsettings_compile($include = NULL) {
$commands = new Commands();
/** @var CompilerFactory $localsettings_compiler_factory */
global $localsettings_compiler_factory;
$compiler = $localsettings_compiler_factory->get();
$compiler->compileAll($commands);
return _localsettings_process_commands($commands);
}
function drush_localsettings_scaffold_validate($include = NULL) {
$localsettings_autoload = _localsettings_autoload();
if (!$include) {
$include = '../localsettings/project.php';
}
/** @var CompilerFactory $localsettings_compiler_factory */
global $localsettings_compiler_factory;
$localsettings_compiler_factory = new CompilerFactory($include);
return _localsettings_validate_root_or_simulate()
&& $localsettings_compiler_factory->validate()
&& $localsettings_autoload;
}
function drush_localsettings_scaffold($include = NULL) {
$declaration_name = drush_get_option('declaration-name', 'dev');
$commands = new Commands();
global $localsettings_compiler_factory;
/** @var CompilerFactory $localsettings_compiler_factory */
$compiler = $localsettings_compiler_factory->get();
$compiler->scaffold($commands, $declaration_name);
return _localsettings_process_commands($commands);
}
function drush_localsettings_prepare_validate() {
return _localsettings_autoload();
}
function drush_localsettings_prepare() {
$commands = new Commands();
Compiler::prepare($commands);
return _localsettings_process_commands($commands);
}
function drush_localsettings_pre_update_validate() {
return _localsettings_autoload();
}
function drush_localsettings_pre_update() {
$commands = new Commands();
Compiler::preUpdate($commands);
return _localsettings_process_commands($commands);
}
function drush_localsettings_post_update_validate($include = NULL) {
$localsettings_autoload = _localsettings_autoload();
global $localsettings_compiler_factory;
/** @var CompilerFactory $localsettings_compiler_factory */
$localsettings_compiler_factory = new CompilerFactory($include);
return _localsettings_validate_root_or_simulate()
&& $localsettings_compiler_factory->validate()
&& $localsettings_autoload;
}
function drush_localsettings_post_update($include = NULL) {
$commands = new Commands();
global $localsettings_compiler_factory;
/** @var CompilerFactory $localsettings_compiler_factory */
$compiler = $localsettings_compiler_factory->get();
$compiler->postUpdate($commands);
return _localsettings_process_commands($commands);
}
function drush_localsettings_post_clone_validate($include = NULL) {
$localsettings_autoload = _localsettings_autoload();
global $localsettings_compiler_factory;
/** @var CompilerFactory $localsettings_compiler_factory */
$localsettings_compiler_factory = new CompilerFactory($include);
return _localsettings_validate_root_or_simulate()
&& $localsettings_compiler_factory->validate()
&& $localsettings_autoload;
}
function drush_localsettings_post_clone() {
$commands = new Commands();
global $localsettings_compiler_factory;
/** @var CompilerFactory $localsettings_compiler_factory */
$compiler = $localsettings_compiler_factory->get();
$compiler->postClone($commands);
return _localsettings_process_commands($commands);
}
function drush_localsettings_activate_validate($site = 'default') {
return _localsettings_autoload();
}
function drush_localsettings_activate($site = 'default') {
$commands = new Commands();
Compiler::activateSite($commands, $site);
return _localsettings_process_commands($commands);
}
/**
* @return bool
*/
function _localsettings_validate_root_or_simulate() {
// Always try to bootstrap to prepare autoloader.
$has_drupal_root = drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_ROOT);
if (!drush_get_context('DRUSH_SIMULATE')
&& !$has_drupal_root
) {
return drush_set_error('DRUSH_localsettings_ERROR', dt('MMM builder needs to run in a valid drupal root if not called with --simulate.'));
}
return TRUE;
}
/**
* @return bool
*/
function _localsettings_autoload() {
// Hackily guess some autoloader paths.
$paths = [
DRUPAL_ROOT . '/../vendor/autoload.php',
__DIR__ . '/vendor/autoload.php',
];
foreach ($paths as $path) {
if (is_file($path)) {
include $path;
}
}
if (!class_exists(Compiler::class)) {
return drush_set_error('DRUSH_localsettings_ERROR', dt('Autoloader not found.'));
}
return TRUE;
}
/**
* @param CommandInterface $commands
* @return array|bool
*/
function _localsettings_process_commands($commands) {
$results = [];
$commands->execute($results, drush_get_context('DRUSH_SIMULATE'));
if (drush_get_context('DRUSH_SIMULATE') || drush_get_context('DRUSH_VERBOSE')) {
return $results;
}
}