1 | <?php |
||||
2 | /** |
||||
3 | * SEOmatic plugin for Craft CMS 3.x |
||||
4 | * |
||||
5 | * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful, |
||||
6 | * and flexible |
||||
7 | * |
||||
8 | * @link https://nystudio107.com |
||||
9 | * @copyright Copyright (c) 2017 nystudio107 |
||||
10 | */ |
||||
11 | |||||
12 | namespace nystudio107\seomatic\helpers; |
||||
13 | |||||
14 | use Craft; |
||||
15 | use craft\helpers\StringHelper; |
||||
16 | use nystudio107\seomatic\Seomatic; |
||||
17 | use function is_array; |
||||
18 | |||||
19 | /** |
||||
20 | * @author nystudio107 |
||||
21 | * @package Seomatic |
||||
22 | * @since 3.0.0 |
||||
23 | */ |
||||
24 | class Config |
||||
25 | { |
||||
26 | // Constants |
||||
27 | // ========================================================================= |
||||
28 | |||||
29 | const LOCAL_CONFIG_DIR = 'seomatic-config'; |
||||
30 | |||||
31 | // Static Methods |
||||
32 | // ========================================================================= |
||||
33 | |||||
34 | /** |
||||
35 | * Loads a config file from, trying @craft/config first, then the plugin's |
||||
36 | * @nystudio107/seomatic |
||||
37 | * |
||||
38 | * @param string $filePath |
||||
39 | * @param string|null $alias |
||||
40 | * |
||||
41 | * @return array |
||||
42 | */ |
||||
43 | public static function getConfigFromFile(string $filePath, $alias = null): array |
||||
44 | { |
||||
45 | // Try craft/config first |
||||
46 | $path = self::getConfigFilePath('@config', $filePath); |
||||
47 | if (!file_exists($path)) { |
||||
48 | // Now try our own internal config |
||||
49 | $path = self::getConfigFilePath('@nystudio107/seomatic', $filePath); |
||||
50 | if (!file_exists($path)) { |
||||
51 | if (!$alias) { |
||||
52 | return []; |
||||
53 | } |
||||
54 | // Now the additional alias config |
||||
55 | $path = self::getConfigFilePath($alias, $filePath); |
||||
56 | if (!file_exists($path)) { |
||||
57 | return []; |
||||
58 | } |
||||
59 | } |
||||
60 | } |
||||
61 | |||||
62 | if (!is_array($config = @include $path)) { |
||||
63 | return []; |
||||
64 | } |
||||
65 | |||||
66 | // If it's not a multi-environment config, return the whole thing |
||||
67 | if (!array_key_exists('*', $config)) { |
||||
68 | return $config; |
||||
69 | } |
||||
70 | |||||
71 | // If no environment was specified, just look in the '*' array |
||||
72 | if (Seomatic::$environment === null) { |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
73 | return $config['*']; |
||||
74 | } |
||||
75 | |||||
76 | $mergedConfig = []; |
||||
77 | /** @var array $config */ |
||||
78 | foreach ($config as $env => $envConfig) { |
||||
79 | if ($env === '*' || StringHelper::contains(Seomatic::$environment, $env)) { |
||||
80 | $mergedConfig = ArrayHelper::merge($mergedConfig, $envConfig); |
||||
81 | } |
||||
82 | } |
||||
83 | |||||
84 | return $mergedConfig; |
||||
85 | } |
||||
86 | |||||
87 | /** |
||||
88 | * Load an array of config files, merging them together in a single array |
||||
89 | * |
||||
90 | * @param array $filePaths |
||||
91 | * |
||||
92 | * @return array |
||||
93 | */ |
||||
94 | public static function getMergedConfigFromFiles(array $filePaths): array |
||||
95 | { |
||||
96 | $mergedConfig = []; |
||||
97 | foreach ($filePaths as $filePath) { |
||||
98 | $mergedConfig = ArrayHelper::merge($mergedConfig, self::getConfigFromFile($filePath)); |
||||
99 | } |
||||
100 | |||||
101 | return $mergedConfig; |
||||
102 | } |
||||
103 | |||||
104 | // Private Methods |
||||
105 | // ========================================================================= |
||||
106 | |||||
107 | /** |
||||
108 | * Return a path from an alias and a partial path |
||||
109 | * |
||||
110 | * @param string $alias |
||||
111 | * @param string $filePath |
||||
112 | * |
||||
113 | * @return string |
||||
114 | */ |
||||
115 | private static function getConfigFilePath(string $alias, string $filePath): string |
||||
116 | { |
||||
117 | $path = DIRECTORY_SEPARATOR . ltrim($filePath, DIRECTORY_SEPARATOR); |
||||
118 | $path = Craft::getAlias($alias) |
||||
0 ignored issues
–
show
Are you sure
Craft::getAlias($alias) of type false|string can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
119 | . DIRECTORY_SEPARATOR |
||||
120 | . self::LOCAL_CONFIG_DIR |
||||
121 | . str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path) |
||||
122 | . '.php'; |
||||
123 | |||||
124 | return $path; |
||||
125 | } |
||||
126 | } |
||||
127 |