This commit is contained in:
2021-04-02 18:57:56 +08:00
parent 0186bbc33d
commit d46762abbb
2 changed files with 116 additions and 92 deletions
+6 -7
View File
@@ -43,7 +43,7 @@ class Router extends HttpService implements RouterInterface
public ?Closure $middleware = null; public ?Closure $middleware = null;
public bool $useTree = false; public int $useTree = ROUTER_TREE;
/** /**
@@ -64,12 +64,13 @@ class Router extends HttpService implements RouterInterface
$this->dir = Config::get('http.namespace', false, $this->dir); $this->dir = Config::get('http.namespace', false, $this->dir);
} }
/** /**
* @param bool $useTree * @param bool $useTree
*/ */
public function setUseTree(bool $useTree): void public function setUseTree(bool $useTree): void
{ {
$this->useTree = $useTree; $this->useTree = $useTree ? ROUTER_TREE : ROUTER_HASH;
} }
@@ -101,7 +102,6 @@ class Router extends HttpService implements RouterInterface
* @param $handler * @param $handler
* @param string $method * @param string $method
* @return ?Node * @return ?Node
* @throws ConfigException
*/ */
public function addRoute($path, $handler, $method = 'any'): ?Node public function addRoute($path, $handler, $method = 'any'): ?Node
{ {
@@ -110,8 +110,7 @@ class Router extends HttpService implements RouterInterface
$this->nodes[$method] = []; $this->nodes[$method] = [];
} }
$useTree = Config::get('router', false, ROUTER_TREE); if ($this->useTree == ROUTER_TREE) {
if ($useTree == ROUTER_TREE) {
return $this->tree($path, $handler, $method); return $this->tree($path, $handler, $method);
} else { } else {
return $this->hash($path, $handler, $method); return $this->hash($path, $handler, $method);
@@ -515,7 +514,7 @@ class Router extends HttpService implements RouterInterface
} }
return $node->afterDispatch($response); return $node->afterDispatch($response);
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$this->addError($exception,'throwable'); $this->addError($exception, 'throwable');
$Code = $exception->getCode() == 0 ? 500 : $exception->getCode(); $Code = $exception->getCode() == 0 ? 500 : $exception->getCode();
@@ -658,7 +657,7 @@ class Router extends HttpService implements RouterInterface
$router = $this; $router = $this;
include_once "{$files}"; include_once "{$files}";
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$this->addError($exception,'throwable'); $this->addError($exception, 'throwable');
} finally { } finally {
if (isset($exception)) { if (isset($exception)) {
unset($exception); unset($exception);
+110 -85
View File
@@ -11,6 +11,7 @@ namespace Snowflake;
use Annotation\Aspect; use Annotation\Aspect;
use Closure;
use Console\Console; use Console\Console;
use Console\ConsoleProviders; use Console\ConsoleProviders;
use Database\DatabasesProviders; use Database\DatabasesProviders;
@@ -36,104 +37,128 @@ 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(DatabasesProviders::class);
$this->import(ServerProviders::class); $this->import(ServerProviders::class);
$this->import(CrontabProviders::class); $this->import(CrontabProviders::class);
} }
/** /**
* @param string $service * @param Closure|array $closure
* @return $this * @return $this
* @throws * @throws Exception
*/ */
public function import(string $service): static public function middleware(Closure|array $closure): static
{ {
if (!class_exists($service)) { $this->getRouter()->setMiddleware($closure);
throw new NotFindClassException($service); return $this;
} }
$class = Snowflake::createObject($service);
if (method_exists($class, 'onImport')) {
$class->onImport($this);
}
return $this;
}
/** /**
* @param $kernel * @param bool $useTree
* @return $this * @return $this
*/ * @throws Exception
public function commands(Kernel $kernel): static */
{ public function setUseTree(bool $useTree): static
foreach ($kernel->getCommands() as $command) { {
$this->register($command); $this->getRouter()->setUseTree($useTree);
} return $this;
return $this; }
}
/** /**
* @param string $command * @param string $service
* @throws * @return $this
*/ * @throws
public function register(string $command) */
{ public function import(string $service): static
/** @var Console $abstracts */ {
$abstracts = $this->get('console'); if (!class_exists($service)) {
$abstracts->register($command); throw new NotFindClassException($service);
} }
$class = Snowflake::createObject($service);
if (method_exists($class, 'onImport')) {
$class->onImport($this);
}
return $this;
}
/** /**
* @param Input $argv * @param $kernel
* @return void * @return $this
* @throws Exception */
*/ public function commands(Kernel $kernel): static
public function start(Input $argv): void {
{ foreach ($kernel->getCommands() as $command) {
try { $this->register($command);
fire(Event::SERVER_BEFORE_START); }
return $this;
}
$manager = Snowflake::app()->get('console');
$manager->setParameters($argv);
$class = $manager->search();
response()->send($manager->execCommand($class));
} catch (\Throwable $exception) {
response()->send(implode("\n", [
'Msg: ' . $exception->getMessage(),
'Line: ' . $exception->getLine(),
'File: ' . $exception->getFile()
]));
} finally {
Timer::clearAll();
}
}
/** /**
* @param $className * @param string $command
* @param null $abstracts * @throws
* @return stdClass */
* @throws Exception public function register(string $command)
*/ {
public function make($className, $abstracts = null): stdClass /** @var Console $abstracts */
{ $abstracts = $this->get('console');
return make($className, $abstracts); $abstracts->register($command);
} }
/**
* @param Input $argv
* @return void
* @throws Exception
*/
public function start(Input $argv): void
{
try {
fire(Event::SERVER_BEFORE_START);
$manager = Snowflake::app()->get('console');
$manager->setParameters($argv);
$class = $manager->search();
response()->send($manager->execCommand($class));
} catch (\Throwable $exception) {
response()->send(implode("\n", [
'Msg: ' . $exception->getMessage(),
'Line: ' . $exception->getLine(),
'File: ' . $exception->getFile()
]));
} finally {
Timer::clearAll();
}
}
/**
* @param $className
* @param null $abstracts
* @return stdClass
* @throws Exception
*/
public function make($className, $abstracts = null): stdClass
{
return make($className, $abstracts);
}
} }