This commit is contained in:
Dennis Eichhorn 2020-02-15 02:00:46 +01:00
parent 135f3f08e4
commit f4ae141981
4 changed files with 95 additions and 251 deletions

View File

@ -15,7 +15,7 @@ declare(strict_types=1);
namespace phpOMS;
use phpOMS\Account\AccountManager;
use phpOMS\Config\SettingsAbstract;
use phpOMS\Config\SettingsInterface;
use phpOMS\DataStorage\Cache\CachePool;
use phpOMS\DataStorage\Cookie\CookieJar;
use phpOMS\DataStorage\Database\DatabasePool;
@ -46,7 +46,7 @@ use phpOMS\Router\RouterInterface;
* @property \phpOMS\Module\ModuleManager $moduleManager
* @property \phpOMS\Dispatcher\Dispatcher $dispatcher
* @property \phpOMS\DataStorage\Cache\CachePool $cachePool
* @property \phpOMS\Config\SettingsAbstract $appSettings
* @property \phpOMS\Config\SettingsInterface $appSettings
* @property \phpOMS\Event\EventManager $eventManager
* @property \phpOMS\Account\AccountManager $accountManager
* @property \phpOMS\Log\FileLogger $logger
@ -93,10 +93,10 @@ class ApplicationAbstract
/**
* Application settings object.
*
* @var SettingsAbstract
* @var SettingsInterface
* @since 1.0.0
*/
protected SettingsAbstract $appSettings;
protected SettingsInterface $appSettings;
/**
* Account manager instance.

View File

@ -1,243 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Config
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Config;
use phpOMS\DataStorage\Cache\CachePool;
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
use phpOMS\DataStorage\Database\Query\Builder;
/**
* Settings class.
*
* Responsible for providing a database/cache bound settings manger
*
* @package phpOMS\Config
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
abstract class SettingsAbstract implements OptionsInterface
{
use OptionsTrait;
/**
* Cache manager (pool).
*
* @var null|CachePool
* @since 1.0.0
*/
protected ?CachePool $cache = null;
/**
* Database connection instance.
*
* @var ConnectionAbstract
* @since 1.0.0
*/
protected ConnectionAbstract $connection;
/**
* Settings table.
*
* @var null|string
* @since 1.0.0
*/
protected static ?string $table = null;
/**
* Columns to identify the value.
*
* @var string[]
* @since 1.0.0
*/
protected static array $columns = [
'id',
];
/**
* Field where the actual value is stored.
*
* @var string
* @since 1.0.0
*/
protected string $valueField = 'option';
/**
* Get option.
*
* Possible usage:
* - Use column key
* - Use combination of module, group, account and name without column key
*
* @param null|int|int[]|string|string[] $columns Column values for filtering
* @param null|string $name Setting name @todo consider to make this an integer?!
* @param null|string $module Module name
* @param null|int $group Group id
* @param null|int $account Account id
*
* @return mixed Option value
*
* @since 1.0.0
*/
public function get(
$columns = null,
string $name = null,
string $module = null,
int $group = null,
int $account = null
) {
$options = [];
$keys = [];
if ($columns === null) {
$key = ($name ?? '') . ':' . ($module ?? '') . ':' . ($group ?? '') . ':' . ($account ?? '');
if ($this->exists($key)) {
$options[$key] = $this->getOption($key);
return \count($options) > 1 ? $options : \reset($options);
}
} else {
if (!\is_array($columns)) {
$keys = [$columns];
} else {
$keys = [];
foreach ($columns as $key) {
$keys[] = \is_string($key) ? (int) \preg_replace('/[^0-9.]/', '', $key) : $key;
}
}
foreach ($keys as $key) {
if ($this->exists($key)) {
$options[$key] = $this->getOption($key);
unset($keys[$key]);
}
}
if (empty($keys)) {
return \count($options) > 1 ? $options : \reset($options);
}
}
try {
$dbOptions = [];
$query = new Builder($this->connection);
$query->select(...static::$columns)
->from($this->connection->prefix . static::$table);
if (!empty($columns)) {
$query->where(static::$columns[0], 'in', $keys);
} else {
if ($name !== null) {
$query->where(static::$columns['name'], '=', $name);
}
if ($module !== null) {
$query->andWhere(static::$columns['module'], '=', $module);
}
if ($group !== null) {
$query->andWhere(static::$columns['group'], '=', $group);
}
if ($account !== null) {
$query->andWhere(static::$columns['account'], '=', $account);
}
}
$sql = $query->toSql();
$sth = $this->connection->con->prepare($sql);
$sth->execute();
$dbOptions = $sth->fetchAll(\PDO::FETCH_KEY_PAIR);
$options += $dbOptions === false ? [] : $dbOptions;
if ($dbOptions === false) {
return \count($options) > 1 ? $options : \reset($options); // @codeCoverageIgnore
}
$this->setOptions($dbOptions);
} catch (\Throwable $e) {
throw $e;
}
return \count($options) > 1 ? $options : \reset($options);
}
/**
* Set option by key.
*
* @param string[] $options Column values for filtering
* @param bool $store Save this Setting immediately to database
*
* @return void
*
* @since 1.0.0
*/
public function set(array $options, bool $store = false) : void
{
$this->setOptions($options);
if ($store) {
$this->connection->con->beginTransaction();
foreach ($options as $key => $option) {
if (\is_string($key)) {
$key = (int) \preg_replace('/[^0-9.]/', '', $key);
}
$query = new Builder($this->connection);
$sql = $query->update($this->connection->prefix . static::$table)
->set([static::$columns[1] => $option])
->where(static::$columns[0], '=', $key)
->toSql();
$sth = $this->connection->con->prepare($sql);
$sth->execute();
}
$this->connection->con->commit();
}
}
/**
* Save options.
*
* @return void
*
* @since 1.0.0
*/
public function save() : void
{
$this->connection->con->beginTransaction();
foreach ($this->options as $key => $option) {
if (\is_string($key)) {
$key = (int) \preg_replace('/[^0-9.]/', '', $key);
}
$query = new Builder($this->connection);
$sql = $query->update($this->connection->prefix . static::$table)
->set([static::$columns[1] => $option])
->where(static::$columns[0], '=', $key)
->toSql();
$sth = $this->connection->con->prepare($sql);
$sth->execute();
}
$this->connection->con->commit();
}
}

