winter/modules/system/traits/ConfigMaker.php

202 lines
5.6 KiB
PHP
Raw Normal View History

2014-05-14 23:24:20 +10:00
<?php namespace System\Traits;
use Yaml;
2014-05-14 23:24:20 +10:00
use File;
use Lang;
2014-05-17 22:10:01 +10:00
use Event;
2015-01-28 18:03:35 +11:00
use SystemException;
2014-05-17 22:10:01 +10:00
use stdClass;
use Config;
2014-05-14 23:24:20 +10:00
/**
* Config Maker Trait
* Adds configuration based methods to a class
*
* @package october\system
2014-05-14 23:24:20 +10:00
* @author Alexey Bobkov, Samuel Georges
*/
trait ConfigMaker
{
/**
* @var string Specifies a path to the config directory.
*/
protected $configPath;
2014-05-14 23:24:20 +10:00
/**
* Reads the contents of the supplied file and applies it to this object.
* @param array $configFile
* @param array $requiredConfig
* @return array|stdClass
2014-05-14 23:24:20 +10:00
*/
public function makeConfig($configFile = [], $requiredConfig = [])
{
if (!$configFile) {
$configFile = [];
}
2014-05-14 23:24:20 +10:00
/*
* Config already made
2014-05-14 23:24:20 +10:00
*/
if (is_object($configFile)) {
$config = $configFile;
}
/*
* Embedded config
*/
elseif (is_array($configFile)) {
$config = $this->makeConfigFromArray($configFile);
}
2014-05-17 22:10:01 +10:00
/*
* Process config from file contents
2014-05-17 22:10:01 +10:00
*/
else {
2014-10-18 11:58:50 +02:00
if (isset($this->controller) && method_exists($this->controller, 'getConfigPath')) {
$configFile = $this->controller->getConfigPath($configFile);
}
else {
$configFile = $this->getConfigPath($configFile);
2014-10-18 11:58:50 +02:00
}
2014-10-18 11:58:50 +02:00
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) {
2014-10-18 11:58:50 +02:00
if (!is_array($result)) {
continue;
}
$config = array_merge($config, $result);
}
2014-05-17 22:10:01 +10:00
}
$config = $this->makeConfigFromArray($config);
}
2014-05-14 23:24:20 +10:00
/*
* Validate required configuration
*/
2014-05-14 23:24:20 +10:00
foreach ($requiredConfig as $property) {
2014-10-18 11:58:50 +02:00
if (!property_exists($config, $property)) {
throw new SystemException(Lang::get(
'system::lang.config.required',
['property' => $property, 'location' => get_called_class()]
));
}
2014-05-14 23:24:20 +10:00
}
return $config;
}
/**
* Makes a config object from an array, making the first level keys properties of a new object.
*
2014-05-17 18:08:01 +02:00
* @param array $configArray Config array.
* @return stdClass The config object
2014-05-14 23:24:20 +10:00
*/
public function makeConfigFromArray($configArray = [])
{
2016-02-13 14:41:17 +11:00
$object = new stdClass;
2014-05-14 23:24:20 +10:00
2014-10-18 11:58:50 +02:00
if (!is_array($configArray)) {
2014-05-14 23:24:20 +10:00
return $object;
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
foreach ($configArray as $name => $value) {
$object->{$name} = $value;
2014-05-14 23:24:20 +10:00
}
return $object;
}
/**
* Locates a file based on it's definition. If the file starts with
2016-02-13 14:41:17 +11:00
* the ~ symbol it will be returned in context of the application base path,
2014-05-14 23:24:20 +10:00
* 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)
{
2014-10-18 11:58:50 +02:00
if (!isset($this->configPath)) {
2014-05-14 23:24:20 +10:00
$this->configPath = $this->guessConfigPath();
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
2014-10-18 11:58:50 +02:00
if (!$configPath) {
2014-05-14 23:24:20 +10:00
$configPath = $this->configPath;
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
$fileName = File::symbolizePath($fileName);
2014-05-14 23:24:20 +10:00
if (File::isLocalPath($fileName) ||
(!Config::get('cms.restrictBaseDir', true) && realpath($fileName) !== false)
) {
2014-05-14 23:24:20 +10:00
return $fileName;
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
2014-10-18 11:58:50 +02:00
if (!is_array($configPath)) {
2014-05-14 23:24:20 +10:00
$configPath = [$configPath];
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
foreach ($configPath as $path) {
$_fileName = $path . '/' . $fileName;
2014-10-18 11:58:50 +02:00
if (File::isFile($_fileName)) {
return $_fileName;
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
}
2016-02-13 14:41:17 +11:00
return $fileName;
2014-05-14 23:24:20 +10:00
}
/**
* 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 = '')
{
2014-09-29 13:12:34 +10:00
$classFolder = strtolower(class_basename($class));
2014-05-14 23:24:20 +10:00
$classFile = realpath(dirname(File::fromClass($class)));
2018-08-15 19:23:12 +02:00
return $classFile ? $classFile . '/' . $classFolder . $suffix : null;
2014-05-14 23:24:20 +10:00
}
/**
* 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);
}
2014-10-18 11:58:50 +02:00
}