改名
This commit is contained in:
@@ -4,13 +4,17 @@ declare(strict_types=1);
|
|||||||
namespace HttpServer\Abstracts;
|
namespace HttpServer\Abstracts;
|
||||||
|
|
||||||
|
|
||||||
|
use Database\Connection;
|
||||||
use Exception;
|
use Exception;
|
||||||
use PHPMailer\PHPMailer\PHPMailer;
|
use PHPMailer\PHPMailer\PHPMailer;
|
||||||
use PHPMailer\PHPMailer\SMTP;
|
use PHPMailer\PHPMailer\SMTP;
|
||||||
|
use ReflectionException;
|
||||||
use Snowflake\Abstracts\Config;
|
use Snowflake\Abstracts\Config;
|
||||||
use Snowflake\Error\Logger;
|
use Snowflake\Error\Logger;
|
||||||
use Snowflake\Event;
|
use Snowflake\Event;
|
||||||
|
use Snowflake\Exception\ComponentException;
|
||||||
use Snowflake\Exception\ConfigException;
|
use Snowflake\Exception\ConfigException;
|
||||||
|
use Snowflake\Exception\NotFindClassException;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
use Swoole\Coroutine\Server;
|
use Swoole\Coroutine\Server;
|
||||||
use Swoole\Timer;
|
use Swoole\Timer;
|
||||||
@@ -132,4 +136,39 @@ abstract class Callback extends HttpService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ConfigException
|
||||||
|
* @throws ComponentException
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
protected function clearMysqlClient()
|
||||||
|
{
|
||||||
|
$databases = Config::get('databases', false, []);
|
||||||
|
if (empty($databases)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$application = Snowflake::app();
|
||||||
|
foreach ($databases as $name => $database) {
|
||||||
|
/** @var Connection $connection */
|
||||||
|
$connection = $application->get('databases.' . $name, false);
|
||||||
|
if (empty($connection)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$connection->disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ConfigException
|
||||||
|
* @throws ComponentException
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
protected function clearRedisClient()
|
||||||
|
{
|
||||||
|
$redis = Snowflake::app()->getRedis();
|
||||||
|
$redis->destroy();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ class OnBeforeReload extends Callback
|
|||||||
{
|
{
|
||||||
$event = Snowflake::app()->getEvent();
|
$event = Snowflake::app()->getEvent();
|
||||||
$event->trigger(Event::SERVER_BEFORE_RELOAD, [$server]);
|
$event->trigger(Event::SERVER_BEFORE_RELOAD, [$server]);
|
||||||
|
|
||||||
|
$this->clearMysqlClient();
|
||||||
|
$this->clearRedisClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -146,19 +146,13 @@ abstract class Pool extends Component
|
|||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param mixed $callback
|
* @param mixed $callback
|
||||||
* @throws ComponentException
|
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function createByCallback($name, mixed $callback)
|
private function createByCallback($name, mixed $callback)
|
||||||
{
|
{
|
||||||
// if ($this->creates === -1 && !is_callable($callback)) {
|
if ($this->creates === -1 && !is_callable($callback)) {
|
||||||
// $this->creates = $creates = Timer::tick(1000, [$this, 'Heartbeat_detection']);
|
$this->creates = $creates = Timer::tick(1000, [$this, 'Heartbeat_detection']);
|
||||||
//
|
}
|
||||||
// $event = Snowflake::app()->getEvent();
|
|
||||||
// $event->on(Event::SERVER_WORKER_STOP, function () use ($creates) {
|
|
||||||
// Timer::clear($creates);
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
if (!Context::hasContext('create::client::ing::' . $name)) {
|
if (!Context::hasContext('create::client::ing::' . $name)) {
|
||||||
$this->push($name, $this->createClient($name, $callback));
|
$this->push($name, $this->createClient($name, $callback));
|
||||||
Context::remove('create::client::ing::' . $name);
|
Context::remove('create::client::ing::' . $name);
|
||||||
@@ -283,6 +277,7 @@ abstract class Pool extends Component
|
|||||||
*/
|
*/
|
||||||
public function clean(string $name)
|
public function clean(string $name)
|
||||||
{
|
{
|
||||||
|
Timer::clear($this->creates);
|
||||||
if (!Context::inCoroutine() || !isset($this->_items[$name])) {
|
if (!Context::inCoroutine() || !isset($this->_items[$name])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
+114
-109
@@ -24,127 +24,132 @@ use Snowflake\Snowflake;
|
|||||||
class Service extends Component
|
class Service extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
private array $_components = [];
|
private array $_components = [];
|
||||||
|
|
||||||
|
|
||||||
private array $_definition = [];
|
private array $_definition = [];
|
||||||
|
|
||||||
|
|
||||||
protected array $_alias = [];
|
protected array $_alias = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
*
|
* @param bool $try
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws
|
* @throws ComponentException
|
||||||
*/
|
* @throws NotFindClassException
|
||||||
public function get($id): mixed
|
* @throws ReflectionException
|
||||||
{
|
*/
|
||||||
if (isset($this->_components[$id])) {
|
public function get($id, $try = true): mixed
|
||||||
return $this->_components[$id];
|
{
|
||||||
}
|
if (isset($this->_components[$id])) {
|
||||||
if (!isset($this->_definition[$id]) && !isset($this->_alias[$id])) {
|
return $this->_components[$id];
|
||||||
throw new ComponentException("Unknown component ID: $id");
|
}
|
||||||
}
|
if (!isset($this->_definition[$id]) && !isset($this->_alias[$id])) {
|
||||||
if (isset($this->_definition[$id])) {
|
if ($try === false) {
|
||||||
$config = $this->_definition[$id];
|
return null;
|
||||||
if (is_object($config)) {
|
}
|
||||||
return $this->_components[$id] = $config;
|
throw new ComponentException("Unknown component ID: $id");
|
||||||
}
|
}
|
||||||
$object = Snowflake::createObject($config);
|
if (isset($this->_definition[$id])) {
|
||||||
} else {
|
$config = $this->_definition[$id];
|
||||||
$config = $this->_alias[$id];
|
if (is_object($config)) {
|
||||||
|
return $this->_components[$id] = $config;
|
||||||
|
}
|
||||||
|
$object = Snowflake::createObject($config);
|
||||||
|
} else {
|
||||||
|
$config = $this->_alias[$id];
|
||||||
|
|
||||||
$object = Snowflake::createObject($config);
|
$object = Snowflake::createObject($config);
|
||||||
}
|
}
|
||||||
return $this->_components[$id] = $object;
|
return $this->_components[$id] = $object;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $className
|
* @param string $className
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
*/
|
*/
|
||||||
public function setAlias(string $className, string $alias)
|
public function setAlias(string $className, string $alias)
|
||||||
{
|
{
|
||||||
$this->_alias[$className] = $alias;
|
$this->_alias[$className] = $alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @param $definition
|
* @param $definition
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws ComponentException
|
* @throws ComponentException
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
*/
|
*/
|
||||||
public function set($id, $definition): mixed
|
public function set($id, $definition): mixed
|
||||||
{
|
{
|
||||||
if ($definition === NULL) {
|
if ($definition === NULL) {
|
||||||
return $this->remove($id);
|
return $this->remove($id);
|
||||||
}
|
}
|
||||||
unset($this->_components[$id]);
|
unset($this->_components[$id]);
|
||||||
if (is_object($definition) || is_callable($definition, TRUE)) {
|
if (is_object($definition) || is_callable($definition, TRUE)) {
|
||||||
return $this->_definition[$id] = $definition;
|
return $this->_definition[$id] = $definition;
|
||||||
} else if (!is_array($definition)) {
|
} else if (!is_array($definition)) {
|
||||||
throw new ComponentException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
|
throw new ComponentException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
|
||||||
}
|
}
|
||||||
if (!isset($definition['class'])) {
|
if (!isset($definition['class'])) {
|
||||||
throw new ComponentException("The configuration for the \"$id\" component must contain a \"class\" element.");
|
throw new ComponentException("The configuration for the \"$id\" component must contain a \"class\" element.");
|
||||||
} else {
|
} else {
|
||||||
$this->_definition[$id] = $definition;
|
$this->_definition[$id] = $definition;
|
||||||
}
|
}
|
||||||
return $this->get($id);
|
return $this->get($id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function has($id): bool
|
public function has($id): bool
|
||||||
{
|
{
|
||||||
return isset($this->_definition[$id]) || isset($this->_components[$id]) || isset($this->_alias[$id]);
|
return isset($this->_definition[$id]) || isset($this->_components[$id]) || isset($this->_alias[$id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function setComponents(array $data)
|
public function setComponents(array $data)
|
||||||
{
|
{
|
||||||
foreach ($data as $key => $val) {
|
foreach ($data as $key => $val) {
|
||||||
$this->set($key, $val);
|
$this->set($key, $val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __get($name): mixed
|
public function __get($name): mixed
|
||||||
{
|
{
|
||||||
if ($this->has($name)) {
|
if ($this->has($name)) {
|
||||||
return $this->get($name);
|
return $this->get($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
return parent::__get($name);
|
return parent::__get($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function remove($id): bool
|
public function remove($id): bool
|
||||||
{
|
{
|
||||||
unset($this->_components[$id]);
|
unset($this->_components[$id]);
|
||||||
unset($this->_definition[$id]);
|
unset($this->_definition[$id]);
|
||||||
if (isset($this->_alias[$id])) {
|
if (isset($this->_alias[$id])) {
|
||||||
unset($this->_components[$this->_alias[$id]]);
|
unset($this->_components[$this->_alias[$id]]);
|
||||||
unset($this->_definition[$this->_alias[$id]]);
|
unset($this->_definition[$this->_alias[$id]]);
|
||||||
unset($this->_alias[$id]);
|
unset($this->_alias[$id]);
|
||||||
}
|
}
|
||||||
return $this->has($id);
|
return $this->has($id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user