View File

@ -0,0 +1,85 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Config
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Config;
/**
* Options class.
*
* @package phpOMS\Config
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
interface SettingsInterface extends OptionsInterface
{
/**
* Get option.
*
* Possible usage:
* - Use column key
* - Use combination of module, group, account and name without column key
*
* @param null|int|int[]|string|string[] $ids Ids
* @param null|string|string[] $names Setting name
* @param null|string $module Module name
* @param null|int $group Group id
* @param null|int $account Account id
*
* @return mixed Option value
*
* @since 1.0.0
*/
public function get(
$ids = null,
$names = null,
string $module = null,
int $group = null,
int $account = null
);
/**
* Set option by key.
*
* @param array<int, array{id?:?int, name?:?string, content:string, module?:?string, group?:?int, account?:?int}> $options Column values for filtering
* @param bool $store Save this Setting immediately to database
*
* @return void
*
* @since 1.0.0
*/
public function set(array $options, bool $store = false) : void;
/**
* Save options.
*
* @param array<int, array{id?:?int, name?:?string, content:string, module?:?string, group?:?int, account?:?int}> $options Options to save
*
* @return void
*
* @since 1.0.0
*/
public function save(array $options = []) : void;
/**
* Create option.
*
* @param array $options Options to save
*
* @return void
*
* @since 1.0.0
*/
public function create(array $options = []) : void;
}

View File

@ -96,7 +96,7 @@ class Builder extends BuilderAbstract
/**
* Into columns.
*
* @var array
* @var array<int, mixed>
* @since 1.0.0
*/
public array $values = [];
@ -888,7 +888,7 @@ class Builder extends BuilderAbstract
$this->type = QueryType::INSERT;
foreach ($columns as $key => $column) {
foreach ($columns as $column) {
$this->inserts[] = $column;
}
@ -951,10 +951,12 @@ class Builder extends BuilderAbstract
public function value($value) : self
{
\end($this->values);
$key = \key($this->values);
$key = \key($this->values);
$key ??= 0;
if (\is_array($value)) {
$this->values[$key] = $value;
$this->values[$key + 1] = $value;
} else {
$this->values[$key][] = $value;
}