Merge branch 'rhymix:develop' into develop

This commit is contained in:
Lastorder 2024-10-12 16:08:02 +09:00 committed by GitHub
commit 88b5281094
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
111 changed files with 2542 additions and 405 deletions

View file

@ -0,0 +1,71 @@
<?php
namespace Rhymix\Framework\Drivers;
/**
* The Queue driver interface.
*/
interface QueueInterface
{
/**
* Create a new instance of the current Queue driver, using the given settings.
*
* @param array $config
* @return QueueInterface
*/
public static function getInstance(array $config): QueueInterface;
/**
* Get the human-readable name of this Queue driver.
*
* @return string
*/
public static function getName(): string;
/**
* Get the list of configuration fields required by this Queue driver.
*
* @return array
*/
public static function getRequiredConfig(): array;
/**
* Get the list of configuration fields optionally used by this Queue driver.
*
* @return array
*/
public static function getOptionalConfig(): array;
/**
* Check if this driver is supported on this server.
*
* @return bool
*/
public static function isSupported(): bool;
/**
* Validate driver configuration.
*
* @param mixed $config
* @return bool
*/
public static function validateConfig($config): bool;
/**
* Add a task.
*
* @param string $handler
* @param ?object $args
* @param ?object $options
* @return int
*/
public function addTask(string $handler, ?object $args = null, ?object $options = null): int;
/**
* Get the first task.
*
* @param int $blocking
* @return ?object
*/
public function getTask(int $blocking = 0): ?object;
}

View file

@ -0,0 +1,131 @@
<?php
namespace Rhymix\Framework\Drivers\Queue;
use Rhymix\Framework\DB as RFDB;
use Rhymix\Framework\Drivers\QueueInterface;
/**
* The DB queue driver.
*/
class DB implements QueueInterface
{
/**
* Create a new instance of the current Queue driver, using the given settings.
*
* @param array $config
* @return QueueInterface
*/
public static function getInstance(array $config): QueueInterface
{
return new self($config);
}
/**
* Get the human-readable name of this Queue driver.
*
* @return string
*/
public static function getName(): string
{
return 'DB';
}
/**
* Get the list of configuration fields required by this Queue driver.
*
* @return array
*/
public static function getRequiredConfig(): array
{
return [];
}
/**
* Get the list of configuration fields optionally used by this Queue driver.
*
* @return array
*/
public static function getOptionalConfig(): array
{
return [];
}
/**
* Check if this driver is supported on this server.
*
* @return bool
*/
public static function isSupported(): bool
{
return true;
}
/**
* Validate driver configuration.
*
* @param mixed $config
* @return bool
*/
public static function validateConfig($config): bool
{
return true;
}
/**
* Constructor.
*
* @param array $config
*/
public function __construct(array $config)
{
}
/**
* Add a task.
*
* @param string $handler
* @param ?object $args
* @param ?object $options
* @return int
*/
public function addTask(string $handler, ?object $args = null, ?object $options = null): int
{
$oDB = RFDB::getInstance();
$stmt = $oDB->prepare('INSERT INTO task_queue (handler, args, options) VALUES (?, ?, ?)');
$result = $stmt->execute([$handler, serialize($args), serialize($options)]);
return $result ? $oDB->getInsertID() : 0;
}
/**
* Get the first task.
*
* @param int $blocking
* @return ?object
*/
public function getTask(int $blocking = 0): ?object
{
$oDB = RFDB::getInstance();
$oDB->beginTransaction();
$stmt = $oDB->query('SELECT * FROM task_queue ORDER BY id LIMIT 1 FOR UPDATE');
$result = $stmt->fetchObject();
$stmt->closeCursor();
if ($result)
{
$stmt = $oDB->prepare('DELETE FROM task_queue WHERE id = ?');
$stmt->execute([$result->id]);
$oDB->commit();
$result->args = unserialize($result->args);
$result->options = unserialize($result->options);
return $result;
}
else
{
$oDB->commit();
return null;
}
}
}

View file

