Skip to content

Commit

Permalink
update some for http route
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 9, 2020
1 parent d5d0ad6 commit 0366bd9
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ php:
- 7.1
- 7.2
- 7.3
- 7.4

services:
- mysql
- redis-server
- redis

before_install:
- sudo mysql -e "use mysql; update user set authentication_string=PASSWORD('swoft123456') where User='root'; update user set plugin='mysql_native_password';FLUSH PRIVILEGES;"
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/src/Contract/RouterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function addRoute(Route $route): Route;
* 'schemas' => ['https'],
* ]
*/
public function map($methods, string $path, $handler, array $binds = [], array $opts = []);
public function map($methods, string $path, $handler, array $binds = [], array $opts = []): void;

/**
* find the matched route info for the given request uri path
Expand Down
33 changes: 20 additions & 13 deletions src/http-server/src/Router/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
* @contact [email protected]
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Http\Server\Router;

use ArrayIterator;
use IteratorAggregate;
use LogicException;
use Traversable;
use function array_merge;
use function array_shift;
use ArrayIterator;
use function count;
use function get_class;
use function is_array;
use function is_object;
use function is_string;
use IteratorAggregate;
use LogicException;
use function preg_match;
use function preg_match_all;
use function property_exists;
Expand All @@ -31,7 +33,6 @@
use function strtr;
use function substr;
use function substr_count;
use Traversable;
use function trim;

/**
Expand Down Expand Up @@ -64,13 +65,15 @@ final class Route implements IteratorAggregate
/**
* map where parameter binds.
* [param name => regular expression path (or symbol name)]
*
* @var string[]
*/
private $bindVars;

/**
* dynamic route param values, only use for route cache
* [param name => value]
*
* @var string[]
*/
private $params = [];
Expand All @@ -79,6 +82,7 @@ final class Route implements IteratorAggregate

/**
* path var names.
*
* @var array '/users/{id}' => ['id']
*/
private $pathVars = [];
Expand All @@ -91,6 +95,7 @@ final class Route implements IteratorAggregate
/**
* '/users/{id}' -> '/users/'
* '/blog/post-{id}' -> '/blog/post-'
*
* @var string
*/
private $pathStart = '';
Expand All @@ -99,12 +104,14 @@ final class Route implements IteratorAggregate

/**
* middleware handler chains
*
* @var callable[]
*/
private $chains = [];

