From becf0bf2496ff8e8d8b10704f3e939e5f8709fe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Sun, 16 Apr 2023 00:59:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kiri-annotation/AbstractAttribute.php | 68 ------- kiri-annotation/Annotation.php | 86 --------- kiri-annotation/Aspect.php | 37 ---- kiri-annotation/Event.php | 69 ------- kiri-annotation/IAnnotation.php | 19 -- kiri-annotation/Inject.php | 106 ----------- kiri-annotation/Loader.php | 228 ----------------------- kiri-annotation/Mapping.php | 31 --- kiri-annotation/Route/Document.php | 34 ---- kiri-annotation/Route/Middleware.php | 52 ------ kiri-annotation/Route/RequestMapping.php | 21 --- kiri-annotation/Route/RequestMethod.php | 32 ---- kiri-annotation/Route/Route.php | 40 ---- kiri-annotation/Route/Socket.php | 31 --- kiri-annotation/Route/TestController.php | 24 --- kiri-annotation/Target.php | 28 --- kiri-engine/Abstracts/Component.php | 10 +- kiri-engine/Main.php | 5 +- kiri-engine/Redis/Redis.php | 98 ++++++---- kiri-engine/Scanner.php | 100 ++++++++++ 20 files changed, 164 insertions(+), 955 deletions(-) delete mode 100644 kiri-annotation/AbstractAttribute.php delete mode 100644 kiri-annotation/Annotation.php delete mode 100644 kiri-annotation/Aspect.php delete mode 100644 kiri-annotation/Event.php delete mode 100644 kiri-annotation/IAnnotation.php delete mode 100644 kiri-annotation/Inject.php delete mode 100644 kiri-annotation/Loader.php delete mode 100644 kiri-annotation/Mapping.php delete mode 100644 kiri-annotation/Route/Document.php delete mode 100644 kiri-annotation/Route/Middleware.php delete mode 100644 kiri-annotation/Route/RequestMapping.php delete mode 100644 kiri-annotation/Route/RequestMethod.php delete mode 100644 kiri-annotation/Route/Route.php delete mode 100644 kiri-annotation/Route/Socket.php delete mode 100644 kiri-annotation/Route/TestController.php delete mode 100644 kiri-annotation/Target.php create mode 100644 kiri-engine/Scanner.php diff --git a/kiri-annotation/AbstractAttribute.php b/kiri-annotation/AbstractAttribute.php deleted file mode 100644 index bcbd5080..00000000 --- a/kiri-annotation/AbstractAttribute.php +++ /dev/null @@ -1,68 +0,0 @@ -_class; - } - - /** - * @param object $class - */ - public function setClass(object $class): void - { - $this->_class = $class; - } - - /** - * @return string - */ - public function getMethod(): string - { - return $this->_method; - } - - /** - * @param string $method - */ - public function setMethod(string $method): void - { - $this->_method = $method; - } - - -} diff --git a/kiri-annotation/Annotation.php b/kiri-annotation/Annotation.php deleted file mode 100644 index 0e246796..00000000 --- a/kiri-annotation/Annotation.php +++ /dev/null @@ -1,86 +0,0 @@ -_loader = new Loader(); - } - - - /** - * @return Loader - */ - public function getLoader(): Loader - { - return $this->_loader; - } - - - /** - * @param Loader $loader - * @return Loader - */ - public function setLoader(Loader $loader): Loader - { - return $this->_loader = $loader; - } - - - /** - * @param object $class - * @throws ReflectionException - */ - public function injectProperty(object $class) - { - $this->_loader->injectProperty($class::class, $class); - } - - - /** - * @param string $path - * @param string $namespace - * @param array $exclude - * @return static - * @throws Exception - */ - public function read(string $path, string $namespace = 'App', array $exclude = []): static - { - $this->_loader->_scanDir(new DirectoryIterator($path), $namespace, $exclude); - return $this; - } - - - /** - * @param string $dir - * @param array $exclude - * @return array - * @throws Exception - */ - public function runtime(string $dir, array $exclude = []): array - { - return $this->_loader->loadByDirectory($dir, $exclude); - } - - -} diff --git a/kiri-annotation/Aspect.php b/kiri-annotation/Aspect.php deleted file mode 100644 index 59937721..00000000 --- a/kiri-annotation/Aspect.php +++ /dev/null @@ -1,37 +0,0 @@ -get(EventProvider::class); - if (is_string($class)) { - $class = Kiri::getDi()->get($class); - } - $pro->on($this->name, [$class, $method]); - return true; - } - -} diff --git a/kiri-annotation/IAnnotation.php b/kiri-annotation/IAnnotation.php deleted file mode 100644 index ba6e56ce..00000000 --- a/kiri-annotation/IAnnotation.php +++ /dev/null @@ -1,19 +0,0 @@ -getProperty($class, $method))) { - return false; - } - /** @var ReflectionProperty $class */ - $injectValue = static::parseInjectValue(); - if ($method->isPrivate() || $method->isProtected()) { - $this->setter($class, $method, $injectValue); - } else { - $class->{$method->getName()} = $injectValue; - } - return true; - } - - - /** - * @param $class - * @param $method - * @param $injectValue - */ - private function setter($class, $method, $injectValue) - { - $method = 'set' . ucfirst(Str::convertUnderline($method->getName())); - if (!method_exists($class, $method)) { - return; - } - $class->$method($injectValue); - } - - - /** - * @param $class - * @param $method - * @return ReflectionProperty|bool - * @throws ReflectionException - */ - private function getProperty($class, $method): ReflectionProperty|bool - { - if ($method instanceof ReflectionProperty && !$method->isStatic()) { - return $method; - } - if (is_object($class)) $class = $class::class; - $method = Kiri::getDi()->getClassReflectionProperty($class, $method); - if (!$method || $method->isStatic()) { - return false; - } - return $method; - } - - - /** - * @return mixed - * @throws Exception - */ - private function parseInjectValue(): mixed - { - $localService = Kiri::getDi()->get(LocalService::class); - if ($localService->has($this->value)) { - return $localService->get($this->value); - } - if (!empty($this->construct)) { - return instance($this->value, $this->construct); - } - return di($this->value); - } - -} diff --git a/kiri-annotation/Loader.php b/kiri-annotation/Loader.php deleted file mode 100644 index 0b70027f..00000000 --- a/kiri-annotation/Loader.php +++ /dev/null @@ -1,228 +0,0 @@ -_scanDir(new DirectoryIterator($path), $namespace); - } - - - /** - * @param string $class - * @param object $handler - * @return $this - * @throws ReflectionException - * @throws Exception - */ - public function injectProperty(string $class, object $handler): static - { - $di = Kiri::getDi(); - - $reflect = $di->getReflect($class); - - $di->propertyInject($reflect, $handler); - - return $this; - } - - - /** - * @param string $class - * @param string $method - * @return mixed - */ - public function getMethod(string $class, string $method = ''): array - { - if (!isset($this->_methods[$class])) { - return []; - } - $properties = $this->_methods[$class]; - if (!empty($method) && isset($properties[$method])) { - return $properties[$method]; - } - return $properties; - } - - - /** - * @param DirectoryIterator $paths - * @param $namespace - * @param array $exclude - * @throws Exception - */ - public function _scanDir(DirectoryIterator $paths, $namespace, array $exclude = []) - { - foreach ($paths as $path) { - if (function_exists('opcache_invalidate')) { - opcache_invalidate($path->getRealPath(), true); - } - if ($path->isDot() || str_starts_with($path->getFilename(), '.')) { - continue; - } - if ($this->inExclude($exclude, $path->getRealPath())) { - continue; - } - if ($path->isDir()) { - $iterator = new DirectoryIterator($path->getRealPath()); - $directory = rtrim($path->getRealPath(), '/'); - if (!isset($this->_directory[$directory])) { - $this->_directory[$directory] = []; - } - $this->_scanDir($iterator, $namespace); - } else { - $this->readFile($path, $namespace); - } - } - } - - - /** - * @param DirectoryIterator $path - * @param $namespace - * @throws Exception - */ - private function readFile(DirectoryIterator $path, $namespace) - { - try { - if ($path->getExtension() !== 'php') { - return; - } - $replace = $this->getReflect($path, $namespace); - if (!$replace || !$replace->getAttributes(Target::class)) { - return; - } - $this->appendFileToDirectory($path->getRealPath(), $replace->getName()); - } catch (Throwable $throwable) { - $this->logger->error(jTraceEx($throwable)); - } - } - - - /** - * @param DirectoryIterator $path - * @param string $namespace - * @return ReflectionClass|null - */ - private function getReflect(DirectoryIterator $path, string $namespace): ?ReflectionClass - { - $class = $this->explodeFileName($path, $namespace); - if (!class_exists($class)) { - return null; - } - return Kiri::getDi()->getReflectionClass($class); - } - - - /** - * @param string $path - * @param array $exclude - * @return array - * @throws Exception - */ - public function loadByDirectory(string $path, array $exclude = []): array - { - try { - $path = '/' . trim($path, '/'); - $paths = []; - foreach ($this->_directory as $key => $_path) { - $key = '/' . trim($key, '/'); - if (!str_starts_with($key, $path) || $this->inExclude($exclude, $path)) { - continue; - } - unset($this->_directory[$key]); - foreach ($_path as $item) { - $paths[] = $item; - } - } - return $paths; - } catch (Throwable $exception) { - $this->logger->addError($exception, 'throwable'); - return []; - } - } - - - /** - * @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($path, $value)) { - return true; - } - } - return false; - } - - - /** - * @param DirectoryIterator $path - * @param string $namespace - * @return string - */ - private function explodeFileName(DirectoryIterator $path, string $namespace): string - { - $replace = str_replace(APP_PATH, '', $path->getRealPath()); - - $replace = str_replace('.php', '', $replace); - $replace = str_replace(DIRECTORY_SEPARATOR, '\\', $replace); - $explode = explode('\\', $replace); - array_shift($explode); - - return $namespace . '\\' . implode('\\', $explode); - } - - - /** - * @param string $filePath - * @param string $className - */ - public function appendFileToDirectory(string $filePath, string $className) - { - $array = explode('/', $filePath); - unset($array[count($array) - 1]); - - $array = '/' . trim(implode('/', $array), '/'); - - $this->_directory[$array][] = $className; - } - - -} diff --git a/kiri-annotation/Mapping.php b/kiri-annotation/Mapping.php deleted file mode 100644 index bc6d501c..00000000 --- a/kiri-annotation/Mapping.php +++ /dev/null @@ -1,31 +0,0 @@ -mapping($this->class, $class); - - return parent::execute($class, $method); - } - -} diff --git a/kiri-annotation/Route/Document.php b/kiri-annotation/Route/Document.php deleted file mode 100644 index fea578a8..00000000 --- a/kiri-annotation/Route/Document.php +++ /dev/null @@ -1,34 +0,0 @@ - '整数', - self::STRING => '字符串', - self::BOOLEAN => '布尔值', - self::FLOAT => '浮点', - ]; - - - public function __construct(array $request, array $response) - { - } - - -} diff --git a/kiri-annotation/Route/Middleware.php b/kiri-annotation/Route/Middleware.php deleted file mode 100644 index 6579642f..00000000 --- a/kiri-annotation/Route/Middleware.php +++ /dev/null @@ -1,52 +0,0 @@ -middleware)) { - $this->middleware = [$this->middleware]; - } - $array = []; - foreach ($this->middleware as $value) { - if (!in_array(MiddlewareInterface::class, class_implements($value))) { - throw new \Exception('The middleware'); - } - $array[] = $value; - } - $this->middleware = $array; - } - - - /** - * @param mixed $class - * @param mixed|null $method - * @return $this - */ - public function execute(mixed $class, mixed $method = null): mixed - { - MiddlewareManager::add($class, $method, $this->middleware); - return parent::execute($class, $method); - } - - -} diff --git a/kiri-annotation/Route/RequestMapping.php b/kiri-annotation/Route/RequestMapping.php deleted file mode 100644 index 31fd7671..00000000 --- a/kiri-annotation/Route/RequestMapping.php +++ /dev/null @@ -1,21 +0,0 @@ - 'POST', - self::REQUEST_GET => 'GET', - self::REQUEST_HEAD => 'HEAD', - self::REQUEST_OPTIONS => 'OPTIONS', - self::REQUEST_DELETE => 'DELETE', - self::REQUEST_PUT => 'PUT' - }; - } - -} diff --git a/kiri-annotation/Route/Route.php b/kiri-annotation/Route/Route.php deleted file mode 100644 index 0366c8db..00000000 --- a/kiri-annotation/Route/Route.php +++ /dev/null @@ -1,40 +0,0 @@ -uri = '/' . ltrim($this->uri, '/'); - } - - - /** - * @param mixed $class - * @param mixed|null $method - * @return bool - */ - public function execute(mixed $class, mixed $method = null): bool - { - $di = Kiri::getDi()->get(Router::class); - $di->addRoute($this->method, $this->uri, $class . '@' . $method); - return parent::execute($class, $method); - } - - -} diff --git a/kiri-annotation/Route/Socket.php b/kiri-annotation/Route/Socket.php deleted file mode 100644 index d0a0ac60..00000000 --- a/kiri-annotation/Route/Socket.php +++ /dev/null @@ -1,31 +0,0 @@ -logger)) { - $this->logger = Kiri::getDi()->get(StdoutLoggerInterface::class); - } - if (!empty($config) && is_array($config)) { - Kiri::configure($this, $config); - } + $this->init(); } /** diff --git a/kiri-engine/Main.php b/kiri-engine/Main.php index c69c7f7e..4fcbb2dd 100644 --- a/kiri-engine/Main.php +++ b/kiri-engine/Main.php @@ -12,6 +12,7 @@ namespace Kiri; use Exception; use Kiri; +use Kiri\Di\Container; use Kiri\Abstracts\{BaseMain, Config, Kernel}; use Kiri\Events\{OnAfterCommandExecute, OnBeforeCommandExecute}; use Psr\Container\ContainerExceptionInterface; @@ -23,7 +24,6 @@ use Symfony\Component\Console\{Application as ConsoleApplication, Output\OutputInterface }; use Kiri\Di\LocalService; -use Kiri\Exception\ConfigException; use Kiri\Error\ErrorHandler; @@ -120,6 +120,9 @@ class Main extends BaseMain $console = $this->container->get(ConsoleApplication::class); $command = $console->find($input->getFirstArgument()); + $scanner = $this->container->get(Scanner::class); + $scanner->read(APP_PATH); + fire(new OnBeforeCommandExecute()); $command->run($input, $output); diff --git a/kiri-engine/Redis/Redis.php b/kiri-engine/Redis/Redis.php index 4580e8fc..7d0d5b19 100644 --- a/kiri-engine/Redis/Redis.php +++ b/kiri-engine/Redis/Redis.php @@ -13,12 +13,15 @@ use Exception; use Kiri; use Kiri\Abstracts\Component; use Kiri\Abstracts\Config; -use Kiri\Core\Json; use Kiri\Events\EventProvider; +use Kiri\Di\Inject\Container; use Kiri\Exception\ConfigException; use Kiri\Exception\RedisConnectException; use Kiri\Pool\Pool; use Kiri\Server\Events\OnWorkerExit; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\ContainerInterface; +use Psr\Container\NotFoundExceptionInterface; /** * Class Redis @@ -28,35 +31,41 @@ use Kiri\Server\Events\OnWorkerExit; class Redis extends Component { + private string $host = ''; - const REDIS_OPTION_HOST = 'host'; - const REDIS_OPTION_PORT = 'port'; - const REDIS_OPTION_PREFIX = 'prefix'; - const REDIS_OPTION_AUTH = 'auth'; - const REDIS_OPTION_DATABASES = 'databases'; - const REDIS_OPTION_TIMEOUT = 'timeout'; - const REDIS_OPTION_POOL = 'pool'; - const REDIS_OPTION_POOL_TICK = 'tick'; - const REDIS_OPTION_POOL_MIN = 'min'; - const REDIS_OPTION_POOL_MAX = 'max'; + private int $port = 6379; + + private string $prefix = 'api:'; + + private string $auth = ''; + + private int $databases = 0; + + private int $timeout = 30; /** - * @param EventProvider $eventProvider - * @param Pool $pool - * @param array $config - * @throws Exception + * @var ContainerInterface */ - public function __construct(public EventProvider $eventProvider, - public Pool $pool, array $config = []) - { - parent::__construct($config); - } + #[Container(ContainerInterface::class)] + readonly public ContainerInterface $container; + + + /** + * @var int + */ + private int $read_timeout = -1; + + /** + * @var array|int[] + */ + private array $pool = ['min' => 1, 'max' => 100]; /** * @return void - * @throws ConfigException + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface * @throws Exception */ public function init(): void @@ -65,9 +74,11 @@ class Redis extends Component $length = Config::get('cache.redis.pool.max', 10); - $this->eventProvider->on(OnWorkerExit::class, [$this, 'destroy'], 0); + $eventProvider = $this->container->get(EventProvider::class); + $eventProvider->on(OnWorkerExit::class, [$this, 'destroy'], 0); - $this->pool->initConnections($config['host'], $length, static function () use ($config) { + $pool = $this->container->get(Pool::class); + $pool->initConnections($config['host'], $length, static function () use ($config) { $redis = new \Redis(); if (!$redis->connect($config['host'], $config['port'], $config['timeout'])) { throw new RedisConnectException(sprintf('The Redis Connect %s::%d Fail.', $config['host'], $config['port'])); @@ -157,30 +168,25 @@ SCRIPT; /** - * @throws ConfigException + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface * @throws Exception */ - public function release() + public function destroy(): void { - $this->pool->clean($this->get_config()['host']); + $pool = $this->container->get(Pool::class); + $pool->clean($this->host); } - /** - * 销毁连接池 - * @throws ConfigException - * @throws Exception - */ - public function destroy() - { - $this->pool->clean($this->get_config()['host']); - } /** * @param $name * @param $arguments * @return mixed * @throws ConfigException - * @throws Exception + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ public function proxy($name, $arguments): mixed { @@ -190,7 +196,8 @@ SCRIPT; } catch (\Throwable $throwable) { $response = $this->logger->addError($throwable->getMessage()); } finally { - $this->pool->push($this->get_config()['host'], $client); + $pool = $this->container->get(Pool::class); + $pool->push($this->host, $client); } return $response; } @@ -199,11 +206,13 @@ SCRIPT; /** * @return \Redis * @throws ConfigException + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ private function getClient(): \Redis { - $config = $this->get_config(); - return $this->pool->get($config['host']); + $pool = $this->container->get(Pool::class); + return $pool->get($this->host); } @@ -212,7 +221,16 @@ SCRIPT; */ public function get_config(): array { - return Config::get('cache.redis', null, true); + return [ + 'host' => $this->host, + 'port' => $this->port, + 'prefix' => $this->prefix, + 'auth' => $this->auth, + 'databases' => $this->databases, + 'timeout' => $this->timeout, + 'read_timeout' => $this->read_timeout, + 'pool' => $this->pool + ]; } } diff --git a/kiri-engine/Scanner.php b/kiri-engine/Scanner.php new file mode 100644 index 00000000..04c8ff58 --- /dev/null +++ b/kiri-engine/Scanner.php @@ -0,0 +1,100 @@ +load_dir($path); + } + + + /** + * @param string $namespace + * @return void + * @throws ReflectionException + * @throws Exception + */ + public function parse(string $namespace): void + { + $container = Container::instance(); + foreach ($this->files as $file) { + $class = $namespace . '\\' . $this->rename($file); + if (!file_exists($class)) { + throw new Exception('Please follow the PSR-4 specification to write code.' . $class); + } + $container->parse($class); + } + } + + + /** + * @param string $file + * @return string + */ + private function rename(string $file): string + { + $filter = array_filter(explode('/', $file), function ($value) { + if (empty($value)) { + return false; + } + return ucfirst($value); + }); + array_shift($filter); + return implode('\\', $filter); + } + + + /** + * @param string $path + * @return void + */ + private function load_dir(string $path): void + { + $dir = new \DirectoryIterator($path); + foreach ($dir as $value) { + if ($value->isDot()) { + continue; + } + if (is_dir($value)) { + $this->load_dir($value->getRealPath()); + } else if ($value->getExtension() == '.php') { + $this->load_file($value); + } + } + } + + + /** + * @param string $path + * @return void + */ + private function load_file(string $path): void + { + try { + require_once "$path"; + $path = str_replace($_SERVER['HOME'], '', $path); + $path = str_replace('.php', '', $path); + $this->files[] = $path; + } catch (\Throwable $throwable) { + error($throwable->getMessage(), [$throwable]); + } + } + + +}