modify plugin name

This commit is contained in:
2022-03-03 18:30:59 +08:00
parent 9cd44aad3d
commit 4af173deff
5 changed files with 38 additions and 67 deletions
+6 -6
View File
@@ -20,6 +20,7 @@ use Kiri\Server\{Server};
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Kiri\Events\EventProvider;
/** /**
* Class BaseApplication * Class BaseApplication
@@ -42,7 +43,7 @@ abstract class BaseApplication extends Component
* *
* @throws * @throws
*/ */
public function __construct(public ContainerInterface $container) public function __construct(public ContainerInterface $container, public EventProvider $eventProvider)
{ {
Kiri::init($this); Kiri::init($this);
$config = sweep(APP_PATH . '/config'); $config = sweep(APP_PATH . '/config');
@@ -183,22 +184,21 @@ abstract class BaseApplication extends Component
*/ */
private function addEvent($key, $value): void private function addEvent($key, $value): void
{ {
$provider = $this->getEventProvider();
if ($value instanceof \Closure || is_object($value)) { if ($value instanceof \Closure || is_object($value)) {
$provider->on($key, $value, 0); $this->eventProvider->on($key, $value, 0);
return; return;
} }
if (is_array($value)) { if (is_array($value)) {
if (is_object($value[0]) && !($value[0] instanceof \Closure)) { if (is_object($value[0]) && !($value[0] instanceof \Closure)) {
$provider->on($key, $value, 0); $this->eventProvider->on($key, $value, 0);
return; return;
} }
if (is_string($value[0])) { if (is_string($value[0])) {
$value[0] = Kiri::createObject($value[0]); $value[0] = Kiri::createObject($value[0]);
$provider->on($key, $value, 0); $this->eventProvider->on($key, $value, 0);
return; return;
} }
@@ -207,7 +207,7 @@ abstract class BaseApplication extends Component
if (!is_callable($item, true)) { if (!is_callable($item, true)) {
throw new InitException("Class does not hav callback."); throw new InitException("Class does not hav callback.");
} }
$provider->on($key, $item, 0); $this->eventProvider->on($key, $item, 0);
} }
} }
-37
View File
@@ -13,13 +13,7 @@ namespace Kiri\Abstracts;
use Exception; use Exception;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Kiri; use Kiri;
use Kiri\Di\Container;
use Kiri\Error\StdoutLoggerInterface; 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 * 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 * @param string $name
* @return mixed * @return mixed
+13 -8
View File
@@ -14,11 +14,10 @@ use Kiri;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Core\Json; use Kiri\Core\Json;
use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Kiri\Pool\Pool; use Kiri\Pool\Pool;
use Kiri\Server\Events\OnWorkerExit; use Kiri\Server\Events\OnWorkerExit;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/** /**
* Class Redis * Class Redis
@@ -41,25 +40,31 @@ class Redis extends Component
const REDIS_OPTION_POOL_MAX = 'max'; 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 * @return void
* @throws ConfigException * @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception * @throws Exception
*/ */
public function init() public function init()
{ {
$this->pool = Kiri::getDi()->get(Pool::class);
$config = $this->get_config(); $config = $this->get_config();
$length = Config::get('cache.redis.pool.max', 10); $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); $this->pool->initConnections($config['host'], $length);
} }
+10 -12
View File
@@ -7,6 +7,7 @@ use Kiri;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Server\SwooleServerInterface; use Kiri\Server\SwooleServerInterface;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
@@ -17,17 +18,15 @@ class AsyncTaskExecute extends Component
{ {
public TaskManager $hashMap;
/** /**
* @return void * @param TaskManager $hashMap
* @throws ContainerExceptionInterface * @param ContainerInterface $container
* @throws NotFoundExceptionInterface * @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)) { if (is_string($handler)) {
$handler = $this->handle($handler, $params); $handler = $this->handle($handler, $params);
} }
$container = $this->getContainer(); if ($this->container->has(SwooleServerInterface::class)) {
if ($container->has(SwooleServerInterface::class)) { $server = $this->container->get(SwooleServerInterface::class);
$server = $container->get(SwooleServerInterface::class);
if ($workerId < 0 || $workerId > $server->setting['task_worker_num']) { if ($workerId < 0 || $workerId > $server->setting['task_worker_num']) {
$workerId = random_int(0, $server->setting['task_worker_num'] - 1); $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)) { if (!class_exists($handler) && $this->hashMap->has($handler)) {
$handler = $this->hashMap->get($handler); $handler = $this->hashMap->get($handler);
} }
$implements = $this->getContainer()->getReflect($handler); $implements = $this->container->getReflect($handler);
if (!in_array(OnTaskInterface::class, $implements->getInterfaceNames())) { if (!in_array(OnTaskInterface::class, $implements->getInterfaceNames())) {
throw new Exception('Task must instance ' . OnTaskInterface::class); throw new Exception('Task must instance ' . OnTaskInterface::class);
} }
+9 -4
View File
@@ -2,10 +2,12 @@
namespace Kiri\Task; namespace Kiri\Task;
use Exception;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Core\HashMap; use Kiri\Core\HashMap;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use Swoole\Server; 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(); $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; $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']); $swollen->on('finish', [$reflect, 'onFinish']);
if ($task_use_object || $swollen->setting['task_enable_coroutine']) { if ($task_use_object || $swollen->setting['task_enable_coroutine']) {
@@ -79,7 +84,7 @@ class TaskManager extends Component
{ {
$task = $this->hashMap->get($key); $task = $this->hashMap->get($key);
if (is_string($task)) { if (is_string($task)) {
$task = $this->getContainer()->get($task); $task = $this->container->get($task);
if (!empty($task)) { if (!empty($task)) {
$this->add($key, $task); $this->add($key, $task);
} }