1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of web-stack |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Slick\ModuleApi; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Merges two arrays recursively, overriding values from the default array with values from the custom array. |
16
|
|
|
* |
17
|
|
|
* @param array<string, mixed> $default The default array. |
18
|
|
|
* @param array<string, mixed> $custom The custom array. |
19
|
|
|
* |
20
|
|
|
* @return array<string, mixed> The merged array. |
21
|
|
|
*/ |
22
|
|
|
function mergeArrays(array $default, array $custom): array |
23
|
|
|
{ |
24
|
|
|
$base = []; |
25
|
|
|
$names = []; |
26
|
|
|
foreach ($default as $name => $value) { |
27
|
|
|
$isPresent = array_key_exists($name, $custom); |
28
|
|
|
$names[] = $name; |
29
|
|
|
if (is_array($value) && $isPresent) { |
30
|
|
|
$base[$name] = mergeArrays($value, $custom[$name]); |
31
|
|
|
continue; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$base[$name] = $isPresent ? $custom[$name] : $value; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
foreach ($custom as $other => $value) { |
38
|
|
|
if (!in_array($other, $names)) { |
39
|
|
|
$base[$other] = $value; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $base; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Import modules from a file and merge with default modules. |
48
|
|
|
* |
49
|
|
|
* @param string $file The file path of the modules file. |
50
|
|
|
* @param array<string, mixed> $default The default modules array (optional). |
51
|
|
|
* @return array<string, mixed> The merged modules array. |
52
|
|
|
*/ |
53
|
|
|
function importSettingsFile(string $file, array $default = []): array |
54
|
|
|
{ |
55
|
|
|
if (!file_exists($file)) { |
56
|
|
|
return $default; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$fromFile = include $file; |
60
|
|
|
return mergeArrays($default, $fromFile); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Check if a constant exists. |
65
|
|
|
* |
66
|
|
|
* @param string $name The name of the constant. |
67
|
|
|
* @return bool Returns true if the constant exists, false otherwise. |
68
|
|
|
*/ |
69
|
|
|
function constantExists(string $name): bool |
70
|
|
|
{ |
71
|
|
|
return defined($name); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the value of a constant. |
76
|
|
|
* |
77
|
|
|
* @param string $name The name of the constant. |
78
|
|
|
* @param mixed $default The default value to return if the constant does not exist. |
79
|
|
|
* @return mixed Returns the value of the constant if it exists, or the default value if it does not. |
80
|
|
|
*/ |
81
|
|
|
function constantValue(string $name, mixed $default = null): mixed |
82
|
|
|
{ |
83
|
|
|
if (constantExists($name)) { |
84
|
|
|
return constant($name); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $default; |
88
|
|
|
} |
89
|
|
|
|