111
This commit is contained in:
@@ -66,13 +66,13 @@ class Redis implements StopHeartbeatCheck
|
||||
*/
|
||||
public function heartbeat_check(): void
|
||||
{
|
||||
if (env('state') == 'exit') {
|
||||
if (env('state','start') == 'exit') {
|
||||
return;
|
||||
}
|
||||
if ($this->_timer === -1 && Context::inCoroutine()) {
|
||||
$this->_timer = Timer::tick(1000, function () {
|
||||
try {
|
||||
if (env('state') == 'exit') {
|
||||
if (env('state','start') == 'exit') {
|
||||
echo 'timer end.' . PHP_EOL;
|
||||
}
|
||||
if (time() - $this->_last > 10 * 60) {
|
||||
|
||||
@@ -1,272 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Di;
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use ReflectionAttribute;
|
||||
use ReflectionClass;
|
||||
use ReflectionProperty;
|
||||
|
||||
trait Attributes
|
||||
{
|
||||
|
||||
|
||||
private array $_classTarget = [];
|
||||
private array $_classMethodNote = [];
|
||||
private array $_classMethod = [];
|
||||
private array $_classPropertyNote = [];
|
||||
private array $_classProperty = [];
|
||||
private array $_mapping = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $class
|
||||
*/
|
||||
protected function setTargetNote(ReflectionClass $class)
|
||||
{
|
||||
$className = $class->getName();
|
||||
if (!isset($this->_classTarget[$className])) {
|
||||
$this->_classTarget[$className] = [];
|
||||
}
|
||||
foreach ($class->getAttributes() as $attribute) {
|
||||
if (!class_exists($attribute->getName())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$instance = $attribute->newInstance();
|
||||
|
||||
$this->_classTarget[$className][] = $instance;
|
||||
|
||||
$this->setMappingClass($attribute, $className);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionAttribute $attribute
|
||||
* @param string $class
|
||||
*/
|
||||
private function setMappingClass(ReflectionAttribute $attribute, string $class)
|
||||
{
|
||||
if (!isset($this->_mapping[$attribute->getName()])) {
|
||||
$this->_mapping[$attribute->getName()] = [];
|
||||
}
|
||||
if (!isset($this->_mapping[$attribute->getName()][$class])) {
|
||||
$this->_mapping[$attribute->getName()][$class] = [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionAttribute $attribute
|
||||
* @param string $class
|
||||
* @param string $method
|
||||
* @param mixed $instance
|
||||
*/
|
||||
private function setMappingMethod(ReflectionAttribute $attribute, string $class, string $method, mixed $instance)
|
||||
{
|
||||
$this->setMappingClass($attribute, $class);
|
||||
|
||||
if (!isset($this->_mapping[$attribute->getName()][$class]['method'])) {
|
||||
$this->_mapping[$attribute->getName()][$class]['method'] = [];
|
||||
}
|
||||
$this->_mapping[$attribute->getName()][$class]['method'][] = [$method => $instance];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionAttribute $attribute
|
||||
* @param string $class
|
||||
* @param string $property
|
||||
* @param $instance
|
||||
*/
|
||||
private function setMappingProperty(ReflectionAttribute $attribute, string $class, string $property, $instance)
|
||||
{
|
||||
$this->setMappingClass($attribute, $class);
|
||||
|
||||
$mapping = $this->_mapping[$attribute->getName()][$class];
|
||||
if (!isset($mapping['property'])) {
|
||||
$mapping['property'] = [];
|
||||
}
|
||||
$mapping['property'][] = [$property => $instance];
|
||||
$this->_mapping[$attribute->getName()][$class] = $mapping;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $class
|
||||
* @return array
|
||||
*/
|
||||
public function getTargetNote(mixed $class): array
|
||||
{
|
||||
if (!is_string($class)) {
|
||||
$class = $class::class;
|
||||
}
|
||||
return $this->_classTarget[$class] ?? [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $class
|
||||
*/
|
||||
protected function setMethodNote(ReflectionClass $class)
|
||||
{
|
||||
$className = $class->getName();
|
||||
$this->_classMethodNote[$className] = $this->_classMethod[$className] = [];
|
||||
foreach ($class->getMethods() as $ReflectionMethod) {
|
||||
$this->_classMethod[$className][$ReflectionMethod->getName()] = $ReflectionMethod;
|
||||
$this->_classMethodNote[$className][$ReflectionMethod->getName()] = [];
|
||||
foreach ($ReflectionMethod->getAttributes() as $attribute) {
|
||||
if (!class_exists($attribute->getName())) {
|
||||
continue;
|
||||
}
|
||||
$instance = $attribute->newInstance();
|
||||
|
||||
$this->_classMethodNote[$className][$ReflectionMethod->getName()][] = $instance;
|
||||
|
||||
$this->setMappingMethod($attribute, $className, $ReflectionMethod->getName(), $instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $method
|
||||
* @return bool
|
||||
*/
|
||||
public function hasMethod(string $class, string $method): bool
|
||||
{
|
||||
return isset($this->_classMethod[$class]) && isset($this->_classMethod[$class][$method]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $class
|
||||
* @return array
|
||||
*/
|
||||
#[Pure] public function getMethodNote(ReflectionClass $class): array
|
||||
{
|
||||
return $this->_classMethodNote[$class->getName()] ?? [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $class
|
||||
*/
|
||||
protected function setPropertyNote(ReflectionClass $class)
|
||||
{
|
||||
$className = $class->getName();
|
||||
$this->_classProperty[$className] = $this->_classPropertyNote[$className] = [];
|
||||
foreach ($class->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PUBLIC |
|
||||
ReflectionProperty::IS_PROTECTED) as $ReflectionMethod) {
|
||||
$this->_classProperty[$className][$ReflectionMethod->getName()] = $ReflectionMethod;
|
||||
foreach ($ReflectionMethod->getAttributes() as $attribute) {
|
||||
if (!class_exists($attribute->getName())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$instance = $attribute->newInstance();
|
||||
|
||||
$this->_classPropertyNote[$className][$ReflectionMethod->getName()] = $instance;
|
||||
|
||||
$this->setMappingProperty($attribute, $className, $ReflectionMethod->getName(), $instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $attribute
|
||||
* @param string|null $class
|
||||
* @return array[]
|
||||
*/
|
||||
public function getAttributeTrees(string $attribute, string $class = null): array
|
||||
{
|
||||
$mapping = $this->_mapping[$attribute] ?? [];
|
||||
if (empty($mapping) || empty($class)) {
|
||||
return $mapping;
|
||||
}
|
||||
return $mapping[$class] ?? [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $attribute
|
||||
* @param string $class
|
||||
* @param string|null $method
|
||||
* @return array
|
||||
*/
|
||||
public function getMethodByAnnotation(string $attribute, string $class, string $method = null): mixed
|
||||
{
|
||||
$class = $this->getAttributeTrees($attribute, $class);
|
||||
if (empty($class) || !isset($class['method']) || empty($method)) {
|
||||
return $class['method'] ?? [];
|
||||
}
|
||||
foreach ($class['method'] as $value) {
|
||||
$key = key($value);
|
||||
if ($method == $key) {
|
||||
return $value[$key];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $attribute
|
||||
* @param string $class
|
||||
* @param string $method
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPropertyByAnnotation(string $attribute, string $class, string $method): mixed
|
||||
{
|
||||
$class = $this->getAttributeTrees($attribute, $class);
|
||||
if (empty($class) || !isset($class['property'])) {
|
||||
return [];
|
||||
}
|
||||
foreach ($class['property'] as $value) {
|
||||
$key = key($value);
|
||||
if ($method == $key) {
|
||||
return $value[$key];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass|string $class
|
||||
* @return array
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function getMethods(ReflectionClass|string $class): array
|
||||
{
|
||||
if (is_string($class)) {
|
||||
$class = $this->getReflect($class);
|
||||
}
|
||||
return $this->_classMethod[$class->getName()] ?? [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $class
|
||||
* @return ReflectionProperty[]
|
||||
*/
|
||||
#[Pure] public function getProperty(ReflectionClass $class): array
|
||||
{
|
||||
return $this->_classProperty[$class->getName()] ?? [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $class
|
||||
* @return array
|
||||
*/
|
||||
#[Pure] public function getPropertyNote(ReflectionClass $class): array
|
||||
{
|
||||
return $this->_classPropertyNote[$class->getName()] ?? [];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -34,8 +34,6 @@ use Server\ResponseInterface;
|
||||
class Container extends BaseObject implements ContainerInterface
|
||||
{
|
||||
|
||||
use Attributes;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*
|
||||
@@ -200,7 +198,7 @@ class Container extends BaseObject implements ContainerInterface
|
||||
*/
|
||||
public function propertyInject(ReflectionClass $reflect, $object): mixed
|
||||
{
|
||||
foreach ($this->getPropertyNote($reflect) as $property => $inject) {
|
||||
foreach (NoteManager::getPropertyNote($reflect) as $property => $inject) {
|
||||
/** @var Inject $inject */
|
||||
$inject->execute($object, $property);
|
||||
}
|
||||
@@ -216,7 +214,7 @@ class Container extends BaseObject implements ContainerInterface
|
||||
*/
|
||||
public function getMethodAttribute($className, $method = null): array
|
||||
{
|
||||
$methods = $this->getMethodNote($this->getReflect($className));
|
||||
$methods = NoteManager::getMethodNote($this->getReflect($className));
|
||||
if (!empty($method)) {
|
||||
return $methods[$method] ?? [];
|
||||
}
|
||||
@@ -232,7 +230,7 @@ class Container extends BaseObject implements ContainerInterface
|
||||
*/
|
||||
public function getClassReflectionProperty(string $class, string $property = null): ReflectionProperty|null|array
|
||||
{
|
||||
$lists = $this->getProperty($this->getReflect($class));
|
||||
$lists = NoteManager::getProperty($this->getReflect($class));
|
||||
if (empty($lists)) {
|
||||
return null;
|
||||
}
|
||||
@@ -272,9 +270,9 @@ class Container extends BaseObject implements ContainerInterface
|
||||
if ($reflect->isAbstract() || $reflect->isTrait() || $reflect->isInterface()) {
|
||||
return $this->_reflection[$class] = $reflect;
|
||||
}
|
||||
$this->setPropertyNote($reflect);
|
||||
$this->setTargetNote($reflect);
|
||||
$this->setMethodNote($reflect);
|
||||
NoteManager::setPropertyNote($reflect);
|
||||
NoteManager::setTargetNote($reflect);
|
||||
NoteManager::setMethodNote($reflect);
|
||||
$construct = $reflect->getConstructor();
|
||||
if (!empty($construct) && $construct->getNumberOfParameters() > 0) {
|
||||
$this->_constructs[$class] = $construct;
|
||||
@@ -293,7 +291,7 @@ class Container extends BaseObject implements ContainerInterface
|
||||
if (is_string($class)) {
|
||||
$class = $this->getReflect($class);
|
||||
}
|
||||
return $this->getMethods($class);
|
||||
return NoteManager::getMethods($class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,272 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Di;
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use ReflectionAttribute;
|
||||
use ReflectionClass;
|
||||
use ReflectionProperty;
|
||||
|
||||
class NoteManager
|
||||
{
|
||||
|
||||
|
||||
private static array $_classTarget = [];
|
||||
private static array $_classMethodNote = [];
|
||||
private static array $_classMethod = [];
|
||||
private static array $_classPropertyNote = [];
|
||||
private static array $_classProperty = [];
|
||||
private static array $_mapping = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $class
|
||||
*/
|
||||
public static function setTargetNote(ReflectionClass $class)
|
||||
{
|
||||
$className = $class->getName();
|
||||
if (!isset(static::$_classTarget[$className])) {
|
||||
static::$_classTarget[$className] = [];
|
||||
}
|
||||
foreach ($class->getAttributes() as $attribute) {
|
||||
if (!class_exists($attribute->getName())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$instance = $attribute->newInstance();
|
||||
|
||||
static::$_classTarget[$className][] = $instance;
|
||||
|
||||
self::setMappingClass($attribute, $className);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionAttribute $attribute
|
||||
* @param string $class
|
||||
*/
|
||||
public static function setMappingClass(ReflectionAttribute $attribute, string $class)
|
||||
{
|
||||
if (!isset(static::$_mapping[$attribute->getName()])) {
|
||||
static::$_mapping[$attribute->getName()] = [];
|
||||
}
|
||||
if (!isset(static::$_mapping[$attribute->getName()][$class])) {
|
||||
static::$_mapping[$attribute->getName()][$class] = [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionAttribute $attribute
|
||||
* @param string $class
|
||||
* @param string $method
|
||||
* @param mixed $instance
|
||||
*/
|
||||
public static function setMappingMethod(ReflectionAttribute $attribute, string $class, string $method, mixed $instance)
|
||||
{
|
||||
self::setMappingClass($attribute, $class);
|
||||
|
||||
if (!isset(static::$_mapping[$attribute->getName()][$class]['method'])) {
|
||||
static::$_mapping[$attribute->getName()][$class]['method'] = [];
|
||||
}
|
||||
static::$_mapping[$attribute->getName()][$class]['method'][] = [$method => $instance];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionAttribute $attribute
|
||||
* @param string $class
|
||||
* @param string $property
|
||||
* @param $instance
|
||||
*/
|
||||
public static function setMappingProperty(ReflectionAttribute $attribute, string $class, string $property, $instance)
|
||||
{
|
||||
self::setMappingClass($attribute, $class);
|
||||
|
||||
$mapping = static::$_mapping[$attribute->getName()][$class];
|
||||
if (!isset($mapping['property'])) {
|
||||
$mapping['property'] = [];
|
||||
}
|
||||
$mapping['property'][] = [$property => $instance];
|
||||
static::$_mapping[$attribute->getName()][$class] = $mapping;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $class
|
||||
* @return array
|
||||
*/
|
||||
public static function getTargetNote(mixed $class): array
|
||||
{
|
||||
if (!is_string($class)) {
|
||||
$class = $class::class;
|
||||
}
|
||||
return static::$_classTarget[$class] ?? [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $class
|
||||
*/
|
||||
public static function setMethodNote(ReflectionClass $class)
|
||||
{
|
||||
$className = $class->getName();
|
||||
static::$_classMethodNote[$className] = static::$_classMethod[$className] = [];
|
||||
foreach ($class->getMethods() as $ReflectionMethod) {
|
||||
static::$_classMethod[$className][$ReflectionMethod->getName()] = $ReflectionMethod;
|
||||
static::$_classMethodNote[$className][$ReflectionMethod->getName()] = [];
|
||||
foreach ($ReflectionMethod->getAttributes() as $attribute) {
|
||||
if (!class_exists($attribute->getName())) {
|
||||
continue;
|
||||
}
|
||||
$instance = $attribute->newInstance();
|
||||
|
||||
static::$_classMethodNote[$className][$ReflectionMethod->getName()][] = $instance;
|
||||
|
||||
self::setMappingMethod($attribute, $className, $ReflectionMethod->getName(), $instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $method
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasMethod(string $class, string $method): bool
|
||||
{
|
||||
return isset(static::$_classMethod[$class]) && isset(static::$_classMethod[$class][$method]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $class
|
||||
* @return array
|
||||
*/
|
||||
#[Pure] public static function getMethodNote(ReflectionClass $class): array
|
||||
{
|
||||
return static::$_classMethodNote[$class->getName()] ?? [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $class
|
||||
*/
|
||||
public static function setPropertyNote(ReflectionClass $class)
|
||||
{
|
||||
$className = $class->getName();
|
||||
static::$_classProperty[$className] = static::$_classPropertyNote[$className] = [];
|
||||
foreach ($class->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PUBLIC |
|
||||
ReflectionProperty::IS_PROTECTED) as $ReflectionMethod) {
|
||||
static::$_classProperty[$className][$ReflectionMethod->getName()] = $ReflectionMethod;
|
||||
foreach ($ReflectionMethod->getAttributes() as $attribute) {
|
||||
if (!class_exists($attribute->getName())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$instance = $attribute->newInstance();
|
||||
|
||||
static::$_classPropertyNote[$className][$ReflectionMethod->getName()] = $instance;
|
||||
|
||||
self::setMappingProperty($attribute, $className, $ReflectionMethod->getName(), $instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $attribute
|
||||
* @param string|null $class
|
||||
* @return array[]
|
||||
*/
|
||||
public static function getAttributeTrees(string $attribute, string $class = null): array
|
||||
{
|
||||
$mapping = static::$_mapping[$attribute] ?? [];
|
||||
if (empty($mapping) || empty($class)) {
|
||||
return $mapping;
|
||||
}
|
||||
return $mapping[$class] ?? [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $attribute
|
||||
* @param string $class
|
||||
* @param string|null $method
|
||||
* @return array
|
||||
*/
|
||||
public static function getMethodByAnnotation(string $attribute, string $class, string $method = null): mixed
|
||||
{
|
||||
$class = self::getAttributeTrees($attribute, $class);
|
||||
if (empty($class) || !isset($class['method']) || empty($method)) {
|
||||
return $class['method'] ?? [];
|
||||
}
|
||||
foreach ($class['method'] as $value) {
|
||||
$key = key($value);
|
||||
if ($method == $key) {
|
||||
return $value[$key];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $attribute
|
||||
* @param string $class
|
||||
* @param string $method
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getPropertyByAnnotation(string $attribute, string $class, string $method): mixed
|
||||
{
|
||||
$class = self::getAttributeTrees($attribute, $class);
|
||||
if (empty($class) || !isset($class['property'])) {
|
||||
return [];
|
||||
}
|
||||
foreach ($class['property'] as $value) {
|
||||
$key = key($value);
|
||||
if ($method == $key) {
|
||||
return $value[$key];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass|string $class
|
||||
* @return array
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public static function getMethods(ReflectionClass|string $class): array
|
||||
{
|
||||
if (is_string($class)) {
|
||||
$class = self::getReflect($class);
|
||||
}
|
||||
return static::$_classMethod[$class->getName()] ?? [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $class
|
||||
* @return ReflectionProperty[]
|
||||
*/
|
||||
#[Pure] public static function getProperty(ReflectionClass $class): array
|
||||
{
|
||||
return static::$_classProperty[$class->getName()] ?? [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $class
|
||||
* @return array
|
||||
*/
|
||||
#[Pure] public static function getPropertyNote(ReflectionClass $class): array
|
||||
{
|
||||
return static::$_classPropertyNote[$class->getName()] ?? [];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+797
-796
File diff suppressed because it is too large
Load Diff
@@ -89,7 +89,7 @@ class ApplicationStore
|
||||
*/
|
||||
#[Pure] public function getStatus(): string
|
||||
{
|
||||
return env('state');
|
||||
return env('state','start');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+415
-405
@@ -27,466 +27,476 @@ use Swoole\WebSocket\Server as WServer;
|
||||
class ServerManager
|
||||
{
|
||||
|
||||
/** @var string */
|
||||
public string $host = '';
|
||||
/** @var string */
|
||||
public string $host = '';
|
||||
|
||||
public int $port = 0;
|
||||
public int $port = 0;
|
||||
|
||||
|
||||
/** @var array<string,Port> */
|
||||
public array $ports = [];
|
||||
/** @var array<string,Port> */
|
||||
public array $ports = [];
|
||||
|
||||
public int $mode = SWOOLE_TCP;
|
||||
public int $mode = SWOOLE_TCP;
|
||||
|
||||
|
||||
private Server|null $server = null;
|
||||
private Server|null $server = null;
|
||||
|
||||
|
||||
/**
|
||||
* @return Server|WServer|HServer|null
|
||||
*/
|
||||
public function getServer(): Server|WServer|HServer|null
|
||||
{
|
||||
return $this->server;
|
||||
}
|
||||
/**
|
||||
* @return Server|WServer|HServer|null
|
||||
*/
|
||||
public function getServer(): Server|WServer|HServer|null
|
||||
{
|
||||
return $this->server;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string $host
|
||||
* @param int $port
|
||||
* @param int $mode
|
||||
* @param array $settings
|
||||
* @throws ReflectionException
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function addListener(string $type, string $host, int $port, int $mode, array $settings = [])
|
||||
{
|
||||
if ($this->checkPortIsAlready($port)) $this->stopServer($port);
|
||||
if (!$this->server) {
|
||||
$this->createBaseServer($type, $host, $port, $mode, $settings);
|
||||
} else {
|
||||
if (!isset($settings['settings'])) {
|
||||
$settings['settings'] = [];
|
||||
}
|
||||
$this->addNewListener($type, $host, $port, $mode, $settings);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string $host
|
||||
* @param int $port
|
||||
* @param int $mode
|
||||
* @param array $settings
|
||||
* @throws ReflectionException
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function addListener(string $type, string $host, int $port, int $mode, array $settings = [])
|
||||
{
|
||||
if ($this->checkPortIsAlready($port)) $this->stopServer($port);
|
||||
if (!$this->server) {
|
||||
$this->createBaseServer($type, $host, $port, $mode, $settings);
|
||||
} else {
|
||||
if (!isset($settings['settings'])) {
|
||||
$settings['settings'] = [];
|
||||
}
|
||||
$this->addNewListener($type, $host, $port, $mode, $settings);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function initBaseServer($configs, int $daemon = 0): void
|
||||
{
|
||||
$context = di(ServerManager::class);
|
||||
foreach ($this->sortService($configs['ports']) as $config) {
|
||||
$this->startListenerHandler($context, $config, $daemon);
|
||||
}
|
||||
$this->bindCallback($this->server, [Constant::PIPE_MESSAGE => [OnPipeMessage::class, 'onPipeMessage']]);
|
||||
$this->bindCallback($this->server, $this->getSystemEvents($configs));
|
||||
}
|
||||
/**
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function initBaseServer($configs, int $daemon = 0): void
|
||||
{
|
||||
$context = di(ServerManager::class);
|
||||
foreach ($this->sortService($configs['ports']) as $config) {
|
||||
$this->startListenerHandler($context, $config, $daemon);
|
||||
}
|
||||
$this->bindCallback($this->server, [Constant::PIPE_MESSAGE => [OnPipeMessage::class, 'onPipeMessage']]);
|
||||
$this->bindCallback($this->server, $this->getSystemEvents($configs));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function isRunner(): bool
|
||||
{
|
||||
$configs = Config::get('server', [], true);
|
||||
foreach ($this->sortService($configs['ports']) as $config) {
|
||||
if ($this->checkPortIsAlready($config['port'])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* @return bool
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function isRunner(): bool
|
||||
{
|
||||
$configs = Config::get('server', [], true);
|
||||
foreach ($this->sortService($configs['ports']) as $config) {
|
||||
if ($this->checkPortIsAlready($config['port'])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string|CustomProcess $customProcess
|
||||
* @param null $redirect_stdin_and_stdout
|
||||
* @param int|null $pipe_type
|
||||
* @param bool $enable_coroutine
|
||||
* @throws Exception
|
||||
*/
|
||||
public function addProcess(string|CustomProcess $customProcess, $redirect_stdin_and_stdout = null, ?int $pipe_type = SOCK_DGRAM, bool $enable_coroutine = true)
|
||||
{
|
||||
$process = $this->initProcess($customProcess, $redirect_stdin_and_stdout, $pipe_type, $enable_coroutine);
|
||||
$this->server->addProcess($process);
|
||||
if ($customProcess instanceof CustomProcess) {
|
||||
Kiri::app()->addProcess($customProcess::class, $process);
|
||||
} else {
|
||||
Kiri::app()->addProcess($customProcess, $process);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param string|CustomProcess $customProcess
|
||||
* @param null $redirect_stdin_and_stdout
|
||||
* @param int|null $pipe_type
|
||||
* @param bool $enable_coroutine
|
||||
* @throws Exception
|
||||
*/
|
||||
public function addProcess(string|CustomProcess $customProcess, $redirect_stdin_and_stdout = null, ?int $pipe_type = SOCK_DGRAM, bool $enable_coroutine = true)
|
||||
{
|
||||
$process = $this->initProcess($customProcess, $redirect_stdin_and_stdout, $pipe_type, $enable_coroutine);
|
||||
$this->server->addProcess($process);
|
||||
if ($customProcess instanceof CustomProcess) {
|
||||
Kiri::app()->addProcess($customProcess::class, $process);
|
||||
} else {
|
||||
Kiri::app()->addProcess($customProcess, $process);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $customProcess
|
||||
* @param $redirect_stdin_and_stdout
|
||||
* @param $pipe_type
|
||||
* @param $enable_coroutine
|
||||
* @return Process
|
||||
*/
|
||||
private function initProcess($customProcess, $redirect_stdin_and_stdout, $pipe_type, $enable_coroutine): Process
|
||||
{
|
||||
$server = $this->server;
|
||||
return new Process(function (Process $soloProcess) use ($customProcess, $server) {
|
||||
if (is_string($customProcess)) {
|
||||
$customProcess = Kiri::createObject($customProcess, [$server]);
|
||||
}
|
||||
/**
|
||||
* @param $customProcess
|
||||
* @param $redirect_stdin_and_stdout
|
||||
* @param $pipe_type
|
||||
* @param $enable_coroutine
|
||||
* @return Process
|
||||
*/
|
||||
private function initProcess($customProcess, $redirect_stdin_and_stdout, $pipe_type, $enable_coroutine): Process
|
||||
{
|
||||
$server = $this->server;
|
||||
return new Process(function (Process $soloProcess) use ($customProcess, $server) {
|
||||
if (is_string($customProcess)) {
|
||||
$customProcess = Kiri::createObject($customProcess, [$server]);
|
||||
}
|
||||
|
||||
$name = $customProcess->getProcessName($soloProcess);
|
||||
info(sprintf("Process %s start.", $name));
|
||||
$name = $customProcess->getProcessName($soloProcess);
|
||||
info(sprintf("Process %s start.", $name));
|
||||
|
||||
scan_directory(directory('app'), 'App');
|
||||
scan_directory(directory('app'), 'App');
|
||||
|
||||
$system = sprintf('%s.process[%d]', Config::get('id', 'system-service'), $soloProcess->pid);
|
||||
if (Kiri::getPlatform()->isLinux()) {
|
||||
$soloProcess->name($system . '.' . $name . ' start.');
|
||||
}
|
||||
$customProcess->signListen($soloProcess);
|
||||
$customProcess->onHandler($soloProcess);
|
||||
},
|
||||
$redirect_stdin_and_stdout,
|
||||
$pipe_type,
|
||||
$enable_coroutine
|
||||
);
|
||||
}
|
||||
$system = sprintf('%s.process[%d]', Config::get('id', 'system-service'), $soloProcess->pid);
|
||||
if (Kiri::getPlatform()->isLinux()) {
|
||||
$soloProcess->name($system . '.' . $name . ' start.');
|
||||
}
|
||||
$customProcess->signListen($soloProcess);
|
||||
$customProcess->onHandler($soloProcess);
|
||||
},
|
||||
$redirect_stdin_and_stdout,
|
||||
$pipe_type,
|
||||
$enable_coroutine
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getSetting(): array
|
||||
{
|
||||
return $this->server->setting;
|
||||
}
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getSetting(): array
|
||||
{
|
||||
return $this->server->setting;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $ports
|
||||
* @return array
|
||||
*/
|
||||
public function sortService(array $ports): array
|
||||
{
|
||||
$array = [];
|
||||
foreach ($ports as $port) {
|
||||
if ($port['type'] == Constant::SERVER_TYPE_WEBSOCKET) {
|
||||
array_unshift($array, $port);
|
||||
} else if ($port['type'] == Constant::SERVER_TYPE_HTTP) {
|
||||
if (!empty($array) && $array[0]['type'] == Constant::SERVER_TYPE_WEBSOCKET) {
|
||||
$array[] = $port;
|
||||
} else {
|
||||
array_unshift($array, $port);
|
||||
}
|
||||
} else {
|
||||
$array[] = $port;
|
||||
}
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
/**
|
||||
* @param array $ports
|
||||
* @return array
|
||||
*/
|
||||
public function sortService(array $ports): array
|
||||
{
|
||||
$array = [];
|
||||
foreach ($ports as $port) {
|
||||
if ($port['type'] == Constant::SERVER_TYPE_WEBSOCKET) {
|
||||
array_unshift($array, $port);
|
||||
} else if ($port['type'] == Constant::SERVER_TYPE_HTTP) {
|
||||
if (!empty($array) && $array[0]['type'] == Constant::SERVER_TYPE_WEBSOCKET) {
|
||||
$array[] = $port;
|
||||
} else {
|
||||
array_unshift($array, $port);
|
||||
}
|
||||
} else {
|
||||
$array[] = $port;
|
||||
}
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $configs
|
||||
* @return array
|
||||
*/
|
||||
private function getSystemEvents(array $configs): array
|
||||
{
|
||||
return array_intersect_key($configs['events'] ?? [], [
|
||||
Constant::SHUTDOWN => '',
|
||||
Constant::WORKER_START => '',
|
||||
Constant::WORKER_ERROR => '',
|
||||
Constant::WORKER_EXIT => '',
|
||||
Constant::WORKER_STOP => '',
|
||||
Constant::MANAGER_START => '',
|
||||
Constant::MANAGER_STOP => '',
|
||||
Constant::BEFORE_RELOAD => '',
|
||||
Constant::AFTER_RELOAD => '',
|
||||
Constant::START => '',
|
||||
]);
|
||||
}
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
*/
|
||||
public static function setEnv(string $key, string|int $value): void
|
||||
{
|
||||
putenv(sprintf('%s=%s', $key, (string)$value));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ServerManager $context
|
||||
* @param array $config
|
||||
* @param int $daemon
|
||||
* @throws ConfigException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function startListenerHandler(ServerManager $context, array $config, int $daemon = 0)
|
||||
{
|
||||
if (!$this->server) {
|
||||
$config = $this->mergeConfig($config, $daemon);
|
||||
}
|
||||
$context->addListener(
|
||||
$config['type'], $config['host'], $config['port'], $config['mode'],
|
||||
$config);
|
||||
}
|
||||
/**
|
||||
* @param array $configs
|
||||
* @return array
|
||||
*/
|
||||
private function getSystemEvents(array $configs): array
|
||||
{
|
||||
return array_intersect_key($configs['events'] ?? [], [
|
||||
Constant::SHUTDOWN => '',
|
||||
Constant::WORKER_START => '',
|
||||
Constant::WORKER_ERROR => '',
|
||||
Constant::WORKER_EXIT => '',
|
||||
Constant::WORKER_STOP => '',
|
||||
Constant::MANAGER_START => '',
|
||||
Constant::MANAGER_STOP => '',
|
||||
Constant::BEFORE_RELOAD => '',
|
||||
Constant::AFTER_RELOAD => '',
|
||||
Constant::START => '',
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $config
|
||||
* @param $daemon
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
private function mergeConfig($config, $daemon): array
|
||||
{
|
||||
$config['settings'] = $config['settings'] ?? [];
|
||||
if (!isset($config['settings']['daemonize']) || !$config['settings']['daemonize'] != $daemon) {
|
||||
$config['settings']['daemonize'] = $daemon;
|
||||
}
|
||||
if (!isset($config['settings']['log_file'])) {
|
||||
$config['settings']['log_file'] = storage('system.log');
|
||||
}
|
||||
$config['settings']['pid_file'] = storage('.swoole.pid');
|
||||
$config['events'] = $config['events'] ?? [];
|
||||
return $config;
|
||||
}
|
||||
/**
|
||||
* @param ServerManager $context
|
||||
* @param array $config
|
||||
* @param int $daemon
|
||||
* @throws ConfigException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function startListenerHandler(ServerManager $context, array $config, int $daemon = 0)
|
||||
{
|
||||
if (!$this->server) {
|
||||
$config = $this->mergeConfig($config, $daemon);
|
||||
}
|
||||
$context->addListener(
|
||||
$config['type'], $config['host'], $config['port'], $config['mode'],
|
||||
$config);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string $host
|
||||
* @param int $port
|
||||
* @param int $mode
|
||||
* @param array $settings
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function addNewListener(string $type, string $host, int $port, int $mode, array $settings = [])
|
||||
{
|
||||
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m $type service %s::%d start.", $host, $port) . PHP_EOL;
|
||||
/** @var Server\Port $service */
|
||||
$this->ports[$port] = $this->server->addlistener($host, $port, $mode);
|
||||
if ($this->ports[$port] === false) {
|
||||
throw new Exception("The port is already in use[$host::$port]");
|
||||
}
|
||||
$this->ports[$port]->set($settings['settings'] ?? []);
|
||||
$this->addServiceEvents($settings['events'] ?? [], $this->ports[$port]);
|
||||
}
|
||||
/**
|
||||
* @param $config
|
||||
* @param $daemon
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
private function mergeConfig($config, $daemon): array
|
||||
{
|
||||
$config['settings'] = $config['settings'] ?? [];
|
||||
if (!isset($config['settings']['daemonize']) || !$config['settings']['daemonize'] != $daemon) {
|
||||
$config['settings']['daemonize'] = $daemon;
|
||||
}
|
||||
if (!isset($config['settings']['log_file'])) {
|
||||
$config['settings']['log_file'] = storage('system.log');
|
||||
}
|
||||
$config['settings']['pid_file'] = storage('.swoole.pid');
|
||||
$config['events'] = $config['events'] ?? [];
|
||||
return $config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $port
|
||||
* @param string $event
|
||||
* @return Closure|array|null
|
||||
*/
|
||||
public function getPortCallback(int $port, string $event): Closure|array|null
|
||||
{
|
||||
/** @var Server\Port $_port */
|
||||
$_port = $this->ports[$port] ?? null;
|
||||
if (is_null($_port)) {
|
||||
return null;
|
||||
}
|
||||
return $_port->getCallback($event);
|
||||
}
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string $host
|
||||
* @param int $port
|
||||
* @param int $mode
|
||||
* @param array $settings
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function addNewListener(string $type, string $host, int $port, int $mode, array $settings = [])
|
||||
{
|
||||
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m $type service %s::%d start.", $host, $port) . PHP_EOL;
|
||||
/** @var Server\Port $service */
|
||||
$this->ports[$port] = $this->server->addlistener($host, $port, $mode);
|
||||
if ($this->ports[$port] === false) {
|
||||
throw new Exception("The port is already in use[$host::$port]");
|
||||
}
|
||||
$this->ports[$port]->set($settings['settings'] ?? []);
|
||||
$this->addServiceEvents($settings['events'] ?? [], $this->ports[$port]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string $host
|
||||
* @param int $port
|
||||
* @param int $mode
|
||||
* @param array $settings
|
||||
* @throws ReflectionException
|
||||
* @throws ConfigException
|
||||
*/
|
||||
private function createBaseServer(string $type, string $host, int $port, int $mode, array $settings = [])
|
||||
{
|
||||
$match = match ($type) {
|
||||
Constant::SERVER_TYPE_BASE, Constant::SERVER_TYPE_TCP,
|
||||
Constant::SERVER_TYPE_UDP => Server::class,
|
||||
Constant::SERVER_TYPE_HTTP => HServer::class,
|
||||
Constant::SERVER_TYPE_WEBSOCKET => WServer::class
|
||||
};
|
||||
$this->server = new $match($host, $port, SWOOLE_PROCESS, $mode);
|
||||
$this->server->set(array_merge(Config::get('server.settings', []), $settings['settings']));
|
||||
|
||||
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m $type service %s::%d start.", $host, $port) . PHP_EOL;
|
||||
|
||||
$this->addDefaultListener($type, $settings);
|
||||
}
|
||||
/**
|
||||
* @param int $port
|
||||
* @param string $event
|
||||
* @return Closure|array|null
|
||||
*/
|
||||
public function getPortCallback(int $port, string $event): Closure|array|null
|
||||
{
|
||||
/** @var Server\Port $_port */
|
||||
$_port = $this->ports[$port] ?? null;
|
||||
if (is_null($_port)) {
|
||||
return null;
|
||||
}
|
||||
return $_port->getCallback($event);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $port
|
||||
* @throws Exception
|
||||
*/
|
||||
public function stopServer(int $port)
|
||||
{
|
||||
if (!($pid = $this->checkPortIsAlready($port))) {
|
||||
return;
|
||||
}
|
||||
while ($this->checkPortIsAlready($port)) {
|
||||
Process::kill($pid, SIGTERM);
|
||||
usleep(300);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string $host
|
||||
* @param int $port
|
||||
* @param int $mode
|
||||
* @param array $settings
|
||||
* @throws ReflectionException
|
||||
* @throws ConfigException
|
||||
*/
|
||||
private function createBaseServer(string $type, string $host, int $port, int $mode, array $settings = [])
|
||||
{
|
||||
$match = match ($type) {
|
||||
Constant::SERVER_TYPE_BASE, Constant::SERVER_TYPE_TCP,
|
||||
Constant::SERVER_TYPE_UDP => Server::class,
|
||||
Constant::SERVER_TYPE_HTTP => HServer::class,
|
||||
Constant::SERVER_TYPE_WEBSOCKET => WServer::class
|
||||
};
|
||||
$this->server = new $match($host, $port, SWOOLE_PROCESS, $mode);
|
||||
$this->server->set(array_merge(Config::get('server.settings', []), $settings['settings']));
|
||||
|
||||
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m $type service %s::%d start.", $host, $port) . PHP_EOL;
|
||||
|
||||
$this->addDefaultListener($type, $settings);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $port
|
||||
* @return bool|string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function checkPortIsAlready($port): bool|string
|
||||
{
|
||||
if (!Kiri::getPlatform()->isLinux()) {
|
||||
exec("lsof -i :" . $port . " | grep -i 'LISTEN' | awk '{print $2}'", $output);
|
||||
if (empty($output)) return false;
|
||||
$output = explode(PHP_EOL, $output[0]);
|
||||
return $output[0];
|
||||
}
|
||||
|
||||
$serverPid = file_get_contents(storage('.swoole.pid'));
|
||||
if (!empty($serverPid) && shell_exec('ps -ef | grep ' . $serverPid . ' | grep -v grep')) {
|
||||
Process::kill($serverPid, SIGTERM);
|
||||
}
|
||||
|
||||
exec('netstat -lnp | grep ' . $port . ' | grep "LISTEN" | awk \'{print $7}\'', $output);
|
||||
if (empty($output)) {
|
||||
return false;
|
||||
}
|
||||
return explode('/', $output[0])[0];
|
||||
}
|
||||
/**
|
||||
* @param int $port
|
||||
* @throws Exception
|
||||
*/
|
||||
public function stopServer(int $port)
|
||||
{
|
||||
if (!($pid = $this->checkPortIsAlready($port))) {
|
||||
return;
|
||||
}
|
||||
while ($this->checkPortIsAlready($port)) {
|
||||
Process::kill($pid, SIGTERM);
|
||||
usleep(300);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param array $settings
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function addDefaultListener(string $type, array $settings): void
|
||||
{
|
||||
if (($this->server->setting['task_worker_num'] ?? 0) > 0) {
|
||||
$this->addTaskListener($settings['events']);
|
||||
}
|
||||
$this->addServiceEvents($settings['events'] ?? [], $this->server);
|
||||
Kiri::getDi()->setBindings(SwooleServerInterface::class,
|
||||
$this->server);
|
||||
}
|
||||
/**
|
||||
* @param $port
|
||||
* @return bool|string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function checkPortIsAlready($port): bool|string
|
||||
{
|
||||
if (!Kiri::getPlatform()->isLinux()) {
|
||||
exec("lsof -i :" . $port . " | grep -i 'LISTEN' | awk '{print $2}'", $output);
|
||||
if (empty($output)) return false;
|
||||
$output = explode(PHP_EOL, $output[0]);
|
||||
return $output[0];
|
||||
}
|
||||
|
||||
$serverPid = file_get_contents(storage('.swoole.pid'));
|
||||
if (!empty($serverPid) && shell_exec('ps -ef | grep ' . $serverPid . ' | grep -v grep')) {
|
||||
Process::kill($serverPid, SIGTERM);
|
||||
}
|
||||
|
||||
exec('netstat -lnp | grep ' . $port . ' | grep "LISTEN" | awk \'{print $7}\'', $output);
|
||||
if (empty($output)) {
|
||||
return false;
|
||||
}
|
||||
return explode('/', $output[0])[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $events
|
||||
* @param Server|Port $server
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function addServiceEvents(array $events, Server|Port $server)
|
||||
{
|
||||
foreach ($events as $name => $event) {
|
||||
if (is_array($event) && is_string($event[0])) {
|
||||
$event[0] = Kiri::getDi()->get($event[0], [$server]);
|
||||
}
|
||||
$server->on($name, $event);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param string $type
|
||||
* @param array $settings
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function addDefaultListener(string $type, array $settings): void
|
||||
{
|
||||
if (($this->server->setting['task_worker_num'] ?? 0) > 0) {
|
||||
$this->addTaskListener($settings['events']);
|
||||
}
|
||||
$this->addServiceEvents($settings['events'] ?? [], $this->server);
|
||||
Kiri::getDi()->setBindings(SwooleServerInterface::class,
|
||||
$this->server);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
$this->server->start();
|
||||
}
|
||||
/**
|
||||
* @param array $events
|
||||
* @param Server|Port $server
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function addServiceEvents(array $events, Server|Port $server)
|
||||
{
|
||||
foreach ($events as $name => $event) {
|
||||
if (is_array($event) && is_string($event[0])) {
|
||||
$event[0] = Kiri::getDi()->get($event[0], [$server]);
|
||||
}
|
||||
$server->on($name, $event);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @return object
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function getNewInstance(string $class): object
|
||||
{
|
||||
return Kiri::getDi()->newObject($class);
|
||||
}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
$this->server->start();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param TaskExecute|string $handler
|
||||
* @param array $params
|
||||
* @param int|null $workerId
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function task(TaskExecute|string $handler, array $params = [], int $workerId = null)
|
||||
{
|
||||
if ($workerId === null || $workerId <= $this->server->setting['worker_num']) {
|
||||
$workerId = random_int($this->server->setting['worker_num'] + 1,
|
||||
$this->server->setting['worker_num'] + 1 + $this->server->setting['task_worker_num']);
|
||||
}
|
||||
if (is_string($handler)) {
|
||||
$implements = Kiri::getDi()->getReflect($handler);
|
||||
if (!in_array(TaskExecute::class, $implements->getInterfaceNames())) {
|
||||
throw new Exception('Task must instance ' . TaskExecute::class);
|
||||
}
|
||||
$handler = $implements->newInstanceArgs($params);
|
||||
}
|
||||
$this->server->task(serialize($handler), $workerId);
|
||||
}
|
||||
/**
|
||||
* @param string $class
|
||||
* @return object
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function getNewInstance(string $class): object
|
||||
{
|
||||
return Kiri::getDi()->newObject($class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $message
|
||||
* @param int $workerId
|
||||
* @return mixed
|
||||
*/
|
||||
public function sendMessage(mixed $message, int $workerId): mixed
|
||||
{
|
||||
return $this->server?->sendMessage($message, $workerId);
|
||||
}
|
||||
/**
|
||||
* @param TaskExecute|string $handler
|
||||
* @param array $params
|
||||
* @param int|null $workerId
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function task(TaskExecute|string $handler, array $params = [], int $workerId = null)
|
||||
{
|
||||
if ($workerId === null || $workerId <= $this->server->setting['worker_num']) {
|
||||
$workerId = random_int($this->server->setting['worker_num'] + 1,
|
||||
$this->server->setting['worker_num'] + 1 + $this->server->setting['task_worker_num']);
|
||||
}
|
||||
if (is_string($handler)) {
|
||||
$implements = Kiri::getDi()->getReflect($handler);
|
||||
if (!in_array(TaskExecute::class, $implements->getInterfaceNames())) {
|
||||
throw new Exception('Task must instance ' . TaskExecute::class);
|
||||
}
|
||||
$handler = $implements->newInstanceArgs($params);
|
||||
}
|
||||
$this->server->task(serialize($handler), $workerId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $events
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function addTaskListener(array $events = []): void
|
||||
{
|
||||
$task_use_object = $this->server->setting['task_object'] ?? $this->server->setting['task_use_object'] ?? false;
|
||||
$reflect = Kiri::getDi()->getReflect(OnServerTask::class)?->newInstance();
|
||||
if ($task_use_object || $this->server->setting['task_enable_coroutine']) {
|
||||
$this->server->on('task', $events[Constant::TASK] ?? [$reflect, 'onCoroutineTask']);
|
||||
} else {
|
||||
$this->server->on('task', $events[Constant::TASK] ?? [$reflect, 'onTask']);
|
||||
}
|
||||
$this->server->on('finish', $events[Constant::FINISH] ?? [$reflect, 'onFinish']);
|
||||
}
|
||||
/**
|
||||
* @param mixed $message
|
||||
* @param int $workerId
|
||||
* @return mixed
|
||||
*/
|
||||
public function sendMessage(mixed $message, int $workerId): mixed
|
||||
{
|
||||
return $this->server?->sendMessage($message, $workerId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Port|Server $server
|
||||
* @param array|null $settings
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function bindCallback(Port|Server $server, ?array $settings = [])
|
||||
{
|
||||
// TODO: Implement bindCallback() method.
|
||||
if (count($settings) < 1) {
|
||||
return;
|
||||
}
|
||||
foreach ($settings as $event_type => $callback) {
|
||||
if ($this->server->getCallback($event_type) !== null) {
|
||||
continue;
|
||||
}
|
||||
if (is_array($callback) && !is_object($callback[0])) {
|
||||
$callback[0] = Kiri::getDi()->get($callback[0]);
|
||||
}
|
||||
$this->server->on($event_type, $callback);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param array $events
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function addTaskListener(array $events = []): void
|
||||
{
|
||||
$task_use_object = $this->server->setting['task_object'] ?? $this->server->setting['task_use_object'] ?? false;
|
||||
$reflect = Kiri::getDi()->getReflect(OnServerTask::class)?->newInstance();
|
||||
if ($task_use_object || $this->server->setting['task_enable_coroutine']) {
|
||||
$this->server->on('task', $events[Constant::TASK] ?? [$reflect, 'onCoroutineTask']);
|
||||
} else {
|
||||
$this->server->on('task', $events[Constant::TASK] ?? [$reflect, 'onTask']);
|
||||
}
|
||||
$this->server->on('finish', $events[Constant::FINISH] ?? [$reflect, 'onFinish']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Port|Server $server
|
||||
* @param array|null $settings
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function bindCallback(Port|Server $server, ?array $settings = [])
|
||||
{
|
||||
// TODO: Implement bindCallback() method.
|
||||
if (count($settings) < 1) {
|
||||
return;
|
||||
}
|
||||
foreach ($settings as $event_type => $callback) {
|
||||
if ($this->server->getCallback($event_type) !== null) {
|
||||
continue;
|
||||
}
|
||||
if (is_array($callback) && !is_object($callback[0])) {
|
||||
$callback[0] = Kiri::getDi()->get($callback[0]);
|
||||
}
|
||||
$this->server->on($event_type, $callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ use Server\Events\OnWorkerError;
|
||||
use Server\Events\OnWorkerExit;
|
||||
use Server\Events\OnWorkerStart;
|
||||
use Server\Events\OnWorkerStop;
|
||||
use Server\ServerManager;
|
||||
use Swoole\Server;
|
||||
use Swoole\Timer;
|
||||
|
||||
@@ -54,8 +55,7 @@ class OnServerWorker extends \Server\Abstracts\Server
|
||||
*/
|
||||
public function setConfigure(OnBeforeWorkerStart $worker)
|
||||
{
|
||||
putenv('state=start');
|
||||
putenv('worker=' . $worker->workerId);
|
||||
ServerManager::setEnv('worker', $worker->workerId);
|
||||
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
|
||||
if (!empty($serialize)) {
|
||||
Config::sets(unserialize($serialize));
|
||||
@@ -83,7 +83,7 @@ class OnServerWorker extends \Server\Abstracts\Server
|
||||
*/
|
||||
public function onWorkerExit(Server $server, int $workerId)
|
||||
{
|
||||
putenv('state=exit');
|
||||
ServerManager::setEnv('state', 'exit');
|
||||
|
||||
$this->eventDispatch->dispatch(new OnWorkerExit($server, $workerId));
|
||||
}
|
||||
@@ -104,7 +104,8 @@ class OnServerWorker extends \Server\Abstracts\Server
|
||||
$message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s',
|
||||
$worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), 9)
|
||||
);
|
||||
write($message, 'worker-exit');
|
||||
|
||||
$this->logger->error($message);
|
||||
|
||||
$this->system_mail($message);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ use Kiri\Kiri;
|
||||
use Kiri\Runtime;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use ReflectionException;
|
||||
use Server\ServerManager;
|
||||
|
||||
class OnWorkerStart implements EventDispatcherInterface
|
||||
{
|
||||
@@ -35,17 +36,17 @@ class OnWorkerStart implements EventDispatcherInterface
|
||||
{
|
||||
$isWorker = $event->workerId < $event->server->setting['worker_num'];
|
||||
|
||||
$this->annotation->read(APP_PATH . 'app','App', $isWorker ? [] : [CONTROLLER_PATH]);
|
||||
$this->annotation->read(APP_PATH . 'app', 'App', $isWorker ? [] : [CONTROLLER_PATH]);
|
||||
$this->interpretDirectory();
|
||||
if ($isWorker) {
|
||||
putenv('environmental=' . Kiri::WORKER);
|
||||
ServerManager::setEnv('environmental', Kiri::WORKER);
|
||||
Kiri::getFactory()->getRouter()->_loader();
|
||||
|
||||
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Worker[%d].%d start.", $event->server->worker_pid, $event->workerId) . PHP_EOL;
|
||||
|
||||
$this->setProcessName(sprintf('Worker[%d].%d', $event->server->worker_pid, $event->workerId));
|
||||
} else {
|
||||
putenv('environmental=' . Kiri::TASK);
|
||||
ServerManager::setEnv('environmental', Kiri::TASK);
|
||||
|
||||
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Tasker[%d].%d start.", $event->server->worker_pid, $event->workerId) . PHP_EOL;
|
||||
|
||||
|
||||
@@ -31,23 +31,4 @@ use Kiri\Kiri;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param static $params
|
||||
* @param mixed $class
|
||||
* @param mixed|null $method
|
||||
* @return Router
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function execute(mixed $params, mixed $class, mixed $method = null): Router
|
||||
{
|
||||
// TODO: Implement setHandler() method.
|
||||
$router = Kiri::app()->getRouter();
|
||||
|
||||
$path = $params->event . '::' . (is_null($params->uri) ? 'event' : $params->uri);
|
||||
|
||||
$router->addRoute($path, [di($class), $method], 'sw::socket');
|
||||
|
||||
return $router;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user