-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigFactoryAdapter.php
142 lines (120 loc) · 3.93 KB
/
ConfigFactoryAdapter.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
<?php
namespace Shelf\Config;
use Shelf\Config\Composer\ComposerHelper;
use Zend\Config\Config as ZendConfig;
use Zend\Config\Factory;
use Zend\Config\Writer\PhpArray;
/**
* Class ConfigFactoryAdapter
* @package Shelf\Config
*/
class ConfigFactoryAdapter extends Factory implements ConfigInterface
{
/**
* @var ComposerHelper
*/
public static $_composerHelper;
/**
* @var ZendConfig
*/
protected static $_config;
/**
* Get Application Settings
* Order:
* - DB Rewrite all
* - Global Rewrite Modules
* - Local Modules
* - Composer Modules
* @return \Zend\Config\Config|array
*/
public static function getConfig()
{
if (! self::$_config) {
$config = [];
$globalConfig = self::_loadGlobalConfig();
if ($globalConfig->get(self::CONFIG_CACHE_KEY) == true
&& file_exists(self::CONFIG_CACHE_FILE)) {
self::$_config = self::fromFile(self::CONFIG_CACHE_FILE);
return self::$_config;
}
$composerModuleConfig = self::_loadComposerModulesConfig();
$localModulesConfig = self::_loadLocalModulesConfig();
//@todo implement DB config
if ($composerModuleConfig instanceof ZendConfig && $localModulesConfig instanceof ZendConfig) {
$config = $composerModuleConfig->merge($localModulesConfig);
} elseif ($localModulesConfig instanceof ZendConfig) {
$config = $localModulesConfig;
}
if ($config instanceof ZendConfig) {
$config->merge($globalConfig);
} else {
$config = $globalConfig;
}
if ($globalConfig->get(self::CONFIG_CACHE_KEY) == true) {
(new PhpArray())->toFile(self::CONFIG_CACHE_FILE, $config);
} else {
if (file_exists(self::CONFIG_CACHE_FILE)) {
unlink(self::CONFIG_CACHE_FILE);
}
}
self::$_config = $config;
}
return self::$_config;
}
/**
* Load Local Modules Settings
* @return array|\Zend\Config\Config
*/
protected static function _loadLocalModulesConfig()
{
return self::fromFiles(glob(self::CONFIG_MODULES_PATTERN), true);
}
/**
* Load Global Settings
* @return array|\Zend\Config\Config
*/
protected static function _loadGlobalConfig()
{
return self::fromFiles(glob(self::CONFIG_GLOBAL_PATTERN), true);
}
/**
* Load all Modules Settings installed via Composer
* @return array|\Zend\Config\Config
*/
protected static function _loadComposerModulesConfig()
{
$composerHelper = self::getComposerHelper();
$installedModules = $composerHelper->getInstalledModules();
if (count($installedModules)) {
$settingsArray = array_map(function ($module) {
if (array_key_exists('path', $module)) {
return self::fromFiles(
glob($module['path'] . '/' . self::CONFIG_COMPOSER_MODULE_INSTALLED_PATTERN),
true
);
}
}, $installedModules);
/** @var \Zend\Config\Config $settings */
$settings = count($settingsArray) ? $settingsArray[0] : [];
if (count($settingsArray) > 1) {
for ($i = 0; $i < count($settingsArray); $i++){
if ($i != 0) {
$settings->merge($settingsArray[$i]);
}
}
}
return $settings;
}
return [];
}
/**
* @return ComposerHelper
*/
public static function getComposerHelper()
{
if (! self::$_composerHelper) {
self::$_composerHelper = new ComposerHelper();
}
return self::$_composerHelper;
}
}