ConfigMaker should validate config no matter what

This commit is contained in:
Sam Georges 2014-06-20 17:59:19 +10:00
parent 60de683cb8
commit 41aa8944dd
2 changed files with 43 additions and 30 deletions

View File

@ -155,7 +155,7 @@ return [
],
'config' => [
'not_found' => 'Unable to find configuration file :file defined for :location.',
'required' => 'Configuration used in :location must supply a value :property.',
'required' => "Configuration used in :location must supply a value ':property'.",
],
'zip' => [
'extract_failed' => "Unable to extract core file ':file'.",

View File

@ -29,40 +29,52 @@ trait ConfigMaker
*/
public function makeConfig($configFile = [], $requiredConfig = [])
{
// Already made
if (is_object($configFile))
return $configFile;
// Embedded config
if (is_array($configFile))
return $this->makeConfigFromArray($configFile);
/*
* Process config file contents
* Config already made
*/
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::parse(File::get($configFile));
/*
* Extensibility
*/
$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);
}
if (is_object($configFile)) {
$config = $configFile;
}
$config = $this->makeConfigFromArray($config);
/*
* 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::parse(File::get($configFile));
/*
* Extensibility
*/
$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()]));
@ -149,4 +161,5 @@ trait ConfigMaker
$guessedPath = $classFile ? $classFile . '/' . $classFolder . $suffix : null;
return $guessedPath;
}
}