-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension_configuration.drush.inc
207 lines (181 loc) · 5.1 KB
/
extension_configuration.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
<?php
/**
* @file
* Contains extension-configuration drush commands.
*/
/**
* Implements hook_drush_command().
*/
function extension_configuration_drush_command() {
$defaults = [
'arguments' => [
'extension' => 'An extension to process configuration.',
],
'required-arguments' => TRUE,
];
$items['extension-configuration-export'] = $defaults + [
'description' => 'Export configuration to extension config directory.',
'aliases' => ['ec-export'],
];
$items['extension-configuration-import'] = $defaults + [
'description' => 'Import configuration from extension config directory.',
'aliases' => ['ec-import'],
];
$items['extension-configuration-delete'] = $defaults + [
'description' => 'Delete extension configuration from active storage.',
'aliases' => ['ec-delete'],
];
$items['extension-configuration-clean'] = $defaults + [
'description' => 'Remove untracked yml files from the config directory.',
'aliases' => ['ec-clean'],
];
$items['extension-configuration-status'] = $defaults + [
'description' => 'Display configuration status.',
'aliases' => ['ec-status'],
];
return $items;
}
/**
* Drush command callback.
*/
function drush_extension_configuration_export($extension) {
drush_extension_configuration_execute($extension, 'export');
}
/**
* Drush command callback.
*/
function drush_extension_configuration_import($extension) {
drush_extension_configuration_execute($extension, 'import');
}
/**
* Drush command callback.
*/
function drush_extension_configuration_delete($extension) {
drush_extension_configuration_execute($extension, 'delete');
}
/**
* Drush command callback.
*/
function drush_extension_configuration_clean($extension) {
drush_extension_configuration_execute($extension, 'findUntrackedFiles', function (array $result) {
if (count($result['files']) > 0) {
drush_print(dt('The following files will be removed:'));
foreach ($result['files'] as $file) {
drush_print('- ' . $file->filename);
}
if (drush_confirm(dt('Do you really want to continue?'))) {
foreach ($result['files'] as $file) {
file_unmanaged_delete($file->uri);
}
}
}
});
}
/**
* Drush command callback.
*/
function drush_extension_configuration_status($extension) {
drush_extension_configuration_execute($extension, 'status', function (array $result) {
drush_extension_configuration_print_status($result);
if (count($result['messages']) > 0) {
drush_print();
drush_extension_configuration_log_result($result);
}
});
}
/**
* Executes the configuration operation.
*
* @param string $extension
* Extension to be processed.
* @param string $operation
* The operation.
*
* @return mixed
* Callback result or FALSE if an exception was thrown.
*/
function drush_extension_configuration_execute($extension, $operation, callable $callback = NULL) {
try {
$result = Drupal::service('extension_configuration.manager')->{$operation}($extension);
$callback = $callback ?: 'drush_extension_configuration_log_result';
return $callback($result);
}
catch (Exception $exception) {
return drush_set_error($exception->getMessage());
}
}
/**
* Prints command result.
*
* @param array $result
* The command result.
*/
function drush_extension_configuration_log_result(array $result) {
foreach ($result['messages'] as $message) {
drush_log($message[0], $message[1]);
}
}
/**
* Prints configuration status.
*
* @param array $result
* The result to be printed.
*/
function drush_extension_configuration_print_status(array $result) {
/** @var \Drupal\extension_configuration\Manager $manager */
$manager = Drupal::service('extension_configuration.manager');
$statuses = [
$manager::CONFIG_STATUS_DEFAULT => dt('Default'),
$manager::CONFIG_STATUS_MISSING_IN_EXTENSION => dt('Missing in extension'),
$manager::CONFIG_STATUS_MISSING_IN_ACTIVE_STORAGE => dt('Missing in active storage'),
$manager::CONFIG_STATUS_OVERRIDDEN => dt('Overridden'),
];
$rows[] = [
dt('Name'),
dt('Type'),
dt('State'),
];
$rows[] = [
'----------',
'----------',
'----------',
];
foreach ($result['rows'] as $row) {
$rows[] = [
$row[0],
$row[1],
$statuses[$row[2]],
];
}
drush_print_table($rows, TRUE);
}
/**
* Command argument complete callback.
*/
function extension_configuration_extension_configuration_export_complete() {
return pm_complete_extensions();
}
/**
* Command argument complete callback.
*/
function extension_configuration_extension_configuration_import_complete() {
return pm_complete_extensions();
}
/**
* Command argument complete callback.
*/
function extension_configuration_extension_configuration_delete_complete() {
return pm_complete_extensions();
}
/**
* Command argument complete callback.
*/
function extension_configuration_extension_configuration_clean_complete() {
return pm_complete_extensions();
}
/**
* Command argument complete callback.
*/
function extension_configuration_extension_configuration_status_complete() {
return pm_complete_extensions();
}