This commit is contained in:
2021-08-05 15:55:27 +08:00
parent a9e172b975
commit 6b9ef31df8
10 changed files with 95 additions and 70 deletions
+3 -4
View File
@@ -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);
}
+1 -1
View File
@@ -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;
}
+35 -25
View File
@@ -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;
}
-3
View File
@@ -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();
}
+4 -5
View File
@@ -12,11 +12,10 @@ interface ConsumerInterface
{
/**
* @param Struct $struct
* @return mixed
*/
public function onHandler(Struct $struct): void;
/**
* @return mixed
*/
public function process(): void;
}
+1 -5
View File
@@ -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];
}
}
+1 -15
View File
@@ -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);
}
}
+7 -3
View File
@@ -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) {
+1 -2
View File
@@ -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');
}
+42 -7
View File
@@ -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);
}