Files
kiri-core/System/Application.php
T

160 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;
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;
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
/**
* @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
* @return bool|string
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public function start(Input $argv): bool|string
2020-08-31 01:27:08 +08:00
{
2020-09-08 10:55:06 +08:00
try {
2021-02-23 00:29:13 +08:00
// $this->scan_system_annotation();
2021-02-22 18:12:36 +08:00
2021-02-22 17:44:24 +08:00
fire(Event::SERVER_BEFORE_START);
$this->set('input', $argv);
2020-09-08 10:55:06 +08:00
$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-12-17 14:09:14 +08:00
return response()->send($manager->execCommand($class));
2020-09-08 10:58:09 +08:00
} catch (\Throwable $exception) {
2020-12-17 14:09:14 +08:00
return 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
}
2021-02-22 18:12:36 +08:00
/**
* @throws ReflectionException
* @throws ComponentException
* @throws NotFindPropertyException
*/
public function scan_system_annotation()
{
2021-02-22 18:31:25 +08:00
$this->debug('scan system files...');
2021-02-22 18:12:36 +08:00
$annotation = Snowflake::app()->getAttributes();
2021-02-22 18:17:43 +08:00
$annotation->readControllers(__DIR__ . '/../Console/', 'Console', 'system');
$annotation->readControllers(__DIR__ . '/../Database/', 'Database', 'system');
$annotation->readControllers(__DIR__ . '/../Gii/', 'Gii', 'system');
$annotation->readControllers(__DIR__ . '/../HttpServer/', 'HttpServer', 'system');
$annotation->readControllers(__DIR__ . '/../Kafka/', 'Kafka', 'system');
$annotation->readControllers(__DIR__ . '/../System/', 'Snowflake', 'system');
$annotation->readControllers(__DIR__ . '/../Validator/', 'Validator', 'system');
2021-02-22 18:12:36 +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
}
}