2022-06-22 16:29:42 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: whwyy
|
|
|
|
|
* Date: 2018/4/25 0025
|
|
|
|
|
* Time: 18:38
|
|
|
|
|
*/
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Kiri;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
use Kiri;
|
2023-05-26 09:20:31 +08:00
|
|
|
use Kiri\Abstracts\{BaseApplication, Kernel};
|
2023-04-16 16:40:45 +08:00
|
|
|
use Kiri\Di\Scanner;
|
|
|
|
|
use Kiri\Error\ErrorHandler;
|
2022-06-22 16:29:42 +08:00
|
|
|
use Kiri\Events\{OnAfterCommandExecute, OnBeforeCommandExecute};
|
|
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
|
use ReflectionException;
|
2023-09-13 16:59:26 +08:00
|
|
|
use Symfony\Component\Console\{Application as ConsoleApplication,
|
|
|
|
|
Exception\ExceptionInterface,
|
|
|
|
|
Input\ArgvInput,
|
|
|
|
|
Output\ConsoleOutput,
|
2023-11-16 23:50:06 +08:00
|
|
|
Output\OutputInterface
|
|
|
|
|
};
|
2023-11-29 15:02:48 +08:00
|
|
|
use Kiri\Server\ServerCommand;
|
2023-11-16 23:50:06 +08:00
|
|
|
use Kiri\Di\Inject\Container;
|
2022-06-22 16:29:42 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Init
|
|
|
|
|
*
|
|
|
|
|
* @package Kiri
|
|
|
|
|
*/
|
2023-05-25 16:59:20 +08:00
|
|
|
class Application extends BaseApplication
|
2022-06-22 16:29:42 +08:00
|
|
|
{
|
|
|
|
|
|
2023-07-10 02:04:49 +08:00
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
public string $id = 'uniqueId';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string $state = '';
|
|
|
|
|
|
|
|
|
|
|
2023-07-31 23:09:00 +08:00
|
|
|
/**
|
2023-11-16 23:50:06 +08:00
|
|
|
* @var ErrorHandler
|
2023-07-31 23:09:00 +08:00
|
|
|
*/
|
2023-11-16 23:50:06 +08:00
|
|
|
#[Container(ErrorHandler::class)]
|
|
|
|
|
public ErrorHandler $errorHandler;
|
2023-07-31 23:09:00 +08:00
|
|
|
|
|
|
|
|
|
2023-07-10 02:04:49 +08:00
|
|
|
/**
|
|
|
|
|
* @return void
|
2023-08-11 00:12:33 +08:00
|
|
|
* @throws Exception
|
2023-07-10 02:04:49 +08:00
|
|
|
*/
|
|
|
|
|
public function init(): void
|
|
|
|
|
{
|
2023-07-31 23:09:00 +08:00
|
|
|
$this->errorHandler->registerShutdownHandler(\config('error.shutdown', []));
|
|
|
|
|
$this->errorHandler->registerExceptionHandler(\config('error.exception', []));
|
|
|
|
|
$this->errorHandler->registerErrorHandler(\config('error.error', []));
|
2023-07-10 02:04:49 +08:00
|
|
|
$this->id = \config('id', uniqid('id.'));
|
2023-08-11 00:12:33 +08:00
|
|
|
|
2023-11-29 15:02:48 +08:00
|
|
|
$this->provider->on(OnBeforeCommandExecute::class, [$this, 'beforeCommandExecute']);
|
2023-08-11 00:12:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param OnBeforeCommandExecute $beforeCommandExecute
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
public function beforeCommandExecute(OnBeforeCommandExecute $beforeCommandExecute): void
|
|
|
|
|
{
|
2023-11-29 15:02:48 +08:00
|
|
|
if (!($beforeCommandExecute->command instanceof ServerCommand)) {
|
2023-08-11 00:12:33 +08:00
|
|
|
$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/');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-07-10 02:04:49 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $service
|
|
|
|
|
* @return $this
|
|
|
|
|
* @throws
|
|
|
|
|
*/
|
|
|
|
|
public function import(string $service): static
|
|
|
|
|
{
|
|
|
|
|
if (!class_exists($service)) {
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2023-08-11 00:12:33 +08:00
|
|
|
$class = $this->container->get($service);
|
2023-07-10 02:04:49 +08:00
|
|
|
if (method_exists($class, 'onImport')) {
|
2023-07-31 23:09:00 +08:00
|
|
|
$class->onImport($this->localService);
|
2023-07-10 02:04:49 +08:00
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Kernel $kernel
|
|
|
|
|
* @return $this
|
2023-08-11 00:12:33 +08:00
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
2023-07-10 02:04:49 +08:00
|
|
|
*/
|
|
|
|
|
public function commands(Kernel $kernel): static
|
|
|
|
|
{
|
|
|
|
|
foreach ($kernel->getCommands() as $command) {
|
|
|
|
|
$this->command($command);
|
|
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $command
|
|
|
|
|
* @return void
|
2023-08-11 00:12:33 +08:00
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
2023-07-10 02:04:49 +08:00
|
|
|
*/
|
|
|
|
|
public function command(string $command): void
|
|
|
|
|
{
|
2023-08-11 00:12:33 +08:00
|
|
|
$console = $this->container->get(ConsoleApplication::class);
|
|
|
|
|
$console->add($this->container->get($command));
|
2023-07-10 02:04:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $argv
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
|
* @throws ReflectionException
|
2023-09-13 16:59:26 +08:00
|
|
|
* @throws Exception|ExceptionInterface
|
2023-07-10 02:04:49 +08:00
|
|
|
*/
|
|
|
|
|
public function execute(array $argv): void
|
|
|
|
|
{
|
2023-11-29 15:02:48 +08:00
|
|
|
[$input, $output] = [new ArgvInput($argv), new ConsoleOutput()];
|
2023-08-11 00:12:33 +08:00
|
|
|
$this->container->bind(ArgvInput::class, $input);
|
|
|
|
|
$this->container->bind(OutputInterface::class, $output);
|
2022-06-22 16:29:42 +08:00
|
|
|
|
2023-08-11 00:12:33 +08:00
|
|
|
$console = $this->container->get(ConsoleApplication::class);
|
2023-09-13 16:59:26 +08:00
|
|
|
$command = $console->find($input->getFirstArgument() ?? 'list');
|
2023-04-16 00:59:31 +08:00
|
|
|
|
2023-08-11 00:12:33 +08:00
|
|
|
fire(new OnBeforeCommandExecute($command));
|
2022-06-22 16:29:42 +08:00
|
|
|
|
2023-07-10 02:04:49 +08:00
|
|
|
$command->run($input, $output);
|
2023-08-11 00:12:33 +08:00
|
|
|
fire(new OnAfterCommandExecute($command));
|
2023-11-29 14:57:02 +08:00
|
|
|
$output->writeln('execute complete.');
|
2023-07-10 02:04:49 +08:00
|
|
|
}
|
2023-11-16 23:50:06 +08:00
|
|
|
|
2022-06-22 16:29:42 +08:00
|
|
|
}
|