forked from twistor/drupal_flysystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflysystem.install
92 lines (79 loc) · 2.84 KB
/
flysystem.install
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
<?php
/**
* @file
* Install, update and uninstall functions for the flysystem module.
*/
use Drupal\Core\Site\Settings;
use Drupal\flysystem\Logger\Convert;
/**
* Implements hook_requirements().
*/
function flysystem_requirements($phase) {
$requirements = [];
__flysystem_validate_schemes($requirements);
__flysystem_check_dependencies($requirements);
if ($phase === 'runtime') {
__flysystem_call_ensure($requirements);
}
return $requirements;
}
/**
* Implements hook_install().
*/
function flysystem_install() {
Drupal::service('flysystem_factory')->ensure();
}
/**
* Validates configured schemes.
*/
function __flysystem_validate_schemes(array &$requirements) {
$invalid = [];
foreach (array_keys(Settings::get('flysystem', [])) as $scheme) {
if (!preg_match('/^[a-zA-Z0-9+.-]+$/', $scheme)) {
$invalid[] = $scheme;
}
}
if ($invalid) {
$requirements['flysystem_invalid_scheme'] = [
'title' => \Drupal::translation()->translate('Invalid Flysystem schemes in settings.php'),
'description' => \Drupal::translation()->translate("The following schemes are not in the correct format: %schemes. Scheme names can only contain letters, numbers, + (plus sign), . (period), - (hyphen).", ['%schemes' => implode(', ', $invalid)]),
'severity' => REQUIREMENT_ERROR,
];
}
}
/**
* Checks that dependencies are installed.
*/
function __flysystem_check_dependencies(array &$requirements) {
$dependencies = [
'League\Flysystem\Filesystem' => \Drupal::translation()->translate('Flysystem'),
'League\Flysystem\Replicate\ReplicateAdapter' => \Drupal::translation()->translate('replicate adapter'),
'Twistor\FlysystemStreamWrapper' => \Drupal::translation()->translate('stream wrapper'),
];
$missing = array_filter(array_map(function ($dependency) use ($dependencies) {
return class_exists($dependency) ? FALSE : $dependencies[$dependency];
}, array_keys($dependencies)));
// @codeCoverageIgnoreStart
if ($missing) {
$requirements['flysystem_dependencies'] = [
'title' => \Drupal::translation()->translate('Flysystem'),
'description' => \Drupal::translation()->translate('Dependencies missing: @deps.', ['@deps' => implode(', ', $missing)]),
'severity' => REQUIREMENT_ERROR,
];
}
// @codeCoverageIgnoreEnd
}
/**
* Checks the state of existing configuratio.
*/
function __flysystem_call_ensure(array &$requirements) {
foreach (\Drupal::service('flysystem_factory')->ensure() as $scheme => $errors) {
foreach ($errors as $error) {
$requirements['flysystem:' . $scheme] = [
'title' => \Drupal::translation()->translate('Flysystem: @scheme', ['@scheme' => $scheme]),
'description' => \Drupal::translation()->translate($error['message'], $error['context']),
'severity' => Convert::rfcToHookRequirements($error['severity']),
];
}
}
}