This commit is contained in:
as2252258@163.com
2021-04-25 01:34:05 +08:00
parent cdd28a8c43
commit 722699203a
+108 -110
View File
@@ -27,6 +27,7 @@ use Snowflake\Exception\NotFindClassException;
use stdClass; use stdClass;
use Swoole\Timer; use Swoole\Timer;
use Wchat\WchatProviders; use Wchat\WchatProviders;
use function Co\run;
/** /**
* Class Init * Class Init
@@ -39,132 +40,129 @@ use Wchat\WchatProviders;
class Application extends BaseApplication class Application extends BaseApplication
{ {
/** /**
* @var string * @var string
*/ */
public string $id = 'uniqueId'; public string $id = 'uniqueId';
public string $state = ''; public string $state = '';
/** /**
* @throws NotFindClassException * @throws NotFindClassException
*/ */
public function init() public function init()
{ {
$this->import(ConsoleProviders::class); $this->import(ConsoleProviders::class);
$this->import(DatabasesProviders::class); $this->import(DatabasesProviders::class);
$this->import(ServerProviders::class); $this->import(ServerProviders::class);
$this->import(CrontabProviders::class); $this->import(CrontabProviders::class);
} }
/** /**
* @param Closure|array $closure * @param Closure|array $closure
* @return $this * @return $this
* @throws Exception * @throws Exception
*/ */
public function middleware(Closure|array $closure): static public function middleware(Closure|array $closure): static
{ {
$this->getRouter()->setMiddleware($closure); $this->getRouter()->setMiddleware($closure);
return $this; return $this;
} }
/** /**
* @param bool $useTree * @param bool $useTree
* @return $this * @return $this
* @throws Exception * @throws Exception
*/ */
public function setUseTree(bool $useTree): static public function setUseTree(bool $useTree): static
{ {
$this->getRouter()->setUseTree($useTree); $this->getRouter()->setUseTree($useTree);
return $this; return $this;
} }
/** /**
* @param string $service * @param string $service
* @return $this * @return $this
* @throws * @throws
*/ */
public function import(string $service): static public function import(string $service): static
{ {
if (!class_exists($service)) { if (!class_exists($service)) {
throw new NotFindClassException($service); throw new NotFindClassException($service);
} }
$class = Snowflake::createObject($service); $class = Snowflake::createObject($service);
if (method_exists($class, 'onImport')) { if (method_exists($class, 'onImport')) {
$class->onImport($this); $class->onImport($this);
} }
return $this; return $this;
} }
/** /**
* @param $kernel * @param $kernel
* @return $this * @return $this
*/ */
public function commands(Kernel $kernel): static public function commands(Kernel $kernel): static
{ {
foreach ($kernel->getCommands() as $command) { foreach ($kernel->getCommands() as $command) {
$this->register($command); $this->register($command);
} }
return $this; return $this;
} }
/** /**
* @param string $command * @param string $command
* @throws * @throws
*/ */
public function register(string $command) public function register(string $command)
{ {
/** @var Console $abstracts */ /** @var Console $abstracts */
$abstracts = $this->get('console'); $abstracts = $this->get('console');
$abstracts->register($command); $abstracts->register($command);
} }
/** /**
* @param Input $argv * @param Input $argv
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
public function start(Input $argv): void public function start(Input $argv): void
{ {
try { run(function () use ($argv) {
/** @var Console $manager */ try {
$manager = Snowflake::app()->get('console'); /** @var Console $manager */
$manager->register(Runtime::class); $manager = Snowflake::app()->get('console');
$manager->setParameters($argv); $manager->register(Runtime::class);
$class = $manager->search(); $manager->setParameters($argv);
if (!($class instanceof Command)) { $class = $manager->search();
scan_directory(directory('app'), 'App'); if (!($class instanceof Command)) {
} scan_directory(directory('app'), 'App');
response()->send($manager->execCommand($class)); }
} catch (\Throwable $exception) { response()->send($manager->execCommand($class));
var_export(current($exception->getTrace())); } catch (\Throwable $exception) {
response()->send(implode("\n", [ response()->send(logger()->exception($exception));
'Msg: ' . $exception->getMessage(), } finally {
'Line: ' . $exception->getLine(), Timer::clearAll();
'File: ' . $exception->getFile() }
])); });
} finally { }
Timer::clearAll();
}
}
/** /**
* @param $className * @param $className
* @param null $abstracts * @param null $abstracts
* @return stdClass * @return stdClass
* @throws Exception * @throws Exception
*/ */
public function make($className, $abstracts = null): stdClass public function make($className, $abstracts = null): stdClass
{ {
return make($className, $abstracts); return make($className, $abstracts);
} }
} }