This commit is contained in:
2023-08-11 00:12:33 +08:00
parent 69473a349e
commit 2c21e5c2fe
5 changed files with 109 additions and 91 deletions
+16
View File
@@ -662,6 +662,22 @@ if (!function_exists('on')) {
} }
if (!function_exists('off')) {
/**
* @param $name
* @param $callback
* @throws
*/
function off($name, $callback): void
{
$pro = di(EventProvider::class);
$pro->off($name, $callback);
}
}
if (!function_exists('process_name_set')) { if (!function_exists('process_name_set')) {
+21 -44
View File
@@ -13,13 +13,17 @@ namespace Kiri\Abstracts;
use Database\DatabasesProviders; use Database\DatabasesProviders;
use Exception; use Exception;
use Kiri; use Kiri;
use Kiri\Events\EventInterface;
use Kiri\Di\LocalService; use Kiri\Di\LocalService;
use Kiri\Config\ConfigProvider; use Kiri\Config\ConfigProvider;
use Kiri\Error\StdoutLogger; use Kiri\Error\StdoutLogger;
use Kiri\Exception\{InitException}; use Kiri\Exception\{InitException};
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Kiri\Events\EventProvider; use Kiri\Events\EventProvider;
use ReflectionException;
/** /**
* Class BaseApplication * Class BaseApplication
@@ -99,16 +103,15 @@ abstract class BaseApplication extends Component
*/ */
public function parseStorage(ConfigProvider $config): void public function parseStorage(ConfigProvider $config): void
{ {
if ($storage = $config->get('storage', 'storage')) { $storage = $config->get('storage', 'storage');
if (!str_contains($storage, APP_PATH)) { if (!str_contains($storage, APP_PATH)) {
$storage = APP_PATH . $storage . '/'; $storage = APP_PATH . $storage . '/';
} }
if (!is_dir($storage)) { if (!is_dir($storage)) {
mkdir($storage, 0777, true); mkdir($storage, 0777, true);
} }
if (!is_dir($storage) || !is_writeable($storage)) { if (!is_dir($storage) || !is_writeable($storage)) {
throw new InitException("Directory {$storage} does not have write permission"); throw new InitException("Directory {$storage} does not have write permission");
}
} }
} }
@@ -116,6 +119,9 @@ abstract class BaseApplication extends Component
/** /**
* @param ConfigProvider $config * @param ConfigProvider $config
* @return void * @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
public function parseEvents(ConfigProvider $config): void public function parseEvents(ConfigProvider $config): void
@@ -123,42 +129,13 @@ abstract class BaseApplication extends Component
$events = $config->get('events', []); $events = $config->get('events', []);
foreach ($events as $key => $value) { foreach ($events as $key => $value) {
if (is_string($value)) { if (is_string($value)) {
$value = Kiri::createObject($value); $value = $this->container->get($value);
if (!($value instanceof EventInterface)) {
throw new Exception("Event listen must implement " . EventInterface::class);
}
$value = [$value, 'process'];
} }
$this->addEvent($key, $value);
}
}
/**
* @param $key
* @param $value
* @return void
* @throws InitException
* @throws Exception
*/
private function addEvent($key, $value): void
{
if ($value instanceof \Closure || is_object($value)) {
$this->provider->on($key, $value, 0); $this->provider->on($key, $value, 0);
return;
}
if (!is_array($value)) {
return;
}
if (is_object($value[0]) && !($value[0] instanceof \Closure)) {
$this->provider->on($key, $value, 0);
return;
} else if (is_string($value[0])) {
$value[0] = Kiri::createObject($value[0]);
$this->provider->on($key, $value, 0);
return;
}
foreach ($value as $item) {
if (!is_callable($item, true)) {
throw new InitException("Class does not hav callback.");
}
$this->provider->on($key, $item, 0);
} }
} }
+59 -46
View File
@@ -19,11 +19,7 @@ use Kiri\Events\{OnAfterCommandExecute, OnBeforeCommandExecute};
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use ReflectionException; use ReflectionException;
use Symfony\Component\Console\{Application as ConsoleApplication, use Symfony\Component\Console\{Application as ConsoleApplication, Input\ArgvInput, Output\ConsoleOutput, Output\OutputInterface};
Input\ArgvInput,
Output\ConsoleOutput,
Output\OutputInterface
};
use Kiri\Server\Events\OnWorkerStart; use Kiri\Server\Events\OnWorkerStart;
/** /**
@@ -54,6 +50,10 @@ class Application extends BaseApplication
/** /**
* @return void * @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
* @throws Exception
*/ */
public function init(): void public function init(): void
{ {
@@ -61,8 +61,44 @@ class Application extends BaseApplication
$this->errorHandler->registerExceptionHandler(\config('error.exception', [])); $this->errorHandler->registerExceptionHandler(\config('error.exception', []));
$this->errorHandler->registerErrorHandler(\config('error.error', [])); $this->errorHandler->registerErrorHandler(\config('error.error', []));
$this->id = \config('id', uniqid('id.')); $this->id = \config('id', uniqid('id.'));
$event = $this->container->get(Kiri\Events\EventProvider::class);
$event->on(OnBeforeCommandExecute::class, [$this, 'beforeCommandExecute']);
} }
/**
* @param OnBeforeCommandExecute $beforeCommandExecute
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function beforeCommandExecute(OnBeforeCommandExecute $beforeCommandExecute): void
{
if (!($beforeCommandExecute->command instanceof Kiri\Server\ServerCommand)) {
$scanner = $this->container->get(Scanner::class);
$scanner->read(APP_PATH . 'app/');
} else if (\config('reload.hot', false) === false) {
$scanner = $this->container->get(Scanner::class);
$scanner->read(APP_PATH . 'app/');
} else {
on(OnWorkerStart::class, [$this, 'scanner']);
}
}
/**
* @return void
* @throws ReflectionException
*/
public function scanner(): void
{
$scanner = di(Scanner::class);
$scanner->read(APP_PATH . 'app/');
}
/** /**
* @param string $service * @param string $service
* @return $this * @return $this
@@ -73,7 +109,7 @@ class Application extends BaseApplication
if (!class_exists($service)) { if (!class_exists($service)) {
return $this; return $this;
} }
$class = Kiri::getDi()->get($service); $class = $this->container->get($service);
if (method_exists($class, 'onImport')) { if (method_exists($class, 'onImport')) {
$class->onImport($this->localService); $class->onImport($this->localService);
} }
@@ -84,6 +120,8 @@ class Application extends BaseApplication
/** /**
* @param Kernel $kernel * @param Kernel $kernel
* @return $this * @return $this
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException * @throws ReflectionException
*/ */
public function commands(Kernel $kernel): static public function commands(Kernel $kernel): static
@@ -98,13 +136,14 @@ class Application extends BaseApplication
/** /**
* @param string $command * @param string $command
* @return void * @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException * @throws ReflectionException
*/ */
public function command(string $command): void public function command(string $command): void
{ {
$container = Kiri::getDi(); $console = $this->container->get(ConsoleApplication::class);
$console = $container->get(ConsoleApplication::class); $console->add($this->container->get($command));
$console->add($container->get($command));
} }
@@ -118,46 +157,20 @@ class Application extends BaseApplication
*/ */
public function execute(array $argv): void public function execute(array $argv): void
{ {
$container = Kiri::getDi();
[$input, $output] = $this->argument($argv);
$console = $container->get(ConsoleApplication::class);
$command = $console->find($input->getFirstArgument());
if (!($command instanceof Kiri\Server\ServerCommand)) {
$scanner = $container->get(Scanner::class);
$scanner->read(APP_PATH . 'app/');
} else if (\config('reload.hot', false) === false) {
$scanner = $container->get(Scanner::class);
$scanner->read(APP_PATH . 'app/');
} else {
on(OnWorkerStart::class, function () {
$scanner = di(Scanner::class);
$scanner->read(APP_PATH . 'app/');
});
}
fire(new OnBeforeCommandExecute());
$command->run($input, $output);
fire(new OnAfterCommandExecute());
$output->writeln('ok' . PHP_EOL);
}
/**
* @param $argv
* @return array
*/
private function argument($argv): array
{
$container = Kiri::getDi();
$input = new ArgvInput($argv); $input = new ArgvInput($argv);
$container->bind(ArgvInput::class, $input); $this->container->bind(ArgvInput::class, $input);
$output = new ConsoleOutput(); $output = new ConsoleOutput();
$container->bind(OutputInterface::class, $output); $this->container->bind(OutputInterface::class, $output);
return [$input, $output]; $console = $this->container->get(ConsoleApplication::class);
$command = $console->find($input->getFirstArgument());
fire(new OnBeforeCommandExecute($command));
$command->run($input, $output);
fire(new OnAfterCommandExecute($command));
$output->writeln('execute complete.' . PHP_EOL);
} }
} }
+3 -1
View File
@@ -2,6 +2,8 @@
namespace Kiri\Events; namespace Kiri\Events;
use Symfony\Component\Console\Command\Command;
class OnAfterCommandExecute class OnAfterCommandExecute
{ {
@@ -9,7 +11,7 @@ class OnAfterCommandExecute
/** /**
* *
*/ */
public function __construct() public function __construct(public Command $command)
{ {
} }
@@ -2,7 +2,17 @@
namespace Kiri\Events; namespace Kiri\Events;
use Symfony\Component\Console\Command\Command;
class OnBeforeCommandExecute class OnBeforeCommandExecute
{ {
/**
* @param Command $command
*/
public function __construct(public Command $command)
{
}
} }