This commit is contained in:
as2252258@163.com
2021-09-04 00:08:34 +08:00
parent 991095ae50
commit abd043ee15
10 changed files with 1503 additions and 1511 deletions
+2 -2
View File
@@ -66,13 +66,13 @@ class Redis implements StopHeartbeatCheck
*/ */
public function heartbeat_check(): void public function heartbeat_check(): void
{ {
if (env('state') == 'exit') { if (env('state','start') == 'exit') {
return; return;
} }
if ($this->_timer === -1 && Context::inCoroutine()) { if ($this->_timer === -1 && Context::inCoroutine()) {
$this->_timer = Timer::tick(1000, function () { $this->_timer = Timer::tick(1000, function () {
try { try {
if (env('state') == 'exit') { if (env('state','start') == 'exit') {
echo 'timer end.' . PHP_EOL; echo 'timer end.' . PHP_EOL;
} }
if (time() - $this->_last > 10 * 60) { if (time() - $this->_last > 10 * 60) {
-272
View File
@@ -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()] ?? [];
}
}
+7 -9
View File
@@ -34,8 +34,6 @@ use Server\ResponseInterface;
class Container extends BaseObject implements ContainerInterface class Container extends BaseObject implements ContainerInterface
{ {
use Attributes;
/** /**
* @var array * @var array
* *
@@ -200,7 +198,7 @@ class Container extends BaseObject implements ContainerInterface
*/ */
public function propertyInject(ReflectionClass $reflect, $object): mixed public function propertyInject(ReflectionClass $reflect, $object): mixed
{ {
foreach ($this->getPropertyNote($reflect) as $property => $inject) { foreach (NoteManager::getPropertyNote($reflect) as $property => $inject) {
/** @var Inject $inject */ /** @var Inject $inject */
$inject->execute($object, $property); $inject->execute($object, $property);
} }
@@ -216,7 +214,7 @@ class Container extends BaseObject implements ContainerInterface
*/ */
public function getMethodAttribute($className, $method = null): array public function getMethodAttribute($className, $method = null): array
{ {
$methods = $this->getMethodNote($this->getReflect($className)); $methods = NoteManager::getMethodNote($this->getReflect($className));
if (!empty($method)) { if (!empty($method)) {
return $methods[$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 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)) { if (empty($lists)) {
return null; return null;
} }
@@ -272,9 +270,9 @@ class Container extends BaseObject implements ContainerInterface
if ($reflect->isAbstract() || $reflect->isTrait() || $reflect->isInterface()) { if ($reflect->isAbstract() || $reflect->isTrait() || $reflect->isInterface()) {
return $this->_reflection[$class] = $reflect; return $this->_reflection[$class] = $reflect;
} }
$this->setPropertyNote($reflect); NoteManager::setPropertyNote($reflect);
$this->setTargetNote($reflect); NoteManager::setTargetNote($reflect);
$this->setMethodNote($reflect); NoteManager::setMethodNote($reflect);
$construct = $reflect->getConstructor(); $construct = $reflect->getConstructor();
if (!empty($construct) && $construct->getNumberOfParameters() > 0) { if (!empty($construct) && $construct->getNumberOfParameters() > 0) {
$this->_constructs[$class] = $construct; $this->_constructs[$class] = $construct;
@@ -293,7 +291,7 @@ class Container extends BaseObject implements ContainerInterface
if (is_string($class)) { if (is_string($class)) {
$class = $this->getReflect($class); $class = $this->getReflect($class);
} }
return $this->getMethods($class); return NoteManager::getMethods($class);
} }
+272
View File
@@ -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()] ?? [];
}
}
+2 -1
View File
@@ -21,6 +21,7 @@ use Kiri\Kiri;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Server\Constrict\Request; use Server\Constrict\Request;
use Server\Constrict\Response; use Server\Constrict\Response;
use Server\ServerManager;
use Swoole\WebSocket\Server; use Swoole\WebSocket\Server;
if (!function_exists('make')) { if (!function_exists('make')) {
@@ -62,7 +63,7 @@ if (!function_exists('done')) {
*/ */
function done() function done()
{ {
putenv('state=exit'); ServerManager::setEnv('state', 'exit');
} }
} }
+1 -1
View File
@@ -89,7 +89,7 @@ class ApplicationStore
*/ */
#[Pure] public function getStatus(): string #[Pure] public function getStatus(): string
{ {
return env('state'); return env('state','start');
} }
} }
+10
View File
@@ -194,6 +194,16 @@ class ServerManager
} }
/**
* @param $key
* @param $value
*/
public static function setEnv(string $key, string|int $value): void
{
putenv(sprintf('%s=%s', $key, (string)$value));
}
/** /**
* @param array $configs * @param array $configs
* @return array * @return array
+5 -4
View File
@@ -14,6 +14,7 @@ use Server\Events\OnWorkerError;
use Server\Events\OnWorkerExit; use Server\Events\OnWorkerExit;
use Server\Events\OnWorkerStart; use Server\Events\OnWorkerStart;
use Server\Events\OnWorkerStop; use Server\Events\OnWorkerStop;
use Server\ServerManager;
use Swoole\Server; use Swoole\Server;
use Swoole\Timer; use Swoole\Timer;
@@ -54,8 +55,7 @@ class OnServerWorker extends \Server\Abstracts\Server
*/ */
public function setConfigure(OnBeforeWorkerStart $worker) public function setConfigure(OnBeforeWorkerStart $worker)
{ {
putenv('state=start'); ServerManager::setEnv('worker', $worker->workerId);
putenv('worker=' . $worker->workerId);
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME)); $serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
if (!empty($serialize)) { if (!empty($serialize)) {
Config::sets(unserialize($serialize)); Config::sets(unserialize($serialize));
@@ -83,7 +83,7 @@ class OnServerWorker extends \Server\Abstracts\Server
*/ */
public function onWorkerExit(Server $server, int $workerId) public function onWorkerExit(Server $server, int $workerId)
{ {
putenv('state=exit'); ServerManager::setEnv('state', 'exit');
$this->eventDispatch->dispatch(new OnWorkerExit($server, $workerId)); $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', $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) $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); $this->system_mail($message);
} }
+3 -2
View File
@@ -12,6 +12,7 @@ use Kiri\Kiri;
use Kiri\Runtime; use Kiri\Runtime;
use Psr\EventDispatcher\EventDispatcherInterface; use Psr\EventDispatcher\EventDispatcherInterface;
use ReflectionException; use ReflectionException;
use Server\ServerManager;
class OnWorkerStart implements EventDispatcherInterface class OnWorkerStart implements EventDispatcherInterface
{ {
@@ -38,14 +39,14 @@ class OnWorkerStart implements EventDispatcherInterface
$this->annotation->read(APP_PATH . 'app', 'App', $isWorker ? [] : [CONTROLLER_PATH]); $this->annotation->read(APP_PATH . 'app', 'App', $isWorker ? [] : [CONTROLLER_PATH]);
$this->interpretDirectory(); $this->interpretDirectory();
if ($isWorker) { if ($isWorker) {
putenv('environmental=' . Kiri::WORKER); ServerManager::setEnv('environmental', Kiri::WORKER);
Kiri::getFactory()->getRouter()->_loader(); 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; 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)); $this->setProcessName(sprintf('Worker[%d].%d', $event->server->worker_pid, $event->workerId));
} else { } 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; 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;
-19
View File
@@ -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;
}
} }