mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-16 18:50:11 +03: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:
20
.idea/workspace.xml
generated
20
.idea/workspace.xml
generated
@@ -5,9 +5,21 @@
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="a7058529-bdc4-40b4-a50d-c50564dc83f0" name="Default" comment="">
|
||||
<change beforePath="$PROJECT_DIR$/.idea/php.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/php.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/Pecee/Http/Input/InputHandler.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/Http/Input/InputHandler.php" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/Pecee/Http/Middleware/BaseCsrfVerifier.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/Http/Middleware/BaseCsrfVerifier.php" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/Pecee/Http/Response.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/Http/Response.php" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/Pecee/Http/Url.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/Http/Url.php" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Event/EventArgument.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Event/EventArgument.php" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Handlers/DebugEventHandler.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Handlers/DebugEventHandler.php" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Route/IGroupRoute.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Route/IGroupRoute.php" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Route/Route.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Route/Route.php" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Route/RouteGroup.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Route/RouteGroup.php" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Route/RoutePartialGroup.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Route/RoutePartialGroup.php" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Route/RouteResource.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Route/RouteResource.php" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Router.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Router.php" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/Pecee/SimpleRouter/SimpleRouter.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/SimpleRouter/SimpleRouter.php" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/tests/Pecee/SimpleRouter/RouterUrlTest.php" beforeDir="false" afterPath="$PROJECT_DIR$/tests/Pecee/SimpleRouter/RouterUrlTest.php" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@@ -135,7 +147,7 @@
|
||||
<other-services-enabled>false</other-services-enabled>
|
||||
<auto-save>true</auto-save>
|
||||
</component>
|
||||
<component name="PhpWorkspaceProjectConfiguration" interpreter_name="PHP 7.1">
|
||||
<component name="PhpWorkspaceProjectConfiguration" interpreter_name="PHP 7.3">
|
||||
<include_path>
|
||||
<path value="$PROJECT_DIR$/vendor/composer" />
|
||||
<path value="$PROJECT_DIR$/vendor/phpdocumentor/reflection-common" />
|
||||
@@ -177,6 +189,8 @@
|
||||
<path value="$PROJECT_DIR$/vendor/php-di/php-di" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/yaml" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/polyfill-php56" />
|
||||
<path value="$PROJECT_DIR$/vendor/opis/closure" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/polyfill-ctype" />
|
||||
</include_path>
|
||||
</component>
|
||||
<component name="ProjectFrameBounds" extendedState="6">
|
||||
@@ -299,7 +313,7 @@
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="tests" type="PHPUnitRunConfigurationType" factoryName="PHPUnit" temporary="true">
|
||||
<TestRunner directory="$PROJECT_DIR$/tests" />
|
||||
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml" directory="$PROJECT_DIR$/tests" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="debug.php" type="PhpLocalRunConfigurationType" factoryName="PHP Console" temporary="true" path="$PROJECT_DIR$/tests/debug.php">
|
||||
|
||||
@@ -73,7 +73,7 @@ class InputHandler
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach ((array)$_FILES as $key => $value) {
|
||||
foreach ($_FILES as $key => $value) {
|
||||
|
||||
// Handle array input
|
||||
if (\is_array($value['name']) === false) {
|
||||
|
||||
@@ -17,7 +17,6 @@ class BaseCsrfVerifier implements IMiddleware
|
||||
|
||||
/**
|
||||
* BaseCsrfVerifier constructor.
|
||||
* @throws \Pecee\Http\Security\Exceptions\SecurityException
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@@ -30,7 +30,7 @@ class Response
|
||||
* Redirect the response
|
||||
*
|
||||
* @param string $url
|
||||
* @param int $httpCode
|
||||
* @param ?int $httpCode
|
||||
*/
|
||||
public function redirect(string $url, ?int $httpCode = null): void
|
||||
{
|
||||
@@ -86,7 +86,7 @@ class Response
|
||||
/**
|
||||
* Json encode
|
||||
* @param array|\JsonSerializable $value
|
||||
* @param int $options JSON options Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR.
|
||||
* @param ?int $options JSON options Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR.
|
||||
* @param int $dept JSON debt.
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
|
||||
@@ -20,7 +20,7 @@ class Url implements \JsonSerializable
|
||||
/**
|
||||
* Url constructor.
|
||||
*
|
||||
* @param string $url
|
||||
* @param ?string $url
|
||||
* @throws MalformedUrlException
|
||||
*/
|
||||
public function __construct(?string $url)
|
||||
@@ -385,7 +385,7 @@ class Url implements \JsonSerializable
|
||||
{
|
||||
$encodedUrl = preg_replace_callback(
|
||||
'/[^:\/@?&=#]+/u',
|
||||
function ($matches) {
|
||||
static function ($matches) {
|
||||
return urlencode($matches[0]);
|
||||
},
|
||||
$url
|
||||
@@ -412,7 +412,7 @@ class Url implements \JsonSerializable
|
||||
if (\count($getParams) !== 0) {
|
||||
|
||||
if ($includeEmpty === false) {
|
||||
$getParams = array_filter($getParams, function ($item) {
|
||||
$getParams = array_filter($getParams, static function ($item) {
|
||||
return (trim($item) !== '');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -30,8 +30,8 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase
|
||||
public function testUnicodeCharacters()
|
||||
{
|
||||
// Test spanish characters
|
||||
TestRouter::get('/cursos/listado/{listado?}/{category?}', 'DummyController@method1', ['defaultParameterRegex' => '[\w\p{L}\s-]+']);
|
||||
TestRouter::get('/test/{param}', 'DummyController@method1', ['defaultParameterRegex' => '[\w\p{L}\s-\í]+']);
|
||||
TestRouter::get('/cursos/listado/{listado?}/{category?}', 'DummyController@method1', ['defaultParameterRegex' => '[\w\p{L}\s\-]+']);
|
||||
TestRouter::get('/test/{param}', 'DummyController@method1', ['defaultParameterRegex' => '[\w\p{L}\s\-\í]+']);
|
||||
TestRouter::debugNoReset('/cursos/listado/especialidad/cirugía local', 'get');
|
||||
|
||||
$this->assertEquals('/cursos/listado/{listado?}/{category?}/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl());
|
||||
|
||||
Reference in New Issue
Block a user