Files
kiri-core/core/Application.php
T

260 lines
4.5 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-08 10:55:06 +08:00
use Console\Console;
2020-09-03 00:15:57 +08:00
use Console\ConsoleProviders;
2020-09-02 23:41:29 +08:00
use Database\DatabasesProviders;
2020-08-31 01:27:08 +08:00
use Exception;
2021-08-20 19:07:10 +08:00
use Http\Command;
2021-08-17 16:52:50 +08:00
use Http\Context\Response;
2021-08-19 15:36:59 +08:00
use Http\Server;
2021-08-17 16:43:50 +08:00
use Http\ServerProviders;
2021-08-11 01:04:57 +08:00
use Kiri\Abstracts\BaseApplication;
use Kiri\Abstracts\Config;
use Kiri\Abstracts\Input;
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-08-17 18:19:26 +08:00
use Server\ResponseInterface;
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;
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-08-11 10:27:39 +08:00
/**
* @var string
*/
public string $id = 'uniqueId';
2021-04-25 01:34:05 +08:00
2021-08-11 10:27:39 +08:00
public string $state = '';
2021-04-25 01:34:05 +08:00
2021-08-19 18:27:19 +08:00
/** @var array<array<Process>> */
2021-08-19 17:41:10 +08:00
private array $_process = [];
2021-08-11 10:27:39 +08:00
/**
* @throws NotFindClassException
*/
public function init()
{
$this->import(ConsoleProviders::class);
$this->import(ServerProviders::class);
}
2021-04-25 01:34:05 +08:00
2021-07-08 18:02:21 +08:00
2021-08-11 10:27:39 +08:00
/**
* @throws NotFindClassException
*/
public function withDatabase()
{
$this->import(DatabasesProviders::class);
}
2021-07-08 18:02:21 +08:00
2021-08-11 10:28:47 +08:00
/**
* @throws NotFindClassException
*/
public function withCrontab()
{
$this->import(CrontabProviders::class);
}
2021-08-19 17:41:10 +08:00
/**
* @param string $class
* @param Process $process
*/
public function addProcess(string $class, Process $process)
{
2021-08-19 18:46:28 +08:00
if (!isset($this->_process[$class])) {
2021-08-19 18:27:19 +08:00
$this->_process[$class] = [];
}
$this->_process[$class][] = $process;
2021-08-19 17:41:10 +08:00
}
/**
* @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-08-19 15:22:48 +08:00
/**
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public function withFileChangeListen()
{
2021-08-19 15:40:28 +08:00
$manager = $this->getServer();
2021-08-19 15:22:48 +08:00
$manager->addProcess(FileChangeCustomProcess::class);
2021-08-19 15:36:59 +08:00
putenv('enableFileChange=on');
2021-08-19 15:22:48 +08:00
}
2021-08-11 10:27:39 +08:00
/**
* @param Closure|array $closure
* @return $this
* @throws Exception
*/
public function middleware(Closure|array $closure): static
{
$this->getRouter()->setMiddleware($closure);
return $this;
}
2021-04-25 01:34:05 +08:00
2021-08-11 10:27:39 +08:00
/**
* @param bool $useTree
* @return $this
* @throws Exception
*/
public function setUseTree(bool $useTree): static
{
$this->getRouter()->setUseTree($useTree);
return $this;
}
2021-04-25 01:34:05 +08:00
2021-08-11 10:27:39 +08:00
/**
* @param string $service
* @return $this
* @throws
*/
public function import(string $service): static
{
if (!class_exists($service)) {
throw new NotFindClassException($service);
}
$class = Kiri::getDi()->get($service);
if (method_exists($class, 'onImport')) {
$class->onImport($this);
}
return $this;
}
2021-04-25 01:34:05 +08:00
2021-07-13 11:28:12 +08:00
/**
* @param Kernel $kernel
* @return $this
*/
2021-08-11 10:27:39 +08:00
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)
{
/** @var Console $abstracts */
$abstracts = $this->get('console');
$abstracts->register($command);
}
/**
* @param Input $argv
* @return void
* @throws Exception
*/
2021-08-19 15:36:59 +08:00
public function execute(Input $argv): void
2021-08-11 10:27:39 +08:00
{
try {
2021-08-19 15:47:12 +08:00
$this->register(Runtime::class);
2021-08-11 10:27:39 +08:00
$manager = Kiri::app()->get('console');
$class = $manager->setParameters($argv)->search();
2021-08-17 18:19:52 +08:00
2021-08-19 15:36:59 +08:00
$this->enableFileChange($class);
2021-08-17 18:19:26 +08:00
$data = $this->getBuilder($manager->exec($class));
2021-08-11 10:27:39 +08:00
} catch (\Throwable $exception) {
2021-08-24 19:11:14 +08:00
$data = $this->getBuilder(jTraceEx($exception));
2021-08-11 10:27:39 +08:00
} finally {
print_r($data);
Timer::clearAll();
}
}
2021-08-17 18:19:26 +08:00
2021-08-19 15:36:59 +08:00
/**
* @throws NotFindClassException
* @throws ReflectionException
*/
private function enableFileChange($class): void
{
2021-08-20 19:07:10 +08:00
if (env('enableFileChange', 'off') == 'off' || !($class instanceof Command)) {
2021-08-19 15:36:59 +08:00
scan_directory(directory('app'), 'App');
}
}
2021-08-17 18:19:26 +08:00
/**
* @param $data
* @return Response|ResponseInterface
* @throws NotFindClassException
2021-08-19 15:22:48 +08:00
* @throws ReflectionException
2021-08-17 18:19:26 +08:00
* @throws Exception
*/
private function getBuilder($data): Response|ResponseInterface
{
return di(Response::class)->getBuilder($data);
}
2021-08-11 10:27:39 +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
}