Files
kiri-core/System/Application.php
T

146 lines
2.7 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;
2021-02-22 18:12:36 +08:00
use ReflectionException;
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-02-22 18:12:36 +08:00
use Snowflake\Exception\ComponentException;
2020-09-02 18:15:22 +08:00
use Snowflake\Exception\NotFindClassException;
2021-02-22 18:12:36 +08:00
use Snowflake\Exception\NotFindPropertyException;
2020-12-17 14:09:14 +08:00
use stdClass;
2020-09-08 10:55:06 +08:00
use Swoole\Timer;
2021-03-02 13:49:06 +08:00
use function Co\run;
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
2021-03-02 18:51:45 +08:00
public string $state = '';
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-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-12-17 14:09:14 +08:00
public function import(string $service): static
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
*/
2020-12-17 14:09:14 +08:00
public function commands(Kernel $kernel): static
2020-11-06 15:55:17 +08:00
{
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-12-17 14:09:14 +08:00
* @param Input $argv
2021-03-02 13:49:06 +08:00
* @return void
2020-12-17 14:09:14 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
2021-03-02 13:49:06 +08:00
public function start(Input $argv): void
2020-08-31 01:27:08 +08:00
{
2020-09-08 10:55:06 +08:00
try {
2021-02-24 14:57:24 +08:00
ini_set('opcache.enable', '1');
ini_set('opcache.enable_cli', '1');
2021-02-24 14:57:00 +08:00
ini_set('opcache.jit_buffer_size', '100M');
2021-02-24 14:57:24 +08:00
ini_set('opcache.jit', '1255');
2021-02-22 18:12:36 +08:00
2021-02-22 17:44:24 +08:00
fire(Event::SERVER_BEFORE_START);
2021-03-02 13:49:53 +08:00
$this->set('input', $argv);
$manager = Snowflake::app()->get('console');
$manager->setParameters($argv);
$class = $manager->search();
response()->send($manager->execCommand($class));
2020-09-08 10:58:09 +08:00
} catch (\Throwable $exception) {
2021-03-02 13:49:06 +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
2020-12-17 14:09:14 +08:00
* @return stdClass
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function make($className, $abstracts = null): stdClass
2020-08-31 01:27:08 +08:00
{
2020-08-31 12:38:32 +08:00
return make($className, $abstracts);
2020-08-31 01:27:08 +08:00
}
}