MadelineProto/src/danog/MadelineProto/Db/DbPropertiesFactory.php

77 lines
2.5 KiB
PHP
Raw Normal View History

2020-04-25 21:57:55 +02:00
<?php
namespace danog\MadelineProto\Db;
2020-05-17 21:04:32 +02:00
use Amp\Promise;
use danog\MadelineProto\Settings\Database\Memory;
use danog\MadelineProto\Settings\Database\Mysql;
use danog\MadelineProto\Settings\Database\Postgres;
use danog\MadelineProto\Settings\Database\Redis;
use danog\MadelineProto\Settings\DatabaseAbstract;
2020-04-28 02:41:06 +02:00
/**
* This factory class initializes the correct database backend for MadelineProto.
*/
abstract class DbPropertiesFactory
2020-04-25 21:57:55 +02:00
{
/**
* Indicates a simple K-V array stored in a database backend.
* Values can be objects or other arrays, but when lots of nesting is required, it's best to split the array into multiple arrays.
*/
const TYPE_ARRAY = 'array';
2020-04-25 21:57:55 +02:00
/**
* @param DatabaseAbstract $dbSettings
* @param string $table
* @param self::TYPE_*|array $propertyType
2020-04-25 21:57:55 +02:00
* @param $value
2020-10-18 15:04:43 +02:00
* @param DriverArray|null $value
2020-04-25 21:57:55 +02:00
*
2020-05-17 21:04:32 +02:00
* @return Promise<DbType>
2020-04-25 21:57:55 +02:00
*
* @internal
*
2020-04-25 21:57:55 +02:00
* @uses \danog\MadelineProto\Db\MemoryArray
* @uses \danog\MadelineProto\Db\MysqlArray
2020-07-30 19:51:16 +02:00
* @uses \danog\MadelineProto\Db\PostgresArray
* @uses \danog\MadelineProto\Db\RedisArray
2020-04-25 21:57:55 +02:00
*/
public static function get(DatabaseAbstract $dbSettings, string $table, $propertyType, $value = null): Promise
2020-04-25 21:57:55 +02:00
{
2020-09-25 22:17:05 +02:00
$config = $propertyType['config'] ?? [];
$propertyType = \is_array($propertyType) ? $propertyType['type'] : $propertyType;
2020-09-24 23:25:54 +02:00
$propertyType = \strtolower($propertyType);
2020-09-26 21:57:06 +02:00
$class = !($config['enableCache'] ?? true) && !$dbSettings instanceof Memory
? __NAMESPACE__.'\\NullCache'
: __NAMESPACE__;
switch (true) {
case $dbSettings instanceof Memory:
2020-09-12 19:06:42 +02:00
$class .= '\\Memory';
2020-04-25 21:57:55 +02:00
break;
case $dbSettings instanceof Mysql:
2020-09-12 19:06:42 +02:00
$class .= '\\Mysql';
2020-04-25 21:57:55 +02:00
break;
case $dbSettings instanceof Postgres:
2020-09-12 19:06:42 +02:00
$class .= '\\Postgres';
break;
case $dbSettings instanceof Redis:
2020-09-12 19:06:42 +02:00
$class .= '\\Redis';
2020-07-30 19:51:16 +02:00
break;
2020-04-25 21:57:55 +02:00
default:
throw new \InvalidArgumentException("Unknown dbType: ".\get_class($dbSettings));
2020-04-25 21:57:55 +02:00
}
2020-04-28 02:41:06 +02:00
/** @var DbType $class */
2020-06-16 17:52:55 +02:00
switch (\strtolower($propertyType)) {
case self::TYPE_ARRAY:
2020-04-25 21:57:55 +02:00
$class .= 'Array';
break;
default:
throw new \InvalidArgumentException("Unknown $propertyType: {$propertyType}");
}
return $class::getInstance($table, $value, $dbSettings);
2020-05-03 03:33:54 +02:00
}
2020-06-16 17:52:55 +02:00
}