Files
kiri-core/System/Application.php
T

140 lines
2.9 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-03-29 16:27:12 +08:00
use Annotation\Aspect;
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;
2021-03-29 16:27:12 +08:00
use Database\InjectProperty;
2020-08-31 01:27:08 +08:00
use Exception;
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-03-26 02:12:34 +08:00
/**
* @var string
*/
public string $id = 'uniqueId';
public string $state = '';
/**
* @throws NotFindClassException
*/
2021-03-29 17:00:53 +08:00
public function init()
2021-03-26 02:12:34 +08:00
{
$this->import(ConsoleProviders::class);
$this->import(DatabasesProviders::class);
$this->import(ServerProviders::class);
2021-03-26 02:13:18 +08:00
$this->import(CrontabProviders::class);
2021-03-26 02:12:34 +08:00
}
/**
* @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;
}
/**
* @param $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)
{
/** @var Console $abstracts */
$abstracts = $this->get('console');
$abstracts->register($command);
}
/**
* @param Input $argv
* @return void
* @throws Exception
*/
public function start(Input $argv): void
{
try {
fire(Event::SERVER_BEFORE_START);
$manager = Snowflake::app()->get('console');
$manager->setParameters($argv);
$class = $manager->search();
response()->send($manager->execCommand($class));
} catch (\Throwable $exception) {
response()->send(implode("\n", [
'Msg: ' . $exception->getMessage(),
'Line: ' . $exception->getLine(),
'File: ' . $exception->getFile()
]));
} finally {
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
}