Refactored Config.
This commit is contained in:
parent
e23ad3237a
commit
dd9f43b7dd
37 changed files with 40 additions and 43 deletions
|
|
@ -151,7 +151,7 @@ class Application extends b8\Application
|
|||
*/
|
||||
protected function shouldSkipAuth()
|
||||
{
|
||||
$config = b8\Config::getInstance();
|
||||
$config = Config::getInstance();
|
||||
$disableAuth = (bool)$config->get('php-censor.security.disable_auth', false);
|
||||
$defaultUserId = (integer)$config->get('php-censor.security.default_user_id', 1);
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ use PHPCensor\Helper\MailerFactory;
|
|||
use PHPCensor\Logging\BuildLogger;
|
||||
use PHPCensor\Model\Build;
|
||||
use PHPCensor\Store\Factory;
|
||||
use b8\Config;
|
||||
use PHPCensor\Store\BuildErrorWriter;
|
||||
use Psr\Log\LoggerAwareInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ namespace PHPCensor\Command;
|
|||
use Exception;
|
||||
use PDO;
|
||||
|
||||
use b8\Config;
|
||||
use PHPCensor\Config;
|
||||
use PHPCensor\Store\Factory;
|
||||
use PHPCensor\Model\ProjectGroup;
|
||||
use PHPCensor\Store\UserStore;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace PHPCensor\Command;
|
||||
|
||||
use b8\Config;
|
||||
use PHPCensor\Config;
|
||||
use Monolog\Logger;
|
||||
use PHPCensor\Logging\BuildDBLogHandler;
|
||||
use PHPCensor\Logging\LoggedBuildContextTidier;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace PHPCensor\Command;
|
||||
|
||||
use b8\Config;
|
||||
use PHPCensor\Config;
|
||||
use Monolog\Logger;
|
||||
use PHPCensor\Logging\OutputLogHandler;
|
||||
use PHPCensor\Worker\BuildWorker;
|
||||
|
|
|
|||
214
src/PHPCensor/Config.php
Normal file
214
src/PHPCensor/Config.php
Normal file
|
|
@ -0,0 +1,214 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor;
|
||||
|
||||
use Symfony\Component\Yaml\Parser as YamlParser;
|
||||
|
||||
if (!defined('B8_PATH')) {
|
||||
define('B8_PATH', dirname(__DIR__) . '/B8Framework/');
|
||||
}
|
||||
|
||||
class Config
|
||||
{
|
||||
/**
|
||||
* @var Config
|
||||
*/
|
||||
protected static $instance;
|
||||
|
||||
/**
|
||||
* @return Config
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $config = [];
|
||||
|
||||
/**
|
||||
* @param array $settings
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $yamlFile
|
||||
*/
|
||||
public function loadYaml($yamlFile)
|
||||
{
|
||||
// Path to a YAML file.
|
||||
$parser = new YamlParser();
|
||||
$yaml = file_get_contents($yamlFile);
|
||||
$config = (array)$parser->parse($yaml);
|
||||
|
||||
if (empty($config)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->setArray($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a configuration value by key, returning a default value if not set.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key, $default = null)
|
||||
{
|
||||
$keyParts = explode('.', $key);
|
||||
$selected = $this->config;
|
||||
|
||||
$i = -1;
|
||||
$last_part = count($keyParts) - 1;
|
||||
while ($part = array_shift($keyParts)) {
|
||||
$i++;
|
||||
|
||||
if (!array_key_exists($part, $selected)) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
if ($i === $last_part) {
|
||||
return $selected[$part];
|
||||
} else {
|
||||
$selected = $selected[$part];
|
||||
}
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a value by key.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function set($key, $value = null)
|
||||
{
|
||||
$this->config[$key] = $value;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an array of values.
|
||||
*
|
||||
* @param $array
|
||||
*/
|
||||
public function setArray($array)
|
||||
{
|
||||
self::deepMerge($this->config, $array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Short-hand syntax for get()
|
||||
* @see Config::get()
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($key)
|
||||
{
|
||||
return $this->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Short-hand syntax for set()
|
||||
* @see Config::set()
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function __set($key, $value = null)
|
||||
{
|
||||
return $this->set($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is set
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function __isset($key)
|
||||
{
|
||||
return isset($this->config[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unset
|
||||
*
|
||||
* @param string $key
|
||||
*/
|
||||
public function __unset($key)
|
||||
{
|
||||
unset($this->config[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deeply merge the $target array onto the $source array. The $source array will be modified!
|
||||
*
|
||||
* @param array $source
|
||||
* @param array $target
|
||||
*/
|
||||
public static function deepMerge(&$source, $target)
|
||||
{
|
||||
if (count($source) === 0) {
|
||||
$source = $target;
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($target as $target_key => $target_value) {
|
||||
if (isset($source[$target_key])) {
|
||||
if (!is_array($source[$target_key]) && !is_array($target_value)) {
|
||||
// Neither value is an array, overwrite
|
||||
$source[$target_key] = $target_value;
|
||||
} elseif (is_array($source[$target_key]) && is_array($target_value)) {
|
||||
// Both are arrays, deep merge them
|
||||
self::deepMerge($source[$target_key], $target_value);
|
||||
} elseif (is_array($source[$target_key])) {
|
||||
// Source is the array, push target value
|
||||
$source[$target_key][] = $target_value;
|
||||
} else {
|
||||
// Target is the array, push source value and copy back
|
||||
$target_value[] = $source[$target_key];
|
||||
$source[$target_key] = $target_value;
|
||||
}
|
||||
} else {
|
||||
// No merge required, just set the value
|
||||
$source[$target_key] = $target_value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getArray()
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace PHPCensor\Console;
|
||||
|
||||
use b8\Config;
|
||||
use PHPCensor\Config;
|
||||
use PHPCensor\Store\Factory;
|
||||
use Monolog\Handler\RotatingFileHandler;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace PHPCensor;
|
||||
|
||||
use b8\Config;
|
||||
use b8\Exception\HttpException\ForbiddenException;
|
||||
use b8\Http\Request;
|
||||
use b8\Http\Response;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use b8;
|
||||
use PHPCensor\Config;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Controller;
|
||||
|
||||
|
|
@ -22,7 +22,7 @@ class HomeController extends Controller
|
|||
'left' => [],
|
||||
'right' => [],
|
||||
];
|
||||
$widgets_config = b8\Config::getInstance()->get('php-censor.dashboard_widgets', [
|
||||
$widgets_config = Config::getInstance()->get('php-censor.dashboard_widgets', [
|
||||
'all_projects' => [
|
||||
'side' => 'left',
|
||||
],
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use b8;
|
||||
use PHPCensor\Config;
|
||||
use b8\Exception\HttpException\NotFoundException;
|
||||
use b8\Form;
|
||||
use PHPCensor\Controller;
|
||||
|
|
@ -116,7 +116,7 @@ class UserController extends Controller
|
|||
$language->setLabel(Lang::get('language'));
|
||||
$language->setRequired(true);
|
||||
$language->setOptions(array_merge(
|
||||
[null => Lang::get('default') . ' (' . b8\Config::getInstance()->get('php-censor.language') . ')'],
|
||||
[null => Lang::get('default') . ' (' . Config::getInstance()->get('php-censor.language') . ')'],
|
||||
Lang::getLanguageOptions())
|
||||
);
|
||||
$language->setValue($user->getLanguage());
|
||||
|
|
@ -128,7 +128,7 @@ class UserController extends Controller
|
|||
$perPage->setLabel(Lang::get('per_page'));
|
||||
$perPage->setRequired(true);
|
||||
$perPage->setOptions([
|
||||
null => Lang::get('default') . ' (' . b8\Config::getInstance()->get('php-censor.per_page') . ')',
|
||||
null => Lang::get('default') . ' (' . Config::getInstance()->get('php-censor.per_page') . ')',
|
||||
10 => 10,
|
||||
25 => 25,
|
||||
50 => 50,
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ use PHPCensor\Service\BuildService;
|
|||
use PHPCensor\Store\BuildStore;
|
||||
use PHPCensor\Store\ProjectStore;
|
||||
use b8\Controller;
|
||||
use b8\Config;
|
||||
use PHPCensor\Config;
|
||||
use b8\Exception\HttpException\NotFoundException;
|
||||
use PHPCensor\Store\Factory;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
namespace PHPCensor;
|
||||
|
||||
use b8\Config;
|
||||
|
||||
class Database extends \PDO
|
||||
{
|
||||
const MYSQL_TYPE = 'mysql';
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace PHPCensor\Helper;
|
||||
|
||||
use b8\Config;
|
||||
use PHPCensor\Config;
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace PHPCensor\Helper;
|
||||
|
||||
use b8\Config;
|
||||
use PHPCensor\Config;
|
||||
use PHPCensor\Builder;
|
||||
|
||||
/**
|
||||
|
|
@ -99,13 +99,13 @@ class Email
|
|||
'php-censor.email_settings.from_address',
|
||||
self::DEFAULT_FROM
|
||||
);
|
||||
|
||||
|
||||
if (strpos($from, '<') === false) {
|
||||
return (string)$from;
|
||||
}
|
||||
|
||||
|
||||
preg_match('#^(.*?)<(.*)>$#ui', $from, $fromParts);
|
||||
|
||||
|
||||
return [$fromParts[2] => $fromParts[1]];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace PHPCensor\Helper;
|
||||
|
||||
use b8\Config;
|
||||
use PHPCensor\Config;
|
||||
use GuzzleHttp\Client;
|
||||
use Symfony\Component\Cache\Simple\FilesystemCache;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace PHPCensor\Helper;
|
||||
|
||||
use b8\Config;
|
||||
use PHPCensor\Config;
|
||||
use PHPCensor\Store\Factory;
|
||||
use PHPCensor\Store\UserStore;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace PHPCensor\Helper;
|
||||
|
||||
use b8\Config;
|
||||
use PHPCensor\Config;
|
||||
|
||||
/**
|
||||
* Helper class for dealing with SSH keys.
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ use GuzzleHttp\Client;
|
|||
use PHPCensor\Builder;
|
||||
use PHPCensor\Helper\Bitbucket;
|
||||
use PHPCensor\Helper\Diff;
|
||||
use b8\Config;
|
||||
use PHPCensor\Config;
|
||||
use PHPCensor\Model\Build;
|
||||
use PHPCensor\Model\BuildError;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ use GuzzleHttp\Client;
|
|||
use PHPCensor\Builder;
|
||||
use PHPCensor\Helper\Diff;
|
||||
use PHPCensor\Helper\Github;
|
||||
use b8\Config;
|
||||
use PHPCensor\Config;
|
||||
use PHPCensor\Model\Build;
|
||||
use PHPCensor\Model\BuildError;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace PHPCensor\Model;
|
||||
|
||||
use b8\Config;
|
||||
use PHPCensor\Config;
|
||||
use PHPCensor\Model;
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace PHPCensor\Security\Authentication;
|
||||
|
||||
use b8\Config;
|
||||
use PHPCensor\Config;
|
||||
|
||||
/**
|
||||
* Authentication facade.
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace PHPCensor\Service;
|
||||
|
||||
use b8\Config;
|
||||
use PHPCensor\Config;
|
||||
use Pheanstalk\Pheanstalk;
|
||||
use Pheanstalk\PheanstalkInterface;
|
||||
use PHPCensor\BuildFactory;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace PHPCensor\Store;
|
||||
|
||||
use b8\Config;
|
||||
use PHPCensor\Config;
|
||||
use PHPCensor\Database;
|
||||
use PHPCensor\Model\BuildError;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace PHPCensor;
|
||||
|
||||
use b8\Config;
|
||||
use PHPCensor\Store\Factory;
|
||||
use PHPCensor\Model\User;
|
||||
use PHPCensor\Store\UserStore;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue