-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfileInstaller.php
581 lines (474 loc) · 20.2 KB
/
ProfileInstaller.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
<?php
/**
* @file ProfileInstaller.php
* Provides BaseProfile class.
*/
class ProfileInstaller {
private static $instance;
private $baseprofile_name;
private $baseprofile_path;
private $hook_implementations;
private $hook_invocations;
private $included_profiles;
// Note: 'install_profile_modules' is the variable name used by Drupal core.
// It's reused here for consistency with core, even though
// install_profile_dependencies may seem more intuitive and consistent.
private $install_profile_modules;
private $install_profile_dependency_removals;
private $install_callbacks;
private $install_state;
private $install_tasks_alter_implementations; // @todo Remove.
private $install_tasks_alters_status; // @todo Remove.
private $install_configure_form_alter_implementations; // @todo remove.
private $install_configure_form_alters_status; // @todo remove.
private function __construct($baseprofile_name) {
$this->setBaseProfileName($baseprofile_name);
$this->setBaseProfilePath();
$this->setIncludedProfiles();
$this->setInstallProfileModules();
$this->setInstallCallbacks();
}
public static function getInstallerForProfile($profile_name = '') {
if (empty(self::$instance) && !empty($profile_name)) {
self::$instance = new self($profile_name);
}
elseif (empty(self::$instance) && empty($profile_name)) {
throw new Exception('Installer must be instantiated or a particular install profile. Please pass in name as an argument.');
}
return self::$instance;
}
public static function installProfilesIncludedByProfile($profile) {
$installer = new self($profile);
$installer->install();
}
/**
* Run install script (invoke hook_install) for included profiles.
*/
public function install() {
foreach ($this->install_callbacks as $callback => $path) {
include_once $path;
call_user_func($callback);
}
}
public static function profileExists($profile_name, $raise_exception = FALSE) {
$path = self::getPathToProfile($profile_name);
$exists = is_dir($path);
return $exists;
}
public function getInstallTasks(array $install_state) {
$tasks = array(
'profile_installer_install_profiles' => array(
'display_name' => st('Install profiles'),
'type' => 'normal',
),
);
// Keep track of state. Only invoke hooks once per state, so we don't get
// trapped in a loop.
$hook = 'hook_install_tasks';
if ($this->isNewStateForHookInvocations($install_state, $hook)) {
$this->setUpNewHookInvocationsForState($hook, $install_state);
}
// Give included profiles an opportunity to add tasks.
$invocations = $this->getHookInvocationsForState($hook, $install_state);
foreach ($invocations as $invocation) {
if ($this->hookInvocationHasNotBeenCalled($invocation)) {
$this->updateInvocationToInvoked($invocation);
include_once $this->getFileWithHookImplementation($invocation);
$function = $invocation['function'];
$more_tasks = $function($install_state);
$tasks = array_merge($tasks, $more_tasks);
}
}
// Store this so it can be returned in case anyone alters it, when passed by
// reference in hook_install_tasks;
$this->install_state = $install_state;
return $tasks;
}
/**
* WARNING: This should only be called by hook_install_tasks. That's the only
* place we're tracking current install_state.
*
* @return array
*/
public function getInstallState() {
return $this->install_state;
}
public function alterInstallTasks($tasks, $install_state) {
// Use our own handler for installing profile's modules.
$tasks['install_profile_modules']['function'] = 'profile_installer_install_modules';
// Keep track of hook invocations.
if ($this->isNewInstallState($install_state)) {
$this->setUpNewInstallTaskAlterImplementationsForInstallState($install_state);
}
// Give included profiles an opportunity to alter tasks (once per install
// state so we don't get trapped in a loop).
$implementations = $this->getInstallTasksAlterImplementationsForInstallState($install_state);
foreach ($implementations as $function => $implementation_info) {
if ($this->hookImplementationHasNotBeenInvoked($implementation_info)) {
$this->updateHookImplementationStatusToInvoked($implementation_info);
include_once $this->getFileWithHookImplementation($implementation_info);
$function($tasks, $install_state);
}
}
return $tasks;
}
/**
* Checks whether hooks have been registered to be invoked for this state yet.
*
* @param array $state
* This can be $form_state or $install_state.
*
* @return bool
*/
private function isNewStateForHookInvocations(array $state, $hook) {
$key = $this->getKeyForArray($state);
$is_new = !isset($this->hook_invocations[$hook][$key]);
return $is_new;
}
private function isNewInstallState($install_state) {
$key = $this->getKeyForInstallState($install_state);
$is_new = !isset($this->install_tasks_alters_status[$key]);
return $is_new;
}
private function setUpNewHookInvocationsForState($hook, array $state) {
$implementations = $this->getHookImplementations($hook);
$key = $this->getKeyForArray($state);
if (isset($this->hook_invocations[$hook][$key])) {
throw new Exception ("{$hook} invocations this install_state/form_state have already been set up.");
}
foreach ($implementations as $function => $file) {
$this->hook_invocations[$hook][$key][$function]['function'] = $function;
$this->hook_invocations[$hook][$key][$function]['file'] = $file;
$this->hook_invocations[$hook][$key][$function]['invoked'] = FALSE;
$this->hook_invocations[$hook][$key][$function]['key'] = $key;
$this->hook_invocations[$hook][$key][$function]['hook'] = 'hook_install_tasks_alter';
}
}
private function setUpNewInstallTaskAlterImplementationsForInstallState(array $install_state) {
$implementations = $this->getInstallTasksAlterImplementations();
$key = $this->getKeyForInstallState($install_state);
if (isset($this->install_tasks_alters_status[$key])) {
throw new Exception ('Not new. Alters for this install_state have already been set up.');
}
foreach ($implementations as $function => $file) {
$this->install_tasks_alters_status[$key][$function]['function'] = $function;
$this->install_tasks_alters_status[$key][$function]['file'] = $file;
$this->install_tasks_alters_status[$key][$function]['invoked'] = FALSE;
$this->install_tasks_alters_status[$key][$function]['key'] = $key;
$this->install_tasks_alters_status[$key][$function]['hook'] = 'hook_install_tasks_alter';
}
}
private function hookInvocationHasNotBeenCalled($invocation) {
$hook = $invocation['hook'];
$key = $invocation['key'];
$function = $invocation['function'];
if (!empty($this->hook_invocations[$hook][$key][$function])) {
$has_been_invoked = $this->hook_invocations[$hook][$key][$function]['invoked'];
return !$has_been_invoked;
}
else {
throw new Exception('Something went wrong. $invocation does not include the info needed.');
}
}
private function updateInvocationToInvoked(array $invocation) {
$key = $invocation['key'];
$function = $invocation['function'];
$hook = $invocation['hook'];
$this->hook_invocations[$hook][$key][$function]['invoked'] = TRUE;
}
private function getHookImplementationForProfile($hook, $profile_name) {
$suffix = substr($hook, 4);
return "{$profile_name}_{$suffix}";
}
private function hookImplementationHasNotBeenInvoked($implementation_info) {
return $implementation_info['invoked'];
}
private function updateHookImplementationStatusToInvoked($implementation_info) {
$key = $implementation_info['key'];
$function = $implementation_info['function'];
$hook = $implementation_info['hook'];
if ($hook == 'hook_install_tasks_alter') {
$property = 'install_tasks_alters_status';
}
if ($hook == 'hook_form_install_configure_form_alter') {
$property = 'install_configure_form_alters_status';
}
$this->{$property}[$key][$function]['invoked'] = TRUE;
}
private function getFileWithHookImplementation($implementation_info) {
return $implementation_info['file'];
}
private function getHookInvocationsForState($hook, array $state) {
$key = $this->getKeyForArray($state);
$invocations = !empty($this->hook_invocations[$hook][$key]) ? $this->hook_invocations[$hook][$key] : array();
return $invocations;
}
private function getInstallTasksAlterImplementationsForInstallState(array $install_state) {
$key = $this->getKeyForInstallState($install_state);
$implementations = isset($this->install_tasks_alters_status[$key]) ? $this->install_tasks_alters_status[$key] : array();
return $implementations;
}
private function getHookImplementations($hook) {
$implementations = array();
if (empty($this->hook_implementations)) {
$this->setHookImplementations();
}
if (!empty($this->hook_implementations[$hook])) {
foreach ($this->hook_implementations[$hook] as $function => $file) {
$implementations[$function] = $file;
}
}
return $implementations;
}
private function setHookImplementations() {
$this->hook_implementations = array();
$supported_hooks = $this->getSupportedHooks();
foreach ($supported_hooks as $hook) {
foreach ($this->getIncludedProfiles() as $profile_name) {
$function = $this->getHookImplementationForProfile($hook, $profile_name);
if ($file = $this->findFunctionInProfile($function, $profile_name)) {
$this->hook_implementations[$hook][$function] = $file;
}
}
}
}
private static function getSupportedHooks() {
return array(
'hook_install_tasks',
'hook_install_tasks_alter',
'hook_form_install_configure_form_alter',
);
}
private function getInstallTasksAlterImplementations() {
if (empty($this->install_tasks_alter_implementations)) {
$this->setInstallTasksAlterImplementations();
}
return $this->install_tasks_alter_implementations;
}
private function setInstallTasksAlterImplementations() {
$this->install_tasks_alter_implementations = array();
foreach ($this->getIncludedProfiles() as $profile_name) {
$function = "{$profile_name}_install_tasks_alter";
if ($file = $this->findFunctionInProfile($function, $profile_name)) {
$this->install_tasks_alter_implementations[$function] = $file;
}
}
}
private static function getKeyForInstallState(array $install_state) {
return self::getKeyForArray($install_state);
}
function alterInstallConfigureForm($form, $form_state) {
// Keep track of hook invocations.
if ($this->isNewFormState($form_state)) {
$this->setUpNewInstallConfigureFormAlterImplementationsForFormState($form_state);
}
// Give included profiles an opportunity to alter install_configure_form
// once per form state so we don't get trapped in a loop.
$implementations = $this->getInstallConfigureFormAlterImplementationsForFormState($form_state);
foreach ($implementations as $function => $implementation_info) {
if ($this->hookImplementationHasNotBeenInvoked($implementation_info)) {
$this->updateHookImplementationStatusToInvoked($function, $form_state);
include_once $this->getFileWithHookImplementation($implementation_info);
$function($form, $form_state);
}
}
return $form;
}
private function isNewFormState(array $form_state) {
$key = $this->getKeyForFormState($form_state);
$is_new = !isset($this->install_configure_form_alters_status[$key]);
return $is_new;
}
private function setUpNewInstallConfigureFormAlterImplementationsForFormState(array $form_state) {
$implementations = $this->getInstallConfigureFormAlterImplementations();
$key = $this->getKeyForFormState($form_state);
if (isset($this->install_configure_form_alters_status[$key])) {
throw new Exception ('Not new. Alters for this form_state have already been set up.');
}
foreach ($implementations as $function => $file) {
$this->install_configure_form_alters_status[$key][$function]['function'] = $function;
$this->install_configure_form_alters_status[$key][$function]['file'] = $file;
$this->install_configure_form_alters_status[$key][$function]['invoked'] = FALSE;
$this->install_configure_form_alters_status[$key][$function]['key'] = $key;
$this->install_configure_form_alters_status[$key][$function]['hook'] = 'hook_form_install_configure_form_alter';
}
}
private function getInstallConfigureFormAlterImplementations() {
if (empty($this->install_configure_form_alter_implementations)) {
$this->setInstallConfigureFormAlterImplementations();
}
return $this->install_configure_form_alter_implementations;
}
private function setInstallConfigureFormAlterImplementations() {
$this->install_configure_form_alter_implementations = array();
foreach ($this->getIncludedProfiles() as $profile_name) {
$function = "{$profile_name}_form_install_configure_form_alter";
if ($file = $this->findFunctionInProfile($function, $profile_name)) {
$this->install_configure_form_alter_implementations[$function] = $file;
}
}
}
private function getInstallConfigureFormAlterImplementationsForFormState(array $form_state) {
$key = $this->getKeyForFormState($form_state);
$implementations = isset($this->install_configure_form_alters_status[$key]) ? $this->install_configure_form_alters_status[$key] : array();
return $implementations;
}
private static function getKeyForFormState(array $form_state) {
return self::getKeyForArray($form_state);
}
private static function getKeyForArray(array $array) {
return md5(serialize($array));
}
public function removeInstallProfileModules(array $modules) {
$dependencies = $this->getInstallProfileModules();
$dependencies = $this->removeNeedlesFromHaystack($modules, $dependencies);
$this->setInstallProfileModules($dependencies);
}
public function removeNeedlesFromHaystack(array $needles, array $haystack) {
foreach ($needles as $needle) {
$key = array_search($needle, $haystack);
unset($haystack[$key]);
}
return $haystack;
}
private function findFunctionInProfile($function, $profile_name) {
$install_file = $this->getPathToProfileInstallFile($profile_name);
$profile_file = $this->getPathToProfileProfileFile($profile_name);
include_once $install_file;
if (function_exists($function)) {
return $install_file;
}
include_once $profile_file;
if (function_exists($function)) {
return $profile_file;
}
return FALSE;
}
/**
* Getters and setters. ======================================================
*/
public function setInstallCallbacks($callbacks = array()) {
if (empty($callbacks)) {
$included_profiles = $this->getIncludedProfiles();
foreach ($included_profiles as $profile_name) {
$path = $this->getPathToProfile($profile_name);
$path = "{$path}/{$profile_name}.install";
$func = "{$profile_name}_install";
$callbacks[$func] = $path;
}
}
$this->install_callbacks = $callbacks;
}
public function getInstallCallbacks() {
if (empty($this->install_callbacks)) {
$this->setInstallCallbacks();
}
return $this->install_callbacks;
}
public static function getDependenciesForProfilesIncludedByProfile($baseprofile_name) {
$installer = new self($baseprofile_name);
return $installer->getIncludedProfilesDependencies();
}
function getInstallProfileDependencyRemovals() {
if (empty($this->install_profile_dependency_removals)) {
$this->setInstallProfileDependencyRemovals();
}
return $this->install_profile_dependency_removals;
}
function setInstallProfileDependencyRemovals() {
$removals = $this->getAllDependencyRemovalsForProfile($this->getBaseProfileName());
$this->install_profile_dependency_removals = $removals;
}
public function setInstallProfileModules($modules = array()) {
if (empty($modules) || empty($this->install_profile_modules)) {
$dependencies = $this->getAllDependenciesForProfile( $this->getBaseProfileName() );
$removals = $this->getAllDependencyRemovalsForProfile( $this->getBaseProfileName() );
$modules = $this->removeNeedlesFromHaystack($removals, $dependencies);
}
$this->install_profile_modules = $modules;
}
public function getInstallProfileModules() {
return $this->install_profile_modules;
}
private static function getAllDependenciesForProfile($profile_name) {
// Get top-level dependencies for profile.
$info_file = self::getInfoFileForProfile($profile_name);
$dependencies = self::getDependenciesFromInfoFile($info_file);
// Recurse. Detect included profiles, and get their dependencies.
$profile_names = self::getProfileNamesFromInfoFile($info_file);
foreach ($profile_names as $profile_name) {
$additional_dependencies = self::getAllDependenciesForProfile($profile_name);
$dependencies = array_unique(array_merge($dependencies, $additional_dependencies));
}
return $dependencies;
}
private static function getAllDependencyRemovalsForProfile($profile_name) {
// Get top-level removals for profile.
$info_file = self::getInfoFileForProfile($profile_name);
$removals = self::getDependencyRemovalsFromInfoFile($info_file);
// Recurse. Detect included profiles, and get their dependencies.
$profile_names = self::getProfileNamesFromInfoFile($info_file);
foreach ($profile_names as $profile_name) {
$additional_removals = self::getAllDependencyRemovalsForProfile($profile_name);
$removals = array_unique(array_merge($removals, $additional_removals));
}
return $removals;
}
public static function getInfoFileForProfile($profile_name) {
$path = self::getPathToProfile($profile_name);
return "{$path}/{$profile_name}.info";
}
public static function getDependenciesFromInfoFile($info_file) {
$info = drupal_parse_info_file($info_file);
return isset($info['dependencies']) ? $info['dependencies'] : array();
}
public static function getDependencyRemovalsFromInfoFile($info_file) {
$info = drupal_parse_info_file($info_file);
return isset($info['remove_dependencies']) ? $info['remove_dependencies'] : array();
}
private function setIncludedProfiles() {
$profiles = $this->getIncludedProfiles();
// @todo Add sorting (alpha, weight, etc.).
$this->included_profiles = $profiles;
}
public static function getProfileNamesFromInfoFile($info_file) {
$info = drupal_parse_info_file($info_file);
$profile_names = (isset($info['profiles'])) ? $info['profiles'] : array();
return $profile_names;
}
private function getIncludedProfiles() {
if (empty($this->included_profiles)) {
$profile_name = $this->getBaseProfileName();
$profile_path = $this->getBaseProfilePath();
$info_file = self::getInfoFileForProfile($this->baseprofile_name);
$this->included_profiles = self::getProfileNamesFromInfoFile($info_file);
}
return $this->included_profiles;
}
public static function getPathToProfile($profile_name) {
return DRUPAL_ROOT . "/profiles/{$profile_name}";
}
public static function getPathToProfileInstallFile($profile_name) {
return self::getPathToProfile($profile_name) . "/{$profile_name}.install";
}
public static function getPathToProfileProfileFile($profile_name) {
return self::getPathToProfile($profile_name) . "/{$profile_name}.profile";
}
public function getBaseProfileName() {
return $this->baseprofile_name;
}
public function setBaseProfileName($baseprofile_name) {
if (self::profileExists($baseprofile_name, TRUE)) {
$this->baseprofile_name = $baseprofile_name;
}
}
public function getBaseProfilePath() {
return $this->baseprofile_path;
}
public function setBaseProfilePath() {
if (empty($this->baseprofile_name)) {
throw new Exception("Cannot set baseprofile_path if baseprofile_name is empty.");
}
$this->baseprofile_path = $this->getPathToProfile($this->baseprofile_name);
}
}