Files
kiri-core/kiri-engine/Application.php
T

156 lines
3.7 KiB
PHP
Raw Normal View History

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 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};
2023-09-13 16:59:26 +08:00
use Symfony\Component\Console\{Application as ConsoleApplication,
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;
2023-12-12 10:56:44 +08:00
use function config;
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-12-12 15:35:38 +08:00
* @throws
2023-07-10 02:04:49 +08:00
*/
public function init(): void
{
2023-12-12 10:56:44 +08:00
$this->errorHandler->registerShutdownHandler(config('error.shutdown', []));
$this->errorHandler->registerExceptionHandler(config('error.exception', []));
$this->errorHandler->registerErrorHandler(config('error.error', []));
$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
2023-12-12 15:35:38 +08:00
* @throws
2023-08-11 00:12:33 +08:00
*/
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);
2023-12-12 10:56:44 +08:00
$scanner->load_directory(APP_PATH . 'app/');
} else if (config('reload.hot', false) === false) {
2023-08-11 00:12:33 +08:00
$scanner = $this->container->get(Scanner::class);
2023-12-12 10:56:44 +08:00
$scanner->load_directory(APP_PATH . 'app/');
2023-08-11 00:12:33 +08:00
}
}
2023-07-10 02:04:49 +08:00
/**
2023-11-30 18:03:48 +08:00
* @param string ...$services
2023-07-10 02:04:49 +08:00
* @return $this
2023-12-12 15:35:38 +08:00
* @throws
2023-07-10 02:04:49 +08:00
*/
2023-11-30 18:03:48 +08:00
public function import(string ...$services): static
2023-07-10 02:04:49 +08:00
{
2023-11-30 18:03:48 +08:00
foreach ($services as $service) {
if (!class_exists($service)) {
continue;
}
/** @var Kiri\Abstracts\Provider $class */
$class = $this->container->get($service);
if (method_exists($class, 'onImport')) {
$class->onImport();
}
2023-07-10 02:04:49 +08:00
}
return $this;
}
/**
* @param Kernel $kernel
* @return $this
2023-12-12 15:35:38 +08:00
* @throws
2023-07-10 02:04:49 +08:00
*/
public function commands(Kernel $kernel): static
{
foreach ($kernel->getCommands() as $command) {
$this->command($command);
}
return $this;
}
/**
2023-11-30 17:02:21 +08:00
* @param string ...$command
2023-07-10 02:04:49 +08:00
* @return void
2023-12-12 15:35:38 +08:00
* @throws
2023-07-10 02:04:49 +08:00
*/
2023-11-30 17:02:21 +08:00
public function command(string ...$command): void
2023-07-10 02:04:49 +08:00
{
2023-08-11 00:12:33 +08:00
$console = $this->container->get(ConsoleApplication::class);
2023-11-30 17:02:21 +08:00
foreach ($command as $value) {
$console->add($this->container->get($value));
}
2023-07-10 02:04:49 +08:00
}
/**
* @param array $argv
* @return void
2023-12-12 15:35:38 +08:00
* @throws
2023-07-10 02:04:49 +08:00
*/
public function execute(array $argv): void
{
2023-11-30 17:02:21 +08:00
/** @var ArgvInput $input */
$input = $this->container->bind(ArgvInput::class, new ArgvInput($argv));
/** @var ConsoleOutput $output */
$output = $this->container->bind(OutputInterface::class, new ConsoleOutput());
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));
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-30 17:02:21 +08:00
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
}