/**
* some custom route options data.
*
* @var array
*/
private $options;
Expand Down Expand Up @@ -342,7 +349,8 @@ public function match(string $path): array
*/
public function copyWithParams(array $params): self
{
$route = clone $this;
$route = clone $this;
// set params
$route->params = $params;

return $route;
Expand All @@ -366,11 +374,12 @@ public function middleware(...$middleware): self

/**
* alias of the method: middleware()
* @see middleware()
*
* @param mixed ...$middleware
*
* @return Route
* @see middleware()
*
*/
public function push(...$middleware): self
{
Expand Down Expand Up @@ -405,6 +414,7 @@ public function toUri(array $pathVars = []): string

/**
* get basic info data
*
* @return array
*/
public function info(): array
Expand All @@ -418,6 +428,7 @@ public function info(): array

/**
* get all info data
*
* @return array
*/
public function toArray(): array
Expand Down Expand Up @@ -452,13 +463,8 @@ public function __toString(): string
*/
public function toString(): string
{
return sprintf(
'%-7s %-25s --> %s (%d middleware)',
$this->method,
$this->path,
$this->getHandlerName(),
count($this->chains)
);
return sprintf('%-7s %-25s --> %s (%d middleware)', $this->method, $this->path, $this->getHandlerName(),
count($this->chains));
}

/**
Expand Down Expand Up @@ -512,6 +518,7 @@ public function setOptions(array $options): self

/**
* Retrieve an external iterator
*
* @link https://php.net/manual/en/iteratoraggregate.getiterator.php
* @return Traversable An instance of an object implementing <b>Iterator</b> or <b>Traversable</b>
* @since 5.0.0
Expand Down
75 changes: 62 additions & 13 deletions src/http-server/src/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,13 @@ public function middleware(...$middleware): Router

/**
* register a route, allow GET request method.
* {@inheritdoc}
*
* @param string $path
* @param mixed $handler
* @param array $pathParams
* @param array $opts
*
* @return Route
*/
public function get(string $path, $handler, array $pathParams = [], array $opts = []): Route
{
Expand All @@ -194,7 +200,13 @@ public function get(string $path, $handler, array $pathParams = [], array $opts

/**
* register a route, allow POST request method.
* {@inheritdoc}
*
* @param string $path
* @param mixed $handler
* @param array $pathParams
* @param array $opts
*
* @return Route
*/
public function post(string $path, $handler, array $pathParams = [], array $opts = []): Route
{
Expand All @@ -203,7 +215,13 @@ public function post(string $path, $handler, array $pathParams = [], array $opts

/**
* register a route, allow PUT request method.
* {@inheritdoc}
*
* @param string $path
* @param mixed $handler
* @param array $pathParams
* @param array $opts
*
* @return Route
*/
public function put(string $path, $handler, array $pathParams = [], array $opts = []): Route
{
Expand All @@ -212,7 +230,13 @@ public function put(string $path, $handler, array $pathParams = [], array $opts

/**
* register a route, allow PATCH request method.
* {@inheritdoc}
*
* @param string $path
* @param mixed $handler
* @param array $pathParams
* @param array $opts
*
* @return Route
*/
public function patch(string $path, $handler, array $pathParams = [], array $opts = []): Route
{
Expand All @@ -221,7 +245,13 @@ public function patch(string $path, $handler, array $pathParams = [], array $opt

/**
* register a route, allow DELETE request method.
* {@inheritdoc}
*
* @param string $path
* @param mixed $handler
* @param array $pathParams
* @param array $opts
*
* @return Route
*/
public function delete(string $path, $handler, array $pathParams = [], array $opts = []): Route
{
Expand All @@ -230,7 +260,13 @@ public function delete(string $path, $handler, array $pathParams = [], array $op

/**
* register a route, allow HEAD request method.
* {@inheritdoc}
*
* @param string $path
* @param mixed $handler
* @param array $pathParams
* @param array $opts
*
* @return Route
*/
public function head(string $path, $handler, array $pathParams = [], array $opts = []): Route
{
Expand All @@ -239,7 +275,13 @@ public function head(string $path, $handler, array $pathParams = [], array $opts

/**
* register a route, allow OPTIONS request method.
* {@inheritdoc}
*
* @param string $path
* @param mixed $handler
* @param array $pathParams
* @param array $opts
*
* @return Route
*/
public function options(string $path, $handler, array $pathParams = [], array $opts = []): Route
{
Expand All @@ -248,7 +290,13 @@ public function options(string $path, $handler, array $pathParams = [], array $o

/**
* register a route, allow CONNECT request method.
* {@inheritdoc}
*
* @param string $path
* @param mixed $handler
* @param array $pathParams
* @param array $opts
*
* @return Route
*/
public function connect(string $path, $handler, array $pathParams = [], array $opts = []): Route
{
Expand All @@ -257,7 +305,11 @@ public function connect(string $path, $handler, array $pathParams = [], array $o

/**
* register a route, allow any request METHOD.
* {@inheritdoc}
*
* @param string $path
* @param mixed $handler
* @param array $pathParams
* @param array $opts
*/
public function any(string $path, $handler, array $pathParams = [], array $opts = []): void
{
Expand All @@ -271,7 +323,7 @@ public function any(string $path, $handler, array $pathParams = [], array $opts
* @param array $pathParams
* @param array $opts
*/
public function map($methods, string $path, $handler, array $pathParams = [], array $opts = [])
public function map($methods, string $path, $handler, array $pathParams = [], array $opts = []): void
{
foreach ((array)$methods as $method) {
$this->add($method, $path, $handler, $pathParams, $opts);
Expand All @@ -294,7 +346,6 @@ public function add(string $method, string $path, $handler, array $pathParams =
}

$method = strtoupper($method);

if ($method === 'ANY') {
$this->any($path, $handler, $pathParams, $opts);
return Route::createFromArray();
Expand Down Expand Up @@ -653,7 +704,6 @@ public function count(): int
*/
public function each(Closure $func): void
{
/** @var Route $route */
foreach ($this->staticRoutes as $route) {
$func($route);
}
Expand Down Expand Up @@ -726,7 +776,6 @@ public function toString(callable $filter = null): string
$strings = ['# Routes Number: ' . $this->count()];
$strings[] = "\n# Static Routes:";

/** @var Route $route */
foreach ($this->staticRoutes as $route) {
$routeString = $route->toString();

Expand Down

0 comments on commit 0366bd9

Please sign in to comment.