php-censor/src/Config.php

211 lines
4.6 KiB
PHP
Raw Normal View History

2016-04-12 19:31:39 +02:00
<?php
2018-03-04 09:56:02 +01:00
namespace PHPCensor;
2016-04-12 19:31:39 +02:00
use Symfony\Component\Yaml\Parser as YamlParser;
class Config
{
2017-11-05 15:48:36 +01:00
/**
* @var Config
*/
2016-04-12 19:31:39 +02:00
protected static $instance;
2017-11-05 15:48:36 +01:00
/**
* @return Config
*/
2016-04-12 19:31:39 +02:00
public static function getInstance()
{
return self::$instance;
}
/**
2016-04-25 19:30:23 +02:00
* @var array
*/
2016-04-20 12:30:26 +02:00
protected $config = [];
2016-04-12 19:31:39 +02:00
2017-11-05 15:48:36 +01:00
/**
* @param array $settings
*/
2016-04-12 19:31:39 +02:00
public function __construct($settings = null)
{
self::$instance = $this;
if (empty($settings)) {
return;
} elseif (is_array($settings)) {
// Array of setting data.
$this->setArray($settings);
} elseif (is_string($settings) && file_exists($settings)) {
$this->loadYaml($settings);
}
}
2017-11-05 15:48:36 +01:00
/**
* @param string $yamlFile
*/
2016-04-12 19:31:39 +02:00
public function loadYaml($yamlFile)
{
// Path to a YAML file.
$parser = new YamlParser();
2018-01-18 15:51:33 +01:00
$yaml = file_get_contents($yamlFile);
2016-04-12 19:31:39 +02:00
$config = (array)$parser->parse($yaml);
if (empty($config)) {
return;
}
$this->setArray($config);
}
/**
2016-04-25 19:30:23 +02:00
* Get a configuration value by key, returning a default value if not set.
2017-11-05 15:48:36 +01:00
*
* @param string $key
* @param mixed $default
*
2016-04-25 19:30:23 +02:00
* @return mixed
*/
2016-04-12 19:31:39 +02:00
public function get($key, $default = null)
{
$keyParts = explode('.', $key);
$selected = $this->config;
$i = -1;
$lastPart = count($keyParts) - 1;
2016-04-12 19:31:39 +02:00
while ($part = array_shift($keyParts)) {
$i++;
if (!array_key_exists($part, $selected)) {
return $default;
}
if ($i === $lastPart) {
2016-04-12 19:31:39 +02:00
return $selected[$part];
} else {
$selected = $selected[$part];
}
}
return $default;
}
/**
2016-04-25 19:30:23 +02:00
* Set a value by key.
2017-11-05 15:48:36 +01:00
*
* @param string $key
* @param mixed $value
*
2016-04-25 19:30:23 +02:00
* @return boolean
*/
2016-04-12 19:31:39 +02:00
public function set($key, $value = null)
{
$this->config[$key] = $value;
2016-04-25 19:30:23 +02:00
return true;
2016-04-12 19:31:39 +02:00
}
/**
2016-04-25 19:30:23 +02:00
* Set an array of values.
2017-11-05 15:48:36 +01:00
*
* @param $array
2016-04-25 19:30:23 +02:00
*/
2016-04-12 19:31:39 +02:00
public function setArray($array)
{
self::deepMerge($this->config, $array);
}
/**
2016-04-25 19:30:23 +02:00
* Short-hand syntax for get()
* @see Config::get()
2017-11-05 15:48:36 +01:00
*
* @param string $key
*
* @return mixed
2016-04-25 19:30:23 +02:00
*/
2016-04-12 19:31:39 +02:00
public function __get($key)
{
return $this->get($key);
}
/**
2016-04-25 19:30:23 +02:00
* Short-hand syntax for set()
* @see Config::set()
2017-11-05 15:48:36 +01:00
*
* @param string $key
* @param mixed $value
*
* @return bool
2016-04-25 19:30:23 +02:00
*/
2016-04-12 19:31:39 +02:00
public function __set($key, $value = null)
{
return $this->set($key, $value);
}
/**
2016-04-25 19:30:23 +02:00
* Is set
2017-11-05 15:48:36 +01:00
*
* @param string $key
*
* @return boolean
2016-04-25 19:30:23 +02:00
*/
2016-04-12 19:31:39 +02:00
public function __isset($key)
{
return isset($this->config[$key]);
}
/**
2016-04-25 19:30:23 +02:00
* Unset
2017-11-05 15:48:36 +01:00
*
* @param string $key
2016-04-25 19:30:23 +02:00
*/
2016-04-12 19:31:39 +02:00
public function __unset($key)
{
unset($this->config[$key]);
}
/**
2017-11-05 15:48:36 +01:00
* Deeply merge the $target array onto the $source array. The $source array will be modified!
*
2016-04-12 19:31:39 +02:00
* @param array $source
* @param array $target
*/
public static function deepMerge(&$source, $target)
{
if (count($source) === 0) {
$source = $target;
return;
}
2018-01-18 15:51:33 +01:00
foreach ($target as $targetKey => $targetValue) {
if (isset($source[$targetKey])) {
if (!is_array($source[$targetKey]) && !is_array($targetValue)) {
2016-04-12 19:31:39 +02:00
// Neither value is an array, overwrite
$source[$targetKey] = $targetValue;
} elseif (is_array($source[$targetKey]) && is_array($targetValue)) {
2016-04-12 19:31:39 +02:00
// Both are arrays, deep merge them
self::deepMerge($source[$targetKey], $targetValue);
} elseif (is_array($source[$targetKey])) {
2016-04-12 19:31:39 +02:00
// Source is the array, push target value
$source[$targetKey][] = $targetValue;
2016-04-12 19:31:39 +02:00
} else {
// Target is the array, push source value and copy back
$targetValue[] = $source[$targetKey];
$source[$targetKey] = $targetValue;
2016-04-12 19:31:39 +02:00
}
} else {
// No merge required, just set the value
$source[$targetKey] = $targetValue;
2016-04-12 19:31:39 +02:00
}
}
}
2017-11-05 15:48:36 +01:00
/**
* @return array
*/
2016-04-12 19:31:39 +02:00
public function getArray()
{
return $this->config;
}
}