mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 00:37:52 +00:00
Development
- Removed unused exception from PHP-docs. - Fixed types not same as declared. - Fixed issues with reg-ex and php-unit tests. - Removed unnecessary type casting. - Declared functions as static (better scoping + performance). - Moved `\is_callable($callback) === false` as the execution costs less than previous in `Router.php`. - Changed `ob_get_contents` to `ob_get_clean`. - Added type hints to methods.
This commit is contained in:
@@ -74,7 +74,7 @@ class EventArgument implements IEventArgument
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($name)
|
||||
public function __get(string $name)
|
||||
{
|
||||
return $this->arguments[$name] ?? null;
|
||||
}
|
||||
@@ -83,7 +83,7 @@ class EventArgument implements IEventArgument
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function __isset($name)
|
||||
public function __isset(string $name)
|
||||
{
|
||||
return array_key_exists($name, $this->arguments);
|
||||
}
|
||||
@@ -93,7 +93,7 @@ class EventArgument implements IEventArgument
|
||||
* @param mixed $value
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
public function __set(string $name, $value)
|
||||
{
|
||||
throw new \InvalidArgumentException('Not supported');
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ class DebugEventHandler implements IEventHandler
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->callback = function (EventArgument $argument) {
|
||||
$this->callback = static function (EventArgument $argument) {
|
||||
// todo: log in database
|
||||
};
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ interface IGroupRoute extends IRoute
|
||||
* @param string $prefix
|
||||
* @return static
|
||||
*/
|
||||
public function setPrefix($prefix): self;
|
||||
public function setPrefix(string $prefix): self;
|
||||
|
||||
/**
|
||||
* Get prefix.
|
||||
|
||||
@@ -85,7 +85,7 @@ abstract class Route implements IRoute
|
||||
|
||||
/* Filter parameters with null-value */
|
||||
if ($this->filterEmptyParams === true) {
|
||||
$parameters = array_filter($parameters, function ($var) {
|
||||
$parameters = array_filter($parameters, static function ($var) {
|
||||
return ($var !== null);
|
||||
});
|
||||
}
|
||||
@@ -124,7 +124,7 @@ abstract class Route implements IRoute
|
||||
return \call_user_func_array([$class, $method], $parameters);
|
||||
}
|
||||
|
||||
protected function parseParameters($route, $url, $parameterRegex = null)
|
||||
protected function parseParameters($route, $url, $parameterRegex = null): ?array
|
||||
{
|
||||
$regex = (strpos($route, $this->paramModifiers[0]) === false) ? null :
|
||||
sprintf
|
||||
@@ -155,10 +155,8 @@ abstract class Route implements IRoute
|
||||
/* If custom regex is defined, use that */
|
||||
if (isset($this->where[$name]) === true) {
|
||||
$regex = $this->where[$name];
|
||||
} else if ($parameterRegex !== null) {
|
||||
$regex = $parameterRegex;
|
||||
} else {
|
||||
$regex = $this->defaultParameterRegex ?? static::PARAMETERS_DEFAULT_REGEX;
|
||||
$regex = $parameterRegex ?? $this->defaultParameterRegex ?? static::PARAMETERS_DEFAULT_REGEX;
|
||||
}
|
||||
|
||||
$regex = sprintf('((\/|\-)(?P<%2$s>%3$s))%1$s', $parameters[2][$key], $name, $regex);
|
||||
|
||||
@@ -123,7 +123,7 @@ class RouteGroup extends Route implements IGroupRoute
|
||||
* @param string $prefix
|
||||
* @return static
|
||||
*/
|
||||
public function setPrefix($prefix): IGroupRoute
|
||||
public function setPrefix(string $prefix): IGroupRoute
|
||||
{
|
||||
$this->prefix = '/' . trim($prefix, '/');
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ class RoutePartialGroup extends RouteGroup implements IPartialGroupRoute
|
||||
}
|
||||
|
||||
/* Set the parameters */
|
||||
$this->setParameters((array)$parameters);
|
||||
$this->setParameters($parameters);
|
||||
}
|
||||
|
||||
return $this->matchDomain($request);
|
||||
|
||||
@@ -76,7 +76,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
protected function call($method)
|
||||
protected function call($method): bool
|
||||
{
|
||||
$this->setCallback($this->controller . '@' . $method);
|
||||
|
||||
@@ -190,7 +190,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
|
||||
* @param array $names
|
||||
* @return static $this
|
||||
*/
|
||||
public function setMethodNames(array $names)
|
||||
public function setMethodNames(array $names): RouteResource
|
||||
{
|
||||
$this->methodNames = $names;
|
||||
|
||||
|
||||
@@ -585,7 +585,7 @@ class Router
|
||||
|
||||
/* Check if callback matches (if it's not a function) */
|
||||
$callback = $route->getCallback();
|
||||
if (\is_string($name) === true && \is_string($callback) === true && strpos($name, '@') !== false && strpos($callback, '@') !== false && \is_callable($callback) === false) {
|
||||
if (\is_string($name) === true && \is_string($callback) === true && \is_callable($callback) === false && strpos($name, '@') !== false && strpos($callback, '@') !== false) {
|
||||
|
||||
/* Check if the entire callback is matching */
|
||||
if (strpos($callback, $name) === 0 || strtolower($callback) === strtolower($name)) {
|
||||
@@ -625,7 +625,6 @@ class Router
|
||||
* @param array|null $getParams
|
||||
* @return Url
|
||||
* @throws InvalidArgumentException
|
||||
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||
*/
|
||||
public function getUrl(?string $name = null, $parameters = null, ?array $getParams = null): Url
|
||||
{
|
||||
|
||||
@@ -75,7 +75,7 @@ class SimpleRouter
|
||||
try {
|
||||
ob_start();
|
||||
static::router()->setDebugEnabled(true)->start();
|
||||
$routerOutput = ob_get_contents();
|
||||
$routerOutput = ob_get_clean();
|
||||
ob_end_clean();
|
||||
} catch (\Exception $e) {
|
||||
|
||||
@@ -458,15 +458,8 @@ class SimpleRouter
|
||||
try {
|
||||
return static::router()->getUrl($name, $parameters, $getParams);
|
||||
} catch (\Exception $e) {
|
||||
try {
|
||||
return new Url('/');
|
||||
} catch (MalformedUrlException $e) {
|
||||
|
||||
}
|
||||
return new Url('/');
|
||||
}
|
||||
|
||||
// This will never happen...
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user