This commit is contained in:
2021-08-11 10:27:39 +08:00
parent 3cea2e392e
commit 806bf80764
+107 -102
View File
@@ -37,130 +37,135 @@ use Swoole\Timer;
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(ServerProviders::class);
$this->import(ServerProviders::class);
$this->import(CrontabProviders::class); $this->import(CrontabProviders::class);
} }
/**
* @throws NotFindClassException
*/
public function withDatabase()
{
$this->import(DatabasesProviders::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 = Kiri::getDi()->get($service); $class = Kiri::getDi()->get($service);
if (method_exists($class, 'onImport')) { if (method_exists($class, 'onImport')) {
$class->onImport($this); $class->onImport($this);
} }
return $this; return $this;
} }
/** /**
* @param Kernel $kernel * @param Kernel $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 { try {
/** @var Console $manager */ /** @var Console $manager */
$manager = Kiri::app()->get('console'); $manager = Kiri::app()->get('console');
$manager->register(Runtime::class); $manager->register(Runtime::class);
$class = $manager->setParameters($argv)->search(); $class = $manager->setParameters($argv)->search();
if (!($class instanceof Command)) { if (!($class instanceof Command)) {
scan_directory(directory('app'), 'App'); scan_directory(directory('app'), 'App');
} }
$data = di(Response::class)->getBuilder($manager->execCommand($class)); $data = di(Response::class)->getBuilder($manager->execCommand($class));
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$data = di(Response::class)->getBuilder(logger()->exception($exception)); $data = di(Response::class)->getBuilder(logger()->exception($exception));
} finally { } finally {
print_r($data); print_r($data);
Timer::clearAll(); 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);
} }
} }