Files
kiri-core/System/Application.php
T

137 lines
2.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
namespace Snowflake;
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;
2020-09-02 23:41:29 +08:00
use HttpServer\ServerProviders;
2020-09-10 19:26:42 +08:00
use Queue\QueueProviders;
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;
2020-09-02 18:15:22 +08:00
use Snowflake\Exception\NotFindClassException;
2020-09-10 19:26:42 +08:00
use Snowflake\Exception\ConfigException;
2020-09-18 16:59:46 +08:00
use Swoole\Coroutine;
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
{
/**
* @var string
*/
2020-10-29 18:17:25 +08:00
public string $id = 'uniqueId';
2020-08-31 01:27:08 +08:00
2020-09-02 23:41:29 +08:00
/**
2020-09-10 19:26:42 +08:00
* @throws ConfigException
2020-09-02 23:41:29 +08:00
* @throws NotFindClassException
*/
public function init()
{
2020-09-03 00:15:57 +08:00
$this->import(ConsoleProviders::class);
2020-09-02 23:41:29 +08:00
$this->import(DatabasesProviders::class);
$this->import(ServerProviders::class);
2020-09-10 19:26:42 +08:00
if (Config::get('queue.enable', false, false)) {
$this->import(QueueProviders::class);
}
2020-09-02 23:41:29 +08:00
}
2020-08-31 14:02:54 +08:00
/**
2020-09-02 18:15:22 +08:00
* @param string $service
* @return $this
2020-09-02 18:15:39 +08:00
* @throws
2020-08-31 14:02:54 +08:00
*/
2020-09-02 18:15:22 +08:00
public function import(string $service)
2020-08-31 14:02:54 +08:00
{
2020-09-02 18:15:22 +08:00
if (!class_exists($service)) {
throw new NotFindClassException($service);
}
2020-09-02 18:22:16 +08:00
$class = Snowflake::createObject($service);
2020-08-31 14:02:54 +08:00
if (method_exists($class, 'onImport')) {
$class->onImport($this);
}
return $this;
}
2020-11-06 15:55:17 +08:00
/**
* @param $kernel
* @return $this
*/
public function commands(Kernel $kernel)
{
foreach ($kernel->getCommands() as $command) {
$this->register($command);
}
return $this;
}
2020-09-07 18:11:36 +08:00
/**
* @param string $command
2020-09-08 14:28:02 +08:00
* @throws
2020-09-07 18:11:36 +08:00
*/
2020-09-08 10:55:06 +08:00
public function register(string $command)
2020-09-07 18:11:36 +08:00
{
2020-09-08 10:55:06 +08:00
/** @var Console $abstracts */
2020-09-07 18:11:36 +08:00
$abstracts = $this->get('console');
$abstracts->register($command);
}
2020-08-31 01:27:08 +08:00
/**
2020-09-07 11:01:11 +08:00
* @param $argv
2020-09-18 16:59:46 +08:00
* @return bool|string|void
2020-08-31 14:07:20 +08:00
* @throws
2020-08-31 01:27:08 +08:00
*/
2020-09-07 15:47:13 +08:00
public function start(Input $argv)
2020-08-31 01:27:08 +08:00
{
2020-09-07 18:35:56 +08:00
$this->set('input', $argv);
2020-09-08 10:55:06 +08:00
try {
$manager = Snowflake::app()->get('console');
2020-09-08 10:58:09 +08:00
$manager->setParameters($argv);
2020-09-08 10:55:06 +08:00
$class = $manager->search();
2020-09-18 17:09:13 +08:00
response()->send($manager->execCommand($class));
2020-09-08 10:58:09 +08:00
} catch (\Throwable $exception) {
2020-09-18 16:59:46 +08:00
response()->send(implode("\n", [
2020-09-08 10:55:06 +08:00
'Msg: ' . $exception->getMessage(),
'Line: ' . $exception->getLine(),
'File: ' . $exception->getFile()
]));
} finally {
Timer::clearAll();
2020-09-07 15:47:13 +08:00
}
2020-08-31 01:27:08 +08:00
}
/**
* @param $className
* @param null $abstracts
* @return mixed
* @throws Exception
*/
public function make($className, $abstracts = null)
{
2020-08-31 12:38:32 +08:00
return make($className, $abstracts);
2020-08-31 01:27:08 +08:00
}
}