diff --git a/Annotation/Annotation.php b/Annotation/Annotation.php index 8f9895dd..0096de9f 100644 --- a/Annotation/Annotation.php +++ b/Annotation/Annotation.php @@ -169,7 +169,6 @@ class Annotation extends Component /** * @param object $class * @throws ReflectionException - * @throws NotFindClassException */ public function injectProperty(object $class) { @@ -186,19 +185,19 @@ class Annotation extends Component */ public function read(string $path, string $namespace = 'App', string $alias = 'root'): void { - $this->_loader->_scanDir(new DirectoryIterator($path), $namespace); } /** * @param string $dir + * @param array $exclude * @return array * @throws Exception */ - public function runtime(string $dir): array + public function runtime(string $dir, array $exclude = []): array { - return $this->_loader->loadByDirectory($dir); + return $this->_loader->loadByDirectory($dir, $exclude); } diff --git a/Annotation/Kafka.php b/Annotation/Kafka.php index 3df8933a..61cdc392 100644 --- a/Annotation/Kafka.php +++ b/Annotation/Kafka.php @@ -41,7 +41,7 @@ use Snowflake\Snowflake; /** @var KafkaProvider $container */ $container = Snowflake::app()->get('kafka-container'); - $container->addConsumer($this->topic, [$class, 'onHandler']); + $container->addConsumer($this->topic, $class); return true; } diff --git a/Annotation/Loader.php b/Annotation/Loader.php index af64cf51..bab673e6 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -21,16 +21,16 @@ class Loader extends BaseObject { - private static array $_classes = []; + private array $_classes = []; - private static array $_directory = []; + private array $_directory = []; - private static array $_property = []; + private array $_property = []; - private static array $_methods = []; + private array $_methods = []; /** @@ -46,20 +46,12 @@ class Loader extends BaseObject /** * @param string $class * @param string $property - * @return mixed + * @return \ReflectionProperty|array|null * @throws ReflectionException */ - public function getProperty(string $class, string $property = ''): mixed + public function getProperty(string $class, string $property = ''): \ReflectionProperty|array|null { return Snowflake::getDi()->getClassReflectionProperty($class, $property); - - if (!isset(static::$_property[$class])) { - return null; - } - if (!empty($property)) { - return static::$_property[$class][$property] ?? []; - } - return static::$_property[$class]; } @@ -89,10 +81,10 @@ class Loader extends BaseObject */ public function getMethod(string $class, string $method = ''): array { - if (!isset(static::$_methods[$class])) { + if (!isset($this->_methods[$class])) { return []; } - $properties = static::$_methods[$class]; + $properties = $this->_methods[$class]; if (!empty($method) && isset($properties[$method])) { return $properties[$method]; } @@ -114,8 +106,8 @@ class Loader extends BaseObject if ($path->isDir()) { $iterator = new DirectoryIterator($path->getRealPath()); $directory = rtrim($path->getRealPath(), '/'); - if (!isset(static::$_directory[$directory])) { - static::$_directory[$directory] = []; + if (!isset($this->_directory[$directory])) { + $this->_directory[$directory] = []; } $this->_scanDir($iterator, $namespace); } else { @@ -141,8 +133,6 @@ class Loader extends BaseObject return; } $this->appendFileToDirectory($path->getRealPath(), $replace->getName()); - - static::$_classes[] = $replace->getName(); } catch (Throwable $throwable) { $this->error(jTraceEx($throwable), 'throwable'); } @@ -163,20 +153,21 @@ class Loader extends BaseObject /** * @param string $path + * @param array $exclude * @return array * @throws Exception */ - public function loadByDirectory(string $path): array + public function loadByDirectory(string $path, array $exclude = []): array { try { $path = '/' . trim($path, '/'); $paths = []; - foreach (static::$_directory as $key => $_path) { + foreach ($this->_directory as $key => $_path) { $key = '/' . trim($key, '/'); - if (!str_starts_with($key, $path)) { + if (!str_starts_with($key, $path) || $this->inExclude($exclude, $path)) { continue; } - unset(static::$_directory[$key]); + unset($this->_directory[$key]); foreach ($_path as $item) { $paths[] = $item; } @@ -189,6 +180,25 @@ class Loader extends BaseObject } + /** + * @param array $exclude + * @param $path + * @return bool + */ + private function inExclude(array $exclude, $path): bool + { + if (empty($exclude)) { + return false; + } + foreach ($exclude as $value) { + if (str_starts_with($value, $path)) { + return true; + } + } + return false; + } + + /** * @param DirectoryIterator $path * @param string $namespace @@ -218,7 +228,7 @@ class Loader extends BaseObject $array = '/' . trim(implode('/', $array), '/'); - static::$_directory[$array][] = $className; + $this->_directory[$array][] = $className; } diff --git a/HttpServer/Command.php b/HttpServer/Command.php index aa092dde..6ebccc59 100644 --- a/HttpServer/Command.php +++ b/HttpServer/Command.php @@ -35,7 +35,6 @@ class Command extends \Console\Command { $manager = Snowflake::app()->getServer(); $manager->setDaemon($dtl->get('daemon', 0)); - if (!in_array($dtl->get('action'), self::ACTIONS)) { return 'I don\'t know what I want to do.'; } @@ -45,14 +44,12 @@ class Command extends \Console\Command if ($shutdown->isRunning() && $dtl->get('action') == 'start') { return 'Service is running. Please use restart.'; } - $shutdown->shutdown(); if ($dtl->get('action') == 'stop') { return 'shutdown success.'; } $this->generate_runtime_builder(); - return $manager->start(); } diff --git a/Kafka/ConsumerInterface.php b/Kafka/ConsumerInterface.php index e530c7db..fabcac91 100644 --- a/Kafka/ConsumerInterface.php +++ b/Kafka/ConsumerInterface.php @@ -12,11 +12,10 @@ interface ConsumerInterface { - /** - * @param Struct $struct - * @return mixed - */ - public function onHandler(Struct $struct): void; + /** + * @return mixed + */ + public function process(): void; } diff --git a/Kafka/Kafka.php b/Kafka/Kafka.php index 51fe31c3..9149fc79 100644 --- a/Kafka/Kafka.php +++ b/Kafka/Kafka.php @@ -36,7 +36,7 @@ class Kafka implements CustomProcess */ public function __construct(public array $kafkaConfig) { - + scan_directory(); } @@ -124,8 +124,6 @@ class Kafka implements CustomProcess { go(function () use ($topic, $message) { try { - var_dump($topic, $message); - $server = Snowflake::app()->getSwoole(); $setting = $server->setting['worker_num']; @@ -183,10 +181,8 @@ class Kafka implements CustomProcess return [$conf, $topicConf, $kafka]; } catch (Throwable $exception) { logger()->addError($exception, 'throwable'); - return [null, null, null]; } - } diff --git a/Kafka/KafkaProvider.php b/Kafka/KafkaProvider.php index f5bb5da2..b15f0737 100644 --- a/Kafka/KafkaProvider.php +++ b/Kafka/KafkaProvider.php @@ -27,7 +27,7 @@ class KafkaProvider extends BaseObject if (isset($this->_topics[$topic])) { return; } - $this->_topics[$topic] = $handler; + $this->_topics[$topic] = $handler::class; } @@ -40,18 +40,4 @@ class KafkaProvider extends BaseObject return $this->_topics[$topic] ?? null; } - - /** - * @param $topic - * @param Struct $struct - */ - public function process($topic, Struct $struct) - { - $handler = $this->_topics[$topic] ?? null; - if (empty($handler)) { - return; - } - call_user_func($handler, $struct); - } - } diff --git a/Server/Worker/ServerWorker.php b/Server/Worker/ServerWorker.php index 6187880d..133b91ae 100644 --- a/Server/Worker/ServerWorker.php +++ b/Server/Worker/ServerWorker.php @@ -53,19 +53,23 @@ class ServerWorker extends \Server\Abstracts\Server $this->runEvent(Constant::WORKER_START, null, [$server, $workerId]); $this->workerInitExecutor($server, $annotation, $workerId); - $this->interpretDirectory($annotation); + $this->interpretDirectory($server, $annotation, $workerId); } /** + * @param Server $server * @param Annotation $annotation + * @param $workerId * @throws NotFindClassException * @throws ReflectionException * @throws Exception */ - private function interpretDirectory(Annotation $annotation) + private function interpretDirectory(Server $server, Annotation $annotation, $workerId) { - $fileLists = $annotation->runtime(APP_PATH . 'app'); + $fileLists = $annotation->runtime(APP_PATH . 'app', + $workerId < $server->setting['worker_num'] ? [CONTROLLER_PATH] : [] + ); $di = Snowflake::getDi(); foreach ($fileLists as $class) { diff --git a/System/Application.php b/System/Application.php index 7d3524ca..941f5ae2 100644 --- a/System/Application.php +++ b/System/Application.php @@ -140,8 +140,7 @@ class Application extends BaseApplication /** @var Console $manager */ $manager = Snowflake::app()->get('console'); $manager->register(Runtime::class); - $manager->setParameters($argv); - $class = $manager->search(); + $class = $manager->setParameters($argv)->search(); if (!($class instanceof Command)) { scan_directory(directory('app'), 'App'); } diff --git a/function.php b/function.php index dada6e38..409321b1 100644 --- a/function.php +++ b/function.php @@ -7,8 +7,10 @@ use Annotation\Annotation; use HttpServer\Http\Context; use HttpServer\Http\HttpParams; use HttpServer\Http\Request; +use HttpServer\Http\Response as HttpResponse; use HttpServer\Route\Router; use JetBrains\PhpStorm\Pure; +use Server\Constrict\Response; use Snowflake\Abstracts\Config; use Snowflake\Aop; use Snowflake\Application; @@ -19,8 +21,6 @@ use Snowflake\Exception\ConfigException; use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; use Swoole\WebSocket\Server; -use Server\Constrict\Response; -use HttpServer\Http\Response as HttpResponse; if (!function_exists('make')) { @@ -90,13 +90,48 @@ if (!function_exists('scan_directory')) { /** * @param $dir * @param $namespace + * @param array $exclude + * @throws NotFindClassException + * @throws ReflectionException * @throws Exception */ - function scan_directory($dir, $namespace) + function scan_directory($dir, $namespace, array $exclude = []) { $annotation = Snowflake::app()->getAnnotation(); $annotation->read($dir, $namespace); - $annotation->runtime($dir, $namespace); + + injectRuntime($dir, $exclude); + } + +} + + +if (!function_exists('injectRuntime')) { + + + /** + * @param string $path + * @param array $exclude + * @throws NotFindClassException + * @throws ReflectionException + * @throws Exception + */ + function injectRuntime(string $path, array $exclude = []) + { + $fileLists = Snowflake::getAnnotation()->runtime($path, $exclude); + $di = Snowflake::getDi(); + foreach ($fileLists as $class) { + $instance = $di->get($class); + $methods = $di->getMethodAttribute($class); + foreach ($methods as $method => $attribute) { + if (empty($attribute)) { + continue; + } + foreach ($attribute as $item) { + $item->execute($instance, $method); + } + } + } } } @@ -600,9 +635,9 @@ if (!function_exists('response')) { */ function response(): Response { - if (!Context::hasContext(HttpResponse::class)){ - Context::setContext(HttpResponse::class, new HttpResponse()); - } + if (!Context::hasContext(HttpResponse::class)) { + Context::setContext(HttpResponse::class, new HttpResponse()); + } return di(Response::class); }