1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Twigfield for Craft CMS |
4
|
|
|
* |
5
|
|
|
* Provides a twig editor field with Twig & Craft API autocomplete |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2022 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\twigfield\helpers; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\helpers\ArrayHelper; |
15
|
|
|
use craft\helpers\StringHelper; |
16
|
|
|
|
17
|
|
|
/** |
|
|
|
|
18
|
|
|
* @author nystudio107 |
|
|
|
|
19
|
|
|
* @package Twigfield |
|
|
|
|
20
|
|
|
* @since 1.0.0 |
|
|
|
|
21
|
|
|
*/ |
|
|
|
|
22
|
|
|
class Config |
23
|
|
|
{ |
24
|
|
|
// Constants |
25
|
|
|
// ========================================================================= |
26
|
|
|
|
27
|
|
|
const PHP_SUFFIX = '.php'; |
28
|
|
|
|
29
|
|
|
// Static Methods |
30
|
|
|
// ========================================================================= |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Loads a config file from, trying @craft/config first, then from @nystudio107/twigfield |
34
|
|
|
* |
35
|
|
|
* @param string $fileName |
|
|
|
|
36
|
|
|
* |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public static function getConfigFromFile(string $fileName): array |
40
|
|
|
{ |
41
|
|
|
$fileName .= self::PHP_SUFFIX; |
42
|
|
|
$currentEnv = Craft::$app->getConfig()->env; |
43
|
|
|
// Try craft/config first |
44
|
|
|
$path = Craft::getAlias('@config/' . $fileName, false); |
45
|
|
|
if ($path === false || !file_exists($path)) { |
46
|
|
|
// Now try our own internal config |
47
|
|
|
$path = Craft::getAlias('@nystudio107/twigfield/config.php', false); |
48
|
|
|
if ($path === false || !file_exists($path)) { |
49
|
|
|
return []; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
// Make sure we got a config file |
53
|
|
|
if (!is_array($config = @include $path)) { |
54
|
|
|
return []; |
55
|
|
|
} |
56
|
|
|
// If it's not a multi-environment config, return the whole thing |
57
|
|
|
if (!array_key_exists('*', $config)) { |
58
|
|
|
return $config; |
59
|
|
|
} |
60
|
|
|
// If no environment was specified, just look in the '*' array |
61
|
|
|
if ($currentEnv === null) { |
62
|
|
|
return $config['*']; |
63
|
|
|
} |
64
|
|
|
$mergedConfig = []; |
65
|
|
|
foreach ($config as $env => $envConfig) { |
66
|
|
|
if ($env === '*' || StringHelper::contains($currentEnv, $env)) { |
67
|
|
|
$mergedConfig = ArrayHelper::merge($mergedConfig, $envConfig); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $mergedConfig; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|