Files
kiri-core/System/Application.php
T

179 lines
3.1 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-04-08 11:24:31 +08:00
use HttpServer\Command;
2021-08-04 16:27:23 +08:00
use HttpServer\Http\Response;
2020-09-02 23:41:29 +08:00
use HttpServer\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;
2020-12-17 14:09:14 +08:00
use stdClass;
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-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-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
*/
public function start(Input $argv): void
{
try {
/** @var Console $manager */
$manager = Kiri::app()->get('console');
$manager->register(Runtime::class);
$class = $manager->setParameters($argv)->search();
if (!($class instanceof Command)) {
scan_directory(directory('app'), 'App');
}
$data = di(Response::class)->getBuilder($manager->execCommand($class));
} catch (\Throwable $exception) {
$data = di(Response::class)->getBuilder(logger()->exception($exception));
} finally {
print_r($data);
Timer::clearAll();
}
}
/**
* @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
}