2022-08-19 19:48:37 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This file is part of webman.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under The MIT License
|
|
|
|
|
* For full copyright and license information, please see the MIT-LICENSE.txt
|
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
|
*
|
|
|
|
|
* @author walkor<walkor@workerman.net>
|
|
|
|
|
* @copyright walkor<walkor@workerman.net>
|
|
|
|
|
* @link http://www.workerman.net/
|
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Webman\Route;
|
|
|
|
|
|
|
|
|
|
use Webman\Route as Router;
|
2023-04-25 20:11:49 +08:00
|
|
|
use function array_merge;
|
|
|
|
|
use function count;
|
|
|
|
|
use function preg_replace_callback;
|
|
|
|
|
use function str_replace;
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Route
|
|
|
|
|
* @package Webman
|
|
|
|
|
*/
|
|
|
|
|
class Route
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @var string|null
|
|
|
|
|
*/
|
2023-04-25 20:11:49 +08:00
|
|
|
protected $name = null;
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2023-04-25 20:11:49 +08:00
|
|
|
protected $methods = [];
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2023-04-25 20:11:49 +08:00
|
|
|
protected $path = '';
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var callable
|
|
|
|
|
*/
|
2023-04-25 20:11:49 +08:00
|
|
|
protected $callback = null;
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2023-04-25 20:11:49 +08:00
|
|
|
protected $middlewares = [];
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2023-04-25 20:11:49 +08:00
|
|
|
protected $params = [];
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Route constructor.
|
|
|
|
|
* @param array $methods
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param callable $callback
|
|
|
|
|
*/
|
|
|
|
|
public function __construct($methods, string $path, $callback)
|
|
|
|
|
{
|
2023-04-25 20:11:49 +08:00
|
|
|
$this->methods = (array)$methods;
|
|
|
|
|
$this->path = $path;
|
|
|
|
|
$this->callback = $callback;
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-25 20:11:49 +08:00
|
|
|
* Get name.
|
|
|
|
|
* @return string|null
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2023-04-25 20:11:49 +08:00
|
|
|
public function getName(): ?string
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
2023-04-25 20:11:49 +08:00
|
|
|
return $this->name ?? null;
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-25 20:11:49 +08:00
|
|
|
* Name.
|
2022-08-19 19:48:37 +08:00
|
|
|
* @param string $name
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2023-04-25 20:11:49 +08:00
|
|
|
public function name(string $name): Route
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
2023-04-25 20:11:49 +08:00
|
|
|
$this->name = $name;
|
2022-08-19 19:48:37 +08:00
|
|
|
Router::setByName($name, $this);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-25 20:11:49 +08:00
|
|
|
* Middleware.
|
2022-08-19 19:48:37 +08:00
|
|
|
* @param mixed $middleware
|
|
|
|
|
* @return $this|array
|
|
|
|
|
*/
|
|
|
|
|
public function middleware($middleware = null)
|
|
|
|
|
{
|
|
|
|
|
if ($middleware === null) {
|
2023-04-25 20:11:49 +08:00
|
|
|
return $this->middlewares;
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
2023-04-25 20:11:49 +08:00
|
|
|
$this->middlewares = array_merge($this->middlewares, is_array($middleware) ? $middleware : [$middleware]);
|
2022-08-19 19:48:37 +08:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-25 20:11:49 +08:00
|
|
|
* GetPath.
|
2022-08-19 19:48:37 +08:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2023-04-25 20:11:49 +08:00
|
|
|
public function getPath(): string
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
2023-04-25 20:11:49 +08:00
|
|
|
return $this->path;
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-25 20:11:49 +08:00
|
|
|
* GetMethods.
|
2022-08-19 19:48:37 +08:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2023-04-25 20:11:49 +08:00
|
|
|
public function getMethods(): array
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
2023-04-25 20:11:49 +08:00
|
|
|
return $this->methods;
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-25 20:11:49 +08:00
|
|
|
* GetCallback.
|
|
|
|
|
* @return callable|null
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
|
|
|
|
public function getCallback()
|
|
|
|
|
{
|
2023-04-25 20:11:49 +08:00
|
|
|
return $this->callback;
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-25 20:11:49 +08:00
|
|
|
* GetMiddleware.
|
2022-08-19 19:48:37 +08:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2023-04-25 20:11:49 +08:00
|
|
|
public function getMiddleware(): array
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
2023-04-25 20:11:49 +08:00
|
|
|
return $this->middlewares;
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-25 20:11:49 +08:00
|
|
|
* Param.
|
2022-08-19 19:48:37 +08:00
|
|
|
* @param string|null $name
|
|
|
|
|
* @param $default
|
|
|
|
|
* @return array|mixed|null
|
|
|
|
|
*/
|
|
|
|
|
public function param(string $name = null, $default = null)
|
|
|
|
|
{
|
|
|
|
|
if ($name === null) {
|
2023-04-25 20:11:49 +08:00
|
|
|
return $this->params;
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
2023-04-25 20:11:49 +08:00
|
|
|
return $this->params[$name] ?? $default;
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-25 20:11:49 +08:00
|
|
|
* SetParams.
|
2022-08-19 19:48:37 +08:00
|
|
|
* @param array $params
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2023-04-25 20:11:49 +08:00
|
|
|
public function setParams(array $params): Route
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
2023-04-25 20:11:49 +08:00
|
|
|
$this->params = array_merge($this->params, $params);
|
2022-08-19 19:48:37 +08:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-25 20:11:49 +08:00
|
|
|
* Url.
|
|
|
|
|
* @param array $parameters
|
2022-08-19 19:48:37 +08:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2023-04-25 20:11:49 +08:00
|
|
|
public function url(array $parameters = []): string
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
|
|
|
|
if (empty($parameters)) {
|
2023-04-25 20:11:49 +08:00
|
|
|
return $this->path;
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
2023-04-25 20:11:49 +08:00
|
|
|
$path = str_replace(['[', ']'], '', $this->path);
|
|
|
|
|
$path = preg_replace_callback('/\{(.*?)(?:\:[^\}]*?)*?\}/', function ($matches) use (&$parameters) {
|
2022-08-19 19:48:37 +08:00
|
|
|
if (!$parameters) {
|
|
|
|
|
return $matches[0];
|
|
|
|
|
}
|
|
|
|
|
if (isset($parameters[$matches[1]])) {
|
|
|
|
|
$value = $parameters[$matches[1]];
|
|
|
|
|
unset($parameters[$matches[1]]);
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
$key = key($parameters);
|
|
|
|
|
if (is_int($key)) {
|
|
|
|
|
$value = $parameters[$key];
|
|
|
|
|
unset($parameters[$key]);
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
return $matches[0];
|
|
|
|
|
}, $path);
|
2023-04-25 20:11:49 +08:00
|
|
|
return count($parameters) > 0 ? $path . '?' . http_build_query($parameters) : $path;
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|