-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathConfigMaker.php
212 lines (185 loc) · 6.1 KB
/
ConfigMaker.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php namespace System\Traits;
use Yaml;
use File;
use Lang;
use Event;
use SystemException;
use stdClass;
use Config;
/**
* Config Maker Trait
* Adds configuration based methods to a class
*
* @package october\system
* @author Alexey Bobkov, Samuel Georges
*/
trait ConfigMaker
{
/**
* @var string Specifies a path to the config directory.
*/
protected $configPath;
/**
* Reads the contents of the supplied file and applies it to this object.
* @param array $configFile
* @param array $requiredConfig
* @return array|stdClass
*/
public function makeConfig($configFile = [], $requiredConfig = [])
{
if (!$configFile) {
$configFile = [];
}
/*
* Config already made
*/
if (is_object($configFile)) {
$config = $configFile;
}
/*
* Embedded config
*/
elseif (is_array($configFile)) {
$config = $this->makeConfigFromArray($configFile);
}
/*
* Process config from file contents
*/
else {
if (isset($this->controller) && method_exists($this->controller, 'getConfigPath')) {
$configFile = $this->controller->getConfigPath($configFile);
}
else {
$configFile = $this->getConfigPath($configFile);
}
if (!File::isFile($configFile)) {
throw new SystemException(Lang::get(
'system::lang.config.not_found',
['file' => $configFile, 'location' => get_called_class()]
));
}
$config = Yaml::parseFile($configFile);
/**
* @event system.extendConfigFile
* Provides an opportunity to modify config files
*
* Example usage:
*
* Event::listen('system.extendConfigFile', function ((string) $path, (array) $config) {
* if ($path === '/plugins/author/plugin-name/controllers/mycontroller/config_relation.yaml') {
* unset($config['property_value']['view']['recordUrl']);
* return $config;
* }
* });
*
*/
$publicFile = File::localToPublic($configFile);
if ($results = Event::fire('system.extendConfigFile', [$publicFile, $config])) {
foreach ($results as $result) {
if (!is_array($result)) {
continue;
}
$config = array_merge($config, $result);
}
}
$config = $this->makeConfigFromArray($config);
}
/*
* Validate required configuration
*/
foreach ($requiredConfig as $property) {
if (!property_exists($config, $property)) {
throw new SystemException(Lang::get(
'system::lang.config.required',
['property' => $property, 'location' => get_called_class()]
));
}
}
return $config;
}
/**
* Makes a config object from an array, making the first level keys properties of a new object.
*
* @param array $configArray Config array.
* @return stdClass The config object
*/
public function makeConfigFromArray($configArray = [])
{
$object = new stdClass;
if (!is_array($configArray)) {
return $object;
}
foreach ($configArray as $name => $value) {
$object->{$name} = $value;
}
return $object;
}
/**
* Locates a file based on it's definition. If the file starts with
* the ~ symbol it will be returned in context of the application base path,
* otherwise it will be returned in context of the config path.
* @param string $fileName File to load.
* @param mixed $configPath Explicitly define a config path.
* @return string Full path to the config file.
*/
public function getConfigPath($fileName, $configPath = null)
{
if (!isset($this->configPath)) {
$this->configPath = $this->guessConfigPath();
}
if (!$configPath) {
$configPath = $this->configPath;
}
$fileName = File::symbolizePath($fileName);
if (File::isLocalPath($fileName) ||
(!Config::get('cms.restrictBaseDir', true) && realpath($fileName) !== false)
) {
return $fileName;
}
if (!is_array($configPath)) {
$configPath = [$configPath];
}
foreach ($configPath as $path) {
$_fileName = $path . '/' . $fileName;
if (File::isFile($_fileName)) {
return $_fileName;
}
}
return $fileName;
}
/**
* Guess the package path for the called class.
* @param string $suffix An extra path to attach to the end
* @return string
*/
public function guessConfigPath($suffix = '')
{
$class = get_called_class();
return $this->guessConfigPathFrom($class, $suffix);
}
/**
* Guess the package path from a specified class.
* @param string $class Class to guess path from.
* @param string $suffix An extra path to attach to the end
* @return string
*/
public function guessConfigPathFrom($class, $suffix = '')
{
$classFolder = strtolower(class_basename($class));
$classFile = realpath(dirname(File::fromClass($class)));
return $classFile ? $classFile . '/' . $classFolder . $suffix : null;
}
/**
* Merges two configuration sources, either prepared or not, and returns
* them as a single configuration object.
* @param mixed $configA
* @param mixed $configB
* @return stdClass The config object
*/
public function mergeConfig($configA, $configB)
{
$configA = $this->makeConfig($configA);
$configB = $this->makeConfig($configB);
return (object) array_merge((array) $configA, (array) $configB);
}
}