Files
kiri-core/System/Application.php
T

166 lines
3.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
namespace Snowflake;
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;
2020-09-02 23:41:29 +08:00
use HttpServer\ServerProviders;
2020-08-31 01:27:08 +08:00
use Snowflake\Abstracts\BaseApplication;
2020-09-04 01:10:11 +08:00
use Snowflake\Abstracts\Config;
2020-09-07 15:47:13 +08:00
use Snowflake\Abstracts\Input;
2020-11-06 15:55:17 +08:00
use Snowflake\Abstracts\Kernel;
2021-03-26 02:12:34 +08:00
use Snowflake\Crontab\CrontabProviders;
2020-09-02 18:15:22 +08:00
use Snowflake\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
*
2020-08-31 12:38:32 +08:00
* @package Snowflake
2020-08-31 01:27:08 +08:00
*
* @property-read Config $config
*/
class Application extends BaseApplication
{
2021-04-25 01:34:05 +08:00
/**
* @var string
*/
public string $id = 'uniqueId';
public string $state = '';
/**
* @throws NotFindClassException
*/
public function init()
{
$this->import(ConsoleProviders::class);
$this->import(DatabasesProviders::class);
$this->import(ServerProviders::class);
$this->import(CrontabProviders::class);
}
2021-07-08 18:02:21 +08:00
2021-04-25 01:34:05 +08:00
/**
* @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)) {
throw new NotFindClassException($service);
}
$class = Snowflake::createObject($service);
if (method_exists($class, 'onImport')) {
$class->onImport($this);
}
return $this;
}
2021-07-13 11:28:12 +08:00
/**
* @param Kernel $kernel
* @return $this
*/
2021-04-25 01:34:05 +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
{
2021-04-25 01:36:22 +08:00
try {
/** @var Console $manager */
$manager = Snowflake::app()->get('console');
$manager->register(Runtime::class);
$manager->setParameters($argv);
$class = $manager->search();
if (!($class instanceof Command)) {
scan_directory(directory('app'), 'App');
2021-04-25 01:34:05 +08:00
}
2021-04-25 01:36:22 +08:00
response()->send($manager->execCommand($class));
} catch (\Throwable $exception) {
response()->send(logger()->exception($exception));
} finally {
Timer::clearAll();
}
2021-04-25 01:34:05 +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
}