改名
This commit is contained in:
@@ -20,7 +20,7 @@ class Node extends Application
|
|||||||
|
|
||||||
public $path;
|
public $path;
|
||||||
public $index = 0;
|
public $index = 0;
|
||||||
public $method = [];
|
public $method;
|
||||||
|
|
||||||
/** @var Node[] $childes */
|
/** @var Node[] $childes */
|
||||||
public $childes = [];
|
public $childes = [];
|
||||||
@@ -31,7 +31,7 @@ class Node extends Application
|
|||||||
private $_error = '';
|
private $_error = '';
|
||||||
|
|
||||||
public $rules = [];
|
public $rules = [];
|
||||||
public $handler = [];
|
public $handler;
|
||||||
public $htmlSuffix = '.html';
|
public $htmlSuffix = '.html';
|
||||||
public $enableHtmlSuffix = false;
|
public $enableHtmlSuffix = false;
|
||||||
public $namespace = [];
|
public $namespace = [];
|
||||||
@@ -42,24 +42,23 @@ class Node extends Application
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $handler
|
* @param $handler
|
||||||
* @param $method
|
|
||||||
* @return Node
|
* @return Node
|
||||||
* @throws Exception
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function bindHandler($handler, $method)
|
public function bindHandler($handler)
|
||||||
{
|
{
|
||||||
if ($handler instanceof Closure) {
|
if ($handler instanceof Closure) {
|
||||||
$this->handler[$method] = $handler;
|
$this->handler = $handler;
|
||||||
} else if (is_string($handler) && strpos($handler, '@') !== false) {
|
} else if (is_string($handler) && strpos($handler, '@') !== false) {
|
||||||
list($controller, $action) = explode('@', $handler);
|
list($controller, $action) = explode('@', $handler);
|
||||||
if (!empty($this->namespace)) {
|
if (!empty($this->namespace)) {
|
||||||
$controller = implode('\\', $this->namespace) . '\\' . $controller;
|
$controller = implode('\\', $this->namespace) . '\\' . $controller;
|
||||||
}
|
}
|
||||||
$this->handler[$method] = $this->getReflect($controller, $action);
|
$this->handler = $this->getReflect($controller, $action);
|
||||||
} else if ($handler != null && !is_callable($handler, true)) {
|
} else if ($handler != null && !is_callable($handler, true)) {
|
||||||
$this->_error = 'Controller is con\'t exec.';
|
$this->_error = 'Controller is con\'t exec.';
|
||||||
} else {
|
} else {
|
||||||
$this->handler[$method] = $handler;
|
$this->handler = $handler;
|
||||||
}
|
}
|
||||||
return $this->restructure();
|
return $this->restructure();
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-21
@@ -6,7 +6,6 @@ namespace HttpServer\Route;
|
|||||||
use Closure;
|
use Closure;
|
||||||
use Exception;
|
use Exception;
|
||||||
use HttpServer\Http\Context;
|
use HttpServer\Http\Context;
|
||||||
use HttpServer\Http\Request;
|
|
||||||
use HttpServer\IInterface\RouterInterface;
|
use HttpServer\IInterface\RouterInterface;
|
||||||
use HttpServer\Application;
|
use HttpServer\Application;
|
||||||
use HttpServer\Route\Annotation\Annotation;
|
use HttpServer\Route\Annotation\Annotation;
|
||||||
@@ -47,19 +46,19 @@ class Router extends Application implements RouterInterface
|
|||||||
*/
|
*/
|
||||||
public function addRoute($path, $handler, $method = 'any')
|
public function addRoute($path, $handler, $method = 'any')
|
||||||
{
|
{
|
||||||
if (!isset($this->nodes[$path])) {
|
if (!isset($this->nodes[$method])) {
|
||||||
$this->nodes[$path] = [];
|
$this->nodes[$method] = [];
|
||||||
}
|
}
|
||||||
list($first, $explode) = $this->split($path);
|
list($first, $explode) = $this->split($path);
|
||||||
$parent = $this->nodes[$first] ?? null;
|
$parent = $this->nodes[$method][$first] ?? null;
|
||||||
if (empty($parent)) {
|
if (empty($parent)) {
|
||||||
$parent = $this->NodeInstance($first, 0, $method);
|
$parent = $this->NodeInstance($first, 0, $method);
|
||||||
$this->nodes[$first] = $parent;
|
$this->nodes[$method][$first] = $parent;
|
||||||
}
|
}
|
||||||
if ($first !== '/') {
|
if ($first !== '/') {
|
||||||
$parent = $this->bindNode($parent, $explode, $method);
|
$parent = $this->bindNode($parent, $explode, $method);
|
||||||
}
|
}
|
||||||
return $parent->bindHandler($handler, $method);
|
return $parent->bindHandler($handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -221,7 +220,7 @@ class Router extends Application implements RouterInterface
|
|||||||
$node->childes = [];
|
$node->childes = [];
|
||||||
$node->path = $value;
|
$node->path = $value;
|
||||||
$node->index = $index;
|
$node->index = $index;
|
||||||
$node->method = [$method];
|
$node->method = $method;
|
||||||
|
|
||||||
$name = array_column($this->groupTacks, 'namespace');
|
$name = array_column($this->groupTacks, 'namespace');
|
||||||
|
|
||||||
@@ -295,16 +294,17 @@ class Router extends Application implements RouterInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $explode
|
* @param array $explode
|
||||||
|
* @param $method
|
||||||
* @return Node|null
|
* @return Node|null
|
||||||
* 查找指定路由
|
* 查找指定路由
|
||||||
*/
|
*/
|
||||||
public function tree_search($explode)
|
public function tree_search($explode, $method)
|
||||||
{
|
{
|
||||||
if (empty($explode)) {
|
if (empty($explode)) {
|
||||||
return $this->nodes['/'] ?? null;
|
return $this->nodes[$method]['/'] ?? null;
|
||||||
}
|
}
|
||||||
$first = array_shift($explode);
|
$first = array_shift($explode);
|
||||||
if (!($parent = $this->nodes[$first] ?? null)) {
|
if (!($parent = $this->nodes[$method][$first] ?? null)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (empty($explode)) {
|
if (empty($explode)) {
|
||||||
@@ -357,7 +357,7 @@ class Router extends Application implements RouterInterface
|
|||||||
if ($_node->path == '/') {
|
if ($_node->path == '/') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$path = strtoupper(implode('_', $_node->method)) . ' : ' . $_node->path;
|
$path = strtoupper($_node->method) . ' : ' . $_node->path;
|
||||||
if (!empty($_node->childes)) {
|
if (!empty($_node->childes)) {
|
||||||
$path = $this->readByChild($_node->childes, $path);
|
$path = $this->readByChild($_node->childes, $path);
|
||||||
}
|
}
|
||||||
@@ -408,7 +408,7 @@ class Router extends Application implements RouterInterface
|
|||||||
} else {
|
} else {
|
||||||
[$first, $route] = explode(' : ', $paths);
|
[$first, $route] = explode(' : ', $paths);
|
||||||
|
|
||||||
$newPath[] = strtoupper(implode('_', $item->method)) . ' : ' . $route . '/' . $item->path;
|
$newPath[] = strtoupper($item->method) . ' : ' . $route . '/' . $item->path;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -421,17 +421,13 @@ class Router extends Application implements RouterInterface
|
|||||||
*/
|
*/
|
||||||
public function dispatch()
|
public function dispatch()
|
||||||
{
|
{
|
||||||
/** @var Request $request */
|
|
||||||
$request = Context::getContext('request');
|
$request = Context::getContext('request');
|
||||||
if (!($node = $this->find_path($request))) {
|
if (!($node = $this->find_path($request))) {
|
||||||
|
return JSON::to(404, 'Page not found or method not allowed.');
|
||||||
|
}
|
||||||
|
if (empty($node->callback)) {
|
||||||
return JSON::to(404, 'Page not found.');
|
return JSON::to(404, 'Page not found.');
|
||||||
}
|
}
|
||||||
if (!in_array($request->getMethod(), $node->method)) {
|
|
||||||
return JSON::to(405, 'Method not allowed.');
|
|
||||||
}
|
|
||||||
if (empty($node->callback) || isset($node->callback[$request->getMethod()])) {
|
|
||||||
return JSON::to(404, 'Method does not exist.');
|
|
||||||
}
|
|
||||||
return call_user_func($node->callback, $request);
|
return call_user_func($node->callback, $request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -441,14 +437,14 @@ class Router extends Application implements RouterInterface
|
|||||||
*/
|
*/
|
||||||
private function find_path($request)
|
private function find_path($request)
|
||||||
{
|
{
|
||||||
$node = $this->tree_search($request->getExplode());
|
$node = $this->tree_search($request->getExplode(), $request->getMethod());
|
||||||
if ($node instanceof Node) {
|
if ($node instanceof Node) {
|
||||||
return $node;
|
return $node;
|
||||||
}
|
}
|
||||||
if (!$request->isOption) {
|
if (!$request->isOption) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$node = $this->tree_search(['*']);
|
$node = $this->tree_search(['*'], $request->getMethod());
|
||||||
if (!($node instanceof Node)) {
|
if (!($node instanceof Node)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user