初始化仓库
This commit is contained in:
159
vendor/clagiordano/weblibs-configmanager/src/AbstractConfigManager.php
vendored
Normal file
159
vendor/clagiordano/weblibs-configmanager/src/AbstractConfigManager.php
vendored
Normal file
@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
namespace clagiordano\weblibs\configmanager;
|
||||
|
||||
use Exception;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Class AbstractConfigManager
|
||||
* @package clagiordano\weblibs\configmanager
|
||||
*/
|
||||
abstract class AbstractConfigManager implements IConfigurable
|
||||
{
|
||||
/** @var array $configData */
|
||||
protected $configData = null;
|
||||
/** @var string $configFilePath */
|
||||
protected $configFilePath = null;
|
||||
|
||||
/**
|
||||
* Create config object, optionally automatic load config
|
||||
* from argument $configFilePath
|
||||
*
|
||||
* @param string $configFilePath
|
||||
*/
|
||||
public function __construct($configFilePath = null)
|
||||
{
|
||||
try {
|
||||
$this->loadConfig($configFilePath);
|
||||
} catch (Exception $exception) {
|
||||
/**
|
||||
* Allow not existent file name at construct
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get value pointer from config for get/set value
|
||||
*
|
||||
* @param string $configPath
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function & getValuePointer($configPath)
|
||||
{
|
||||
$configData =& $this->configData;
|
||||
$parts = explode('.', $configPath);
|
||||
$length = count($parts);
|
||||
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
if (!isset($configData[ $parts[ $i ] ])) {
|
||||
$configData[ $parts[ $i ] ] = ($i === $length) ? [] : null;
|
||||
}
|
||||
|
||||
$configData = &$configData[ $parts[ $i ] ];
|
||||
}
|
||||
|
||||
return $configData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get value from config data throught keyValue path
|
||||
*
|
||||
* @param string $configPath
|
||||
* @param mixed $defaultValue
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValue($configPath, $defaultValue = null)
|
||||
{
|
||||
$stored = $this->getValuePointer($configPath);
|
||||
|
||||
return (is_null($stored)
|
||||
? $defaultValue
|
||||
: $stored);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if exist required config for keyValue
|
||||
*
|
||||
* @param string $keyValue
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function existValue($keyValue)
|
||||
{
|
||||
return !is_null($this->getValue($keyValue));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set value in config path
|
||||
*
|
||||
* @param string $configPath
|
||||
* @param mixed $newValue
|
||||
*
|
||||
* @return IConfigurable
|
||||
*/
|
||||
public function setValue($configPath, $newValue)
|
||||
{
|
||||
$configData = &$this->getValuePointer($configPath);
|
||||
$configData = $newValue;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getConfig()
|
||||
{
|
||||
return $this->configData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setConfig($config)
|
||||
{
|
||||
$this->configData = (array)$config;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function convert(IConfigurable $target)
|
||||
{
|
||||
$target->setConfig($this->getConfig());
|
||||
|
||||
return $target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if configFilePath exists and is readable
|
||||
* @return bool
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
protected function checkLoadable()
|
||||
{
|
||||
if ($this->configFilePath !== null) {
|
||||
if (file_exists($this->configFilePath) && is_readable($this->configFilePath)) {
|
||||
/**
|
||||
* Readable
|
||||
*/
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* $configFilePath is not null, but not existent or not readable
|
||||
*/
|
||||
throw new RuntimeException("Failed to read config file from path '{$this->configFilePath}'");
|
||||
}
|
||||
|
||||
/**
|
||||
* $configFilePath is null
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
}
|
||||
72
vendor/clagiordano/weblibs-configmanager/src/ArrayConfigManager.php
vendored
Normal file
72
vendor/clagiordano/weblibs-configmanager/src/ArrayConfigManager.php
vendored
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace clagiordano\weblibs\configmanager;
|
||||
|
||||
use Exception;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Class ArrayConfigManager, class for easily read and access to php config array file.
|
||||
* @package clagiordano\weblibs\configmanager
|
||||
*/
|
||||
class ArrayConfigManager extends AbstractConfigManager
|
||||
{
|
||||
/**
|
||||
* Load config data from file and store it into internal property
|
||||
*
|
||||
* @param null|string $configFilePath
|
||||
*
|
||||
* @return IConfigurable
|
||||
*/
|
||||
public function loadConfig($configFilePath = null)
|
||||
{
|
||||
$this->configFilePath = $configFilePath;
|
||||
if ($this->checkLoadable()) {
|
||||
$this->configData = require $this->configFilePath;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare and write config file on disk
|
||||
*
|
||||
* @param null|string $configFilePath
|
||||
* @param bool $autoReloadConfig
|
||||
*
|
||||
* @return IConfigurable
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function saveConfigFile($configFilePath = null, $autoReloadConfig = false)
|
||||
{
|
||||
if (is_null($configFilePath)) {
|
||||
$configFilePath = $this->configFilePath;
|
||||
}
|
||||
|
||||
$configFileContent = "<?php\n\n";
|
||||
$configFileContent .= "return ";
|
||||
$configFileContent .= var_export($this->configData, true);
|
||||
$configFileContent .= ";\n\n";
|
||||
|
||||
try {
|
||||
file_put_contents($configFilePath, $configFileContent);
|
||||
|
||||
if (is_callable('opcache_invalidate')) {
|
||||
/**
|
||||
* Invalidate opcache for writed file if opcache is available
|
||||
*/
|
||||
opcache_invalidate($configFilePath, true);
|
||||
}
|
||||
} catch (Exception $exception) {
|
||||
throw new RuntimeException(
|
||||
"Failed to write config file to path '{$configFilePath}'\n{$exception->getMessage()}"
|
||||
);
|
||||
}
|
||||
|
||||
if ($autoReloadConfig) {
|
||||
$this->loadConfig($configFilePath);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
14
vendor/clagiordano/weblibs-configmanager/src/ConfigManager.php
vendored
Normal file
14
vendor/clagiordano/weblibs-configmanager/src/ConfigManager.php
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace clagiordano\weblibs\configmanager;
|
||||
|
||||
/**
|
||||
* Class ConfigManager, class for easily read and access to php config array file.
|
||||
* @package clagiordano\weblibs\configmanager
|
||||
* @deprecated This is a wrapper for the same class with the new name,
|
||||
* please use directly ArrayConfigManager instead
|
||||
*/
|
||||
class ConfigManager extends ArrayConfigManager
|
||||
{
|
||||
|
||||
}
|
||||
28
vendor/clagiordano/weblibs-configmanager/src/FileConverter.php
vendored
Normal file
28
vendor/clagiordano/weblibs-configmanager/src/FileConverter.php
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace clagiordano\weblibs\configmanager;
|
||||
|
||||
/**
|
||||
* Class FileConverter
|
||||
* @package clagiordano\weblibs\configmanager
|
||||
*/
|
||||
class FileConverter implements IConvertable
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function convert(IConfigurable $source, IConfigurable $target)
|
||||
{
|
||||
return $target->setConfig($source->getConfig());
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function convertAndSave(IConfigurable $source, IConfigurable $target)
|
||||
{
|
||||
$target->setConfig($source->getConfig());
|
||||
|
||||
return $target->saveConfigFile();
|
||||
}
|
||||
}
|
||||
84
vendor/clagiordano/weblibs-configmanager/src/IConfigurable.php
vendored
Normal file
84
vendor/clagiordano/weblibs-configmanager/src/IConfigurable.php
vendored
Normal file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace clagiordano\weblibs\configmanager;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Class ConfigManager, class for easily read and access to php config array file.
|
||||
* @package clagiordano\weblibs\configmanager
|
||||
*/
|
||||
interface IConfigurable
|
||||
{
|
||||
/**
|
||||
* Load config data from file and store it into internal property
|
||||
*
|
||||
* @param null|string $configFilePath
|
||||
*
|
||||
* @return IConfigurable
|
||||
*/
|
||||
public function loadConfig($configFilePath = null);
|
||||
|
||||
/**
|
||||
* Prepare and write config file on disk
|
||||
*
|
||||
* @param null|string $configFilePath
|
||||
* @param bool $autoReloadConfig
|
||||
*
|
||||
* @return IConfigurable
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function saveConfigFile($configFilePath = null, $autoReloadConfig = false);
|
||||
|
||||
/**
|
||||
* Get value from config data throught keyValue path
|
||||
*
|
||||
* @param string $configPath
|
||||
* @param mixed $defaultValue
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValue($configPath, $defaultValue = null);
|
||||
|
||||
/**
|
||||
* Check if exist required config for keyValue
|
||||
*
|
||||
* @param string $keyValue
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function existValue($keyValue);
|
||||
|
||||
/**
|
||||
* Set value in config path
|
||||
*
|
||||
* @param string $configPath
|
||||
* @param mixed $newValue
|
||||
*
|
||||
* @return IConfigurable
|
||||
*/
|
||||
public function setValue($configPath, $newValue);
|
||||
|
||||
/**
|
||||
* Returns the whole internal configuration as array
|
||||
* @return array
|
||||
*/
|
||||
public function getConfig();
|
||||
|
||||
/**
|
||||
* Sets the whole internal configuration from array
|
||||
*
|
||||
* @param array $config
|
||||
* @return IConfigurable
|
||||
*/
|
||||
public function setConfig($config);
|
||||
|
||||
/**
|
||||
* Converts the current instance into another one provided as argument,
|
||||
* migrating its internal configuration and returning the new one.
|
||||
*
|
||||
* @param IConfigurable $target
|
||||
* @return IConfigurable
|
||||
*/
|
||||
public function convert(IConfigurable $target);
|
||||
}
|
||||
28
vendor/clagiordano/weblibs-configmanager/src/IConvertable.php
vendored
Normal file
28
vendor/clagiordano/weblibs-configmanager/src/IConvertable.php
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace clagiordano\weblibs\configmanager;
|
||||
|
||||
/**
|
||||
* Class FileConverter
|
||||
* @package clagiordano\weblibs\configmanager
|
||||
*/
|
||||
interface IConvertable
|
||||
{
|
||||
/**
|
||||
* Converts source config to target config format
|
||||
*
|
||||
* @param IConfigurable $source
|
||||
* @param IConfigurable $target
|
||||
* @return IConfigurable
|
||||
*/
|
||||
public static function convert(IConfigurable $source, IConfigurable $target);
|
||||
|
||||
/**
|
||||
* Converts source config to target config format and save it on target config file
|
||||
*
|
||||
* @param IConfigurable $source
|
||||
* @param IConfigurable $target
|
||||
* @return IConfigurable
|
||||
*/
|
||||
public static function convertAndSave(IConfigurable $source, IConfigurable $target);
|
||||
}
|
||||
68
vendor/clagiordano/weblibs-configmanager/src/JsonConfigManager.php
vendored
Normal file
68
vendor/clagiordano/weblibs-configmanager/src/JsonConfigManager.php
vendored
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace clagiordano\weblibs\configmanager;
|
||||
|
||||
use Exception;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Class JsonConfigManager
|
||||
* @package clagiordano\weblibs\configmanager
|
||||
*/
|
||||
class JsonConfigManager extends AbstractConfigManager
|
||||
{
|
||||
/**
|
||||
* Load config data from file and store it into internal property
|
||||
*
|
||||
* @param null|string $configFilePath
|
||||
*
|
||||
* @return IConfigurable
|
||||
*/
|
||||
public function loadConfig($configFilePath = null)
|
||||
{
|
||||
$this->configFilePath = $configFilePath;
|
||||
if ($this->checkLoadable()) {
|
||||
if (!is_callable('json_decode')) {
|
||||
throw new RuntimeException('Missing php-json extension');
|
||||
}
|
||||
|
||||
$this->configData = json_decode(file_get_contents($configFilePath), true);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare and write config file on disk
|
||||
*
|
||||
* @param null|string $configFilePath
|
||||
* @param bool $autoReloadConfig
|
||||
*
|
||||
* @return IConfigurable
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function saveConfigFile($configFilePath = null, $autoReloadConfig = false)
|
||||
{
|
||||
if (is_null($configFilePath)) {
|
||||
$configFilePath = $this->configFilePath;
|
||||
}
|
||||
|
||||
try {
|
||||
if (!is_callable('json_encode')) {
|
||||
throw new RuntimeException('Missing php-json extension');
|
||||
}
|
||||
|
||||
file_put_contents($configFilePath, json_encode($this->configData, JSON_UNESCAPED_UNICODE));
|
||||
} catch (Exception $exception) {
|
||||
throw new RuntimeException(
|
||||
"Failed to write config file to path '{$configFilePath}'\n{$exception->getMessage()}"
|
||||
);
|
||||
}
|
||||
|
||||
if ($autoReloadConfig) {
|
||||
$this->loadConfig($configFilePath);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
64
vendor/clagiordano/weblibs-configmanager/src/YamlConfigManager.php
vendored
Normal file
64
vendor/clagiordano/weblibs-configmanager/src/YamlConfigManager.php
vendored
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace clagiordano\weblibs\configmanager;
|
||||
|
||||
use Exception;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
/**
|
||||
* Class YamlConfigManager
|
||||
* @package clagiordano\weblibs\configmanager
|
||||
*/
|
||||
class YamlConfigManager extends AbstractConfigManager
|
||||
{
|
||||
/**
|
||||
* Load config data from file and store it into internal property
|
||||
*
|
||||
* @param null|string $configFilePath
|
||||
*
|
||||
* @return IConfigurable
|
||||
*/
|
||||
public function loadConfig($configFilePath = null)
|
||||
{
|
||||
$this->configFilePath = $configFilePath;
|
||||
if ($this->checkLoadable()) {
|
||||
$this->configData = Yaml::parse(file_get_contents($configFilePath));
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare and write config file on disk
|
||||
*
|
||||
* @param null|string $configFilePath
|
||||
* @param bool $autoReloadConfig
|
||||
*
|
||||
* @return IConfigurable
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function saveConfigFile($configFilePath = null, $autoReloadConfig = false)
|
||||
{
|
||||
if (is_null($configFilePath)) {
|
||||
$configFilePath = $this->configFilePath;
|
||||
}
|
||||
|
||||
try {
|
||||
file_put_contents(
|
||||
$configFilePath,
|
||||
Yaml::dump($this->configData, 2, 2)
|
||||
);
|
||||
} catch (Exception $exception) {
|
||||
throw new RuntimeException(
|
||||
"Failed to write config file to path '{$configFilePath}'\n{$exception->getMessage()}"
|
||||
);
|
||||
}
|
||||
|
||||
if ($autoReloadConfig) {
|
||||
$this->loadConfig($configFilePath);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user