diff --git a/kiri-engine/Abstracts/BaseApplication.php b/kiri-engine/Abstracts/BaseApplication.php index 420b7d0f..19d3f9af 100644 --- a/kiri-engine/Abstracts/BaseApplication.php +++ b/kiri-engine/Abstracts/BaseApplication.php @@ -20,6 +20,7 @@ use Kiri\Server\{Server}; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Psr\Log\LoggerInterface; +use Kiri\Events\EventProvider; /** * Class BaseApplication @@ -42,7 +43,7 @@ abstract class BaseApplication extends Component * * @throws */ - public function __construct(public ContainerInterface $container) + public function __construct(public ContainerInterface $container, public EventProvider $eventProvider) { Kiri::init($this); $config = sweep(APP_PATH . '/config'); @@ -183,22 +184,21 @@ abstract class BaseApplication extends Component */ private function addEvent($key, $value): void { - $provider = $this->getEventProvider(); if ($value instanceof \Closure || is_object($value)) { - $provider->on($key, $value, 0); + $this->eventProvider->on($key, $value, 0); return; } if (is_array($value)) { if (is_object($value[0]) && !($value[0] instanceof \Closure)) { - $provider->on($key, $value, 0); + $this->eventProvider->on($key, $value, 0); return; } if (is_string($value[0])) { $value[0] = Kiri::createObject($value[0]); - $provider->on($key, $value, 0); + $this->eventProvider->on($key, $value, 0); return; } @@ -207,7 +207,7 @@ abstract class BaseApplication extends Component if (!is_callable($item, true)) { throw new InitException("Class does not hav callback."); } - $provider->on($key, $item, 0); + $this->eventProvider->on($key, $item, 0); } } diff --git a/kiri-engine/Abstracts/Component.php b/kiri-engine/Abstracts/Component.php index a331667f..881813a0 100644 --- a/kiri-engine/Abstracts/Component.php +++ b/kiri-engine/Abstracts/Component.php @@ -13,13 +13,7 @@ namespace Kiri\Abstracts; use Exception; use JetBrains\PhpStorm\Pure; use Kiri; -use Kiri\Di\Container; use Kiri\Error\StdoutLoggerInterface; -use Kiri\Events\EventDispatch; -use Kiri\Events\EventProvider; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\ContainerInterface; -use Psr\Container\NotFoundExceptionInterface; /** * Class Component @@ -65,37 +59,6 @@ class Component implements Configure } - /** - * @return Container|ContainerInterface - */ - #[Pure] public function getContainer(): ContainerInterface|Container - { - return Kiri::getDi(); - } - - - /** - * @return EventProvider - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface - */ - public function getEventProvider(): EventProvider - { - return $this->getContainer()->get(EventProvider::class); - } - - - /** - * @return EventDispatch - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface - */ - protected function getEventDispatch(): EventDispatch - { - return $this->getContainer()->get(EventDispatch::class); - } - - /** * @param string $name * @return mixed diff --git a/kiri-engine/Redis/Redis.php b/kiri-engine/Redis/Redis.php index 7f5eddef..d567cc30 100644 --- a/kiri-engine/Redis/Redis.php +++ b/kiri-engine/Redis/Redis.php @@ -14,11 +14,10 @@ use Kiri; use Kiri\Abstracts\Component; use Kiri\Abstracts\Config; use Kiri\Core\Json; +use Kiri\Events\EventProvider; use Kiri\Exception\ConfigException; use Kiri\Pool\Pool; use Kiri\Server\Events\OnWorkerExit; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; /** * Class Redis @@ -41,25 +40,31 @@ class Redis extends Component const REDIS_OPTION_POOL_MAX = 'max'; - private Kiri\Pool\Pool $pool; + /** + * @param EventProvider $eventProvider + * @param Pool $pool + * @param array $config + * @throws Exception + */ + public function __construct(public EventProvider $eventProvider, + public Pool $pool, array $config = []) + { + parent::__construct($config); + } /** * @return void * @throws ConfigException - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface * @throws Exception */ public function init() { - $this->pool = Kiri::getDi()->get(Pool::class); - $config = $this->get_config(); $length = Config::get('cache.redis.pool.max', 10); - $this->getEventProvider()->on(OnWorkerExit::class, [$this, 'destroy'], 0); + $this->eventProvider->on(OnWorkerExit::class, [$this, 'destroy'], 0); $this->pool->initConnections($config['host'], $length); } diff --git a/kiri-task/AsyncTaskExecute.php b/kiri-task/AsyncTaskExecute.php index ec067ceb..aa90bd37 100644 --- a/kiri-task/AsyncTaskExecute.php +++ b/kiri-task/AsyncTaskExecute.php @@ -7,6 +7,7 @@ use Kiri; use Kiri\Abstracts\Component; use Kiri\Server\SwooleServerInterface; use Psr\Container\ContainerExceptionInterface; +use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; @@ -17,17 +18,15 @@ class AsyncTaskExecute extends Component { - public TaskManager $hashMap; - - /** - * @return void - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface + * @param TaskManager $hashMap + * @param ContainerInterface $container + * @param array $config + * @throws Exception */ - public function init() + public function __construct(public TaskManager $hashMap, public ContainerInterface $container, array $config = []) { - $this->hashMap = $this->getContainer()->get(TaskManager::class); + parent::__construct($config); } @@ -44,9 +43,8 @@ class AsyncTaskExecute extends Component if (is_string($handler)) { $handler = $this->handle($handler, $params); } - $container = $this->getContainer(); - if ($container->has(SwooleServerInterface::class)) { - $server = $container->get(SwooleServerInterface::class); + if ($this->container->has(SwooleServerInterface::class)) { + $server = $this->container->get(SwooleServerInterface::class); if ($workerId < 0 || $workerId > $server->setting['task_worker_num']) { $workerId = random_int(0, $server->setting['task_worker_num'] - 1); } @@ -69,7 +67,7 @@ class AsyncTaskExecute extends Component if (!class_exists($handler) && $this->hashMap->has($handler)) { $handler = $this->hashMap->get($handler); } - $implements = $this->getContainer()->getReflect($handler); + $implements = $this->container->getReflect($handler); if (!in_array(OnTaskInterface::class, $implements->getInterfaceNames())) { throw new Exception('Task must instance ' . OnTaskInterface::class); } diff --git a/kiri-task/TaskManager.php b/kiri-task/TaskManager.php index 9a1a9e74..c398abf1 100644 --- a/kiri-task/TaskManager.php +++ b/kiri-task/TaskManager.php @@ -2,10 +2,12 @@ namespace Kiri\Task; +use Exception; use JetBrains\PhpStorm\Pure; use Kiri\Abstracts\Component; use Kiri\Core\HashMap; use Psr\Container\ContainerExceptionInterface; +use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; use Swoole\Server; @@ -17,10 +19,13 @@ class TaskManager extends Component /** - * + * @param ContainerInterface $container + * @param array $config + * @throws Exception */ - public function init() + public function __construct(public ContainerInterface $container, array $config = []) { + parent::__construct($config); $this->hashMap = new HashMap(); } @@ -38,7 +43,7 @@ class TaskManager extends Component } $task_use_object = $swollen->setting['task_object'] ?? $swollen->setting['task_use_object'] ?? false; - $reflect = $this->getContainer()->get(OnServerTask::class); + $reflect = $this->container->get(OnServerTask::class); $swollen->on('finish', [$reflect, 'onFinish']); if ($task_use_object || $swollen->setting['task_enable_coroutine']) { @@ -79,7 +84,7 @@ class TaskManager extends Component { $task = $this->hashMap->get($key); if (is_string($task)) { - $task = $this->getContainer()->get($task); + $task = $this->container->get($task); if (!empty($task)) { $this->add($key, $task); }