@ -0,0 +1,119 @@
<?php
namespace Rhymix\Framework\Drivers\Queue;
use Rhymix\Framework\Drivers\QueueInterface;
/**
* The Dummy queue driver.
*/
class Dummy implements QueueInterface
{
/**
* Dummy queue for testing.
*/
protected $_dummy_queue;
/**
* Create a new instance of the current Queue driver, using the given settings.
*
* @param array $config
* @return QueueInterface
*/
public static function getInstance(array $config): QueueInterface
{
return new self($config);
}
/**
* Get the human-readable name of this Queue driver.
*
* @return string
*/
public static function getName(): string
{
return 'Dummy';
}
/**
* Get the list of configuration fields required by this Queue driver.
*
* @return array
*/
public static function getRequiredConfig(): array
{
return [];
}
/**
* Get the list of configuration fields optionally used by this Queue driver.
*
* @return array
*/
public static function getOptionalConfig(): array
{
return [];
}
/**
* Check if this driver is supported on this server.
*
* @return bool
*/
public static function isSupported(): bool
{
return true;
}
/**
* Validate driver configuration.
*
* @param mixed $config
* @return bool
*/
public static function validateConfig($config): bool
{
return true;
}
/**
* Constructor.
*
* @param array $config
*/
public function __construct(array $config)
{
}
/**
* Add a task.
*
* @param string $handler
* @param ?object $args
* @param ?object $options
* @return int
*/
public function addTask(string $handler, ?object $args = null, ?object $options = null): int
{
$this->_dummy_queue = (object)[
'handler' => $handler,
'args' => $args,
'options' => $options,
];
return 0;
}
/**
* Get the first task.
*
* @param int $blocking
* @return ?object
*/
public function getTask(int $blocking = 0): ?object
{
$result = $this->_dummy_queue;
$this->_dummy_queue = null;
return $result;
}
}

View file

@ -0,0 +1,197 @@
<?php
namespace Rhymix\Framework\Drivers\Queue;
use Rhymix\Framework\Drivers\QueueInterface;
/**
* The Redis queue driver.
*/
class Redis implements QueueInterface
{
/**
* The Redis connection is stored here.
*/
protected $_conn;
protected $_key;
/**
* Create a new instance of the current Queue driver, using the given settings.
*
* @param array $config
* @return QueueInterface
*/
public static function getInstance(array $config): QueueInterface
{
return new self($config);
}
/**
* Get the human-readable name of this Queue driver.
*
* @return string
*/
public static function getName(): string
{
return 'Redis';
}
/**
* Get the list of configuration fields required by this Queue driver.
*
* @return array
*/
public static function getRequiredConfig(): array
{
return ['host', 'port'];
}
/**
* Get the list of configuration fields optionally used by this Queue driver.
*
* @return array
*/
public static function getOptionalConfig(): array
{
return ['dbnum', 'user', 'pass'];
}
/**
* Check if this driver is supported on this server.
*
* @return bool
*/
public static function isSupported(): bool
{
return class_exists('\\Redis');
}
/**
* Validate driver configuration.
*
* @param mixed $config
* @return bool
*/
public static function validateConfig($config): bool
{
try
{
$test = new \Redis;
$test->connect($config['host'], $config['port'] ?? 6379);
if (isset($config['user']) || isset($config['pass']))
{
$auth = [];
if (isset($config['user']) && $config['user']) $auth[] = $config['user'];
if (isset($config['pass']) && $config['pass']) $auth[] = $config['pass'];
$test->auth(count($auth) > 1 ? $auth : $auth[0]);
}
if (isset($config['dbnum']))
{
$test->select(intval($config['dbnum']));
}
return true;
}
catch (\Throwable $th)
{
return false;
}
}
/**
* Constructor.
*
* @param array $config
*/
public function __construct(array $config)
{
try
{
$this->_conn = new \Redis;
$this->_conn->connect($config['host'], $config['port'] ?? 6379);
if (isset($config['user']) || isset($config['pass']))
{
$auth = [];
if (isset($config['user']) && $config['user']) $auth[] = $config['user'];
if (isset($config['pass']) && $config['pass']) $auth[] = $config['pass'];
$this->_conn->auth(count($auth) > 1 ? $auth : $auth[0]);
}
if (isset($config['dbnum']))
{
$this->_conn->select(intval($config['dbnum']));
}
$this->_key = 'rxQueue_' . substr(hash_hmac('sha1', 'rxQueue_', config('crypto.authentication_key')), 0, 24);
}
catch (\RedisException $e)
{
$this->_conn = null;
}
}
/**
* Add a task.
*
* @param string $handler
* @param ?object $args
* @param ?object $options
* @return int
*/
public function addTask(string $handler, ?object $args = null, ?object $options = null): int
{
$value = serialize((object)[
'handler' => $handler,
'args' => $args,
'options' => $options,
]);
if ($this->_conn)
{
$result = $this->_conn->rPush($this->_key, $value);
return intval($result);
}
else
{
return 0;
}
}
/**
* Get the first task.
*
* @param int $blocking
* @return ?object
*/
public function getTask(int $blocking = 0): ?object
{
if ($this->_conn)
{
if ($blocking > 0)
{
$result = $this->_conn->blpop($this->_key, $blocking);
if (is_array($result) && isset($result[1]))
{
return unserialize($result[1]);
}
else
{
return null;
}
}
else
{
$result = $this->_conn->lpop($this->_key);
if ($result)
{
return unserialize($result);
}
else
{
return null;
}
}
}
else
{
return null;
}
}
}

View file

@ -132,6 +132,11 @@ class SolAPI extends Base implements \Rhymix\Framework\Drivers\SMSInterface
}
}
foreach ($original->getExtraVars() as $key => $value)
{
$options->$key = $value;
}
$data['messages'][] = $options;
}
}