Files
kiri-core/core/Application.php
T

257 lines
4.4 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;
2021-08-11 01:04:57 +08:00
use Kiri\Abstracts\BaseApplication;
use Kiri\Abstracts\Config;
use Kiri\Abstracts\Kernel;
use Kiri\Crontab\CrontabProviders;
use Kiri\Exception\NotFindClassException;
2021-08-19 15:22:48 +08:00
use Kiri\FileListen\FileChangeCustomProcess;
use ReflectionException;
2021-09-05 15:49:25 +08:00
use Server\Events\OnBeforeCommandExecute;
2021-09-02 14:08:44 +08:00
use Server\ServerCommand;
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;
2021-09-06 14:30:20 +08:00
use Symfony\Component\Console\Application as ConsoleApplication;
2021-09-02 14:08:44 +08:00
use Symfony\Component\Console\Command\Command;
2021-09-02 11:13:11 +08:00
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
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)
{
if (!isset($this->_process[$class])) {
$this->_process[$class] = [];
}
$this->_process[$class][] = $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()
{
$manager = $this->getServer();
$manager->addProcess(FileChangeCustomProcess::class);
enable_file_modification_listening();
}
/**
* @param Closure|array $closure
* @return $this
* @throws Exception
*/
public function middleware(Closure|array $closure): static
{
$this->getRouter()->setMiddleware($closure);
return $this;
}
/**
* @param bool $useTree
* @return $this
* @throws Exception
*/
public function setUseTree(bool $useTree): static
{
$this->getRouter()->setUseTree($useTree);
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
* @throws Exception
*/
public function execute(array $argv): void
{
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-09-02 14:11:05 +08:00
$command = 'list';
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 NotFindClassException
* @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-09-02 13:59:00 +08:00
if (!($class instanceof ServerCommand)) {
2021-09-02 11:13:11 +08:00
scan_directory(directory('app'), 'App');
}
2021-09-02 14:08:44 +08:00
$class->run($input, $output);
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
}