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

254 lines
4.2 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/25 0025
* Time: 18:38
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
2021-08-11 01:04:57 +08:00
namespace Kiri;
2020-08-31 01:27:08 +08:00
2021-04-02 18:57:56 +08:00
use Closure;
2020-09-02 23:41:29 +08:00
use Database\DatabasesProviders;
2020-08-31 01:27:08 +08:00
use Exception;
2022-01-08 18:49:08 +08:00
use Kiri\Abstracts\{BaseApplication, Config, Kernel};
2021-08-11 01:04:57 +08:00
use Kiri\Crontab\CrontabProviders;
2022-01-08 18:49:08 +08:00
use Kiri\Events\{OnAfterCommandExecute, OnBeforeCommandExecute};
2021-11-05 18:54:04 +08:00
use Kiri\FileListen\HotReload;
2021-08-19 15:22:48 +08:00
use ReflectionException;
2021-09-02 14:08:44 +08:00
use Server\ServerProviders;
2020-12-17 14:09:14 +08:00
use stdClass;
2021-08-19 17:41:10 +08:00
use Swoole\Process;
2020-09-08 10:55:06 +08:00
use Swoole\Timer;
2022-01-08 18:49:08 +08:00
use Symfony\Component\Console\{Application as ConsoleApplication,
Command\Command,
Input\ArgvInput,
Input\InputInterface,
Output\ConsoleOutput,
Output\OutputInterface
};
2020-08-31 01:27:08 +08:00
/**
* Class Init
*
2021-08-11 01:04:57 +08:00
* @package Kiri
2020-08-31 01:27:08 +08:00
*
* @property-read Config $config
*/
class Application extends BaseApplication
{
2021-09-02 11:13:11 +08:00
/**
* @var string
*/
public string $id = 'uniqueId';
public string $state = '';
/** @var array<array<Process>> */
private array $_process = [];
/**
*/
public function init()
{
$this->import(ServerProviders::class);
2021-09-02 14:08:44 +08:00
$this->register(Runtime::class);
2021-09-02 11:13:11 +08:00
}
/**
2021-09-06 14:30:20 +08:00
* @throws
2021-09-02 11:13:11 +08:00
*/
public function withDatabase()
{
$this->import(DatabasesProviders::class);
}
/**
2021-09-06 14:30:20 +08:00
* @throws
2021-09-02 11:13:11 +08:00
*/
public function withCrontab()
{
$this->import(CrontabProviders::class);
}
/**
* @param string $class
* @param Process $process
*/
public function addProcess(string $class, Process $process)
{
}
/**
* @return Process[]
*/
public function getProcess(): array
{
return $this->_process;
}
/**
* @param string $class
* @return Process|null
*/
public function getProcessName(string $class): ?Process
{
return $this->_process[$class] ?? null;
}
/**
2021-09-06 14:30:20 +08:00
* @throws
2021-09-02 11:13:11 +08:00
*/
public function withFileChangeListen()
{
2021-11-04 16:18:02 +08:00
$container = Kiri::getDi();
2021-09-02 11:13:11 +08:00
2021-11-04 16:18:02 +08:00
$console = $container->get(ConsoleApplication::class);
2021-11-05 18:54:04 +08:00
$console->add($container->get(HotReload::class));
2021-09-02 11:13:11 +08:00
}
/**
* @param Closure|array $closure
* @return $this
* @throws Exception
*/
public function middleware(Closure|array $closure): static
{
return $this;
}
/**
* @param bool $useTree
* @return $this
* @throws Exception
*/
public function setUseTree(bool $useTree): static
{
return $this;
}
/**
* @param string $service
* @return $this
* @throws
*/
public function import(string $service): static
{
if (!class_exists($service)) {
2021-09-06 14:30:20 +08:00
return $this;
2021-09-02 11:13:11 +08:00
}
$class = Kiri::getDi()->get($service);
if (method_exists($class, 'onImport')) {
$class->onImport($this);
}
return $this;
}
/**
* @param Kernel $kernel
* @return $this
*/
public function commands(Kernel $kernel): static
{
foreach ($kernel->getCommands() as $command) {
$this->register($command);
}
return $this;
}
/**
* @param string $command
* @throws
*/
public function register(string $command)
{
2021-09-06 14:30:20 +08:00
di(ConsoleApplication::class)->add(di($command));
2021-09-02 11:13:11 +08:00
}
/**
* @param array $argv
* @return void
*/
public function execute(array $argv): void
{
2022-01-08 19:11:23 +08:00
ini_set('swoole.enable_preemptive_scheduler', 'On');
ini_set('swoole.enable_library', 'On');
2021-09-02 14:08:44 +08:00
[$input, $output] = $this->argument($argv);
2021-09-02 11:13:11 +08:00
try {
2021-09-04 17:24:23 +08:00
$console = di(ConsoleApplication::class);
2021-09-02 14:08:44 +08:00
$command = $input->getFirstArgument();
2021-09-02 14:10:28 +08:00
if (empty($command)) {
2021-11-04 18:47:32 +08:00
$command = 'sw:server';
2021-09-02 14:08:44 +08:00
}
$command = $console->find($command);
if ($command instanceof Command) {
$this->enableFileChange($command, $input, $output);
}
2021-09-02 11:13:11 +08:00
} catch (\Throwable $exception) {
2021-09-02 14:08:44 +08:00
$output->writeln(jTraceEx($exception));
2021-09-02 11:13:11 +08:00
} finally {
Timer::clearAll();
}
}
2021-09-02 14:08:44 +08:00
/**
* @param $argv
* @return array
*/
private function argument($argv): array
{
return [new ArgvInput($argv), new ConsoleOutput()];
}
2021-09-02 11:13:11 +08:00
/**
* @throws ReflectionException
2021-09-02 14:08:44 +08:00
* @throws Exception
2021-09-02 11:13:11 +08:00
*/
2021-09-02 14:08:44 +08:00
private function enableFileChange(Command $class, $input, $output): void
2021-09-02 11:13:11 +08:00
{
2021-09-06 14:30:20 +08:00
fire(new OnBeforeCommandExecute());
2021-11-05 18:54:04 +08:00
if (!($class instanceof HotReload)) {
2021-11-04 16:30:48 +08:00
scan_directory(directory('app'), 'App');
}
2022-01-08 10:07:19 +08:00
$this->container->setBindings(OutputInterface::class, $output);
2021-09-02 14:08:44 +08:00
$class->run($input, $output);
2021-11-03 16:29:47 +08:00
fire(new OnAfterCommandExecute());
2021-09-07 15:33:25 +08:00
$output->writeln('ok' . PHP_EOL);
2021-09-02 11:13:11 +08:00
}
/**
* @param $className
* @param null $abstracts
* @return stdClass
* @throws Exception
*/
public function make($className, $abstracts = null): stdClass
{
return make($className, $abstracts);
}
2020-08-31 01:27:08 +08:00
}