Compare commits

..

2 Commits

Author SHA1 Message Date
Simon 7ba29457ff Pass unit tests 2023-08-09 04:55:26 +02:00
Simon 30db793e85 Optimized parameter handling 2023-08-09 00:18:30 +02:00
13 changed files with 85 additions and 99 deletions
+21 -9
View File
@@ -1742,7 +1742,6 @@ SimpleRouter::setCustomClassLoader(new MyCustomClassLoader());
php-di support was discontinued by version 4.3, however you can easily add it again by creating your own class-loader like the example below:
```php
use Pecee\SimpleRouter\ClassLoader\IClassLoader;
use Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException;
class MyCustomClassLoader implements IClassLoader
@@ -1763,14 +1762,19 @@ class MyCustomClassLoader implements IClassLoader
*
* @param string $class
* @return object
* @throws ClassNotFoundHttpException
* @throws NotFoundHttpException
*/
public function loadClass(string $class)
{
if ($this->container->has($class) === false) {
throw new ClassNotFoundHttpException($class, null, sprintf('Class "%s" does not exist', $class), 404, null);
if (class_exists($class) === false) {
throw new NotFoundHttpException(sprintf('Class "%s" does not exist', $class), 404);
}
return $this->container->get($class);
try {
return $this->container->get($class);
} catch (\Exception $e) {
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
}
}
/**
@@ -1778,11 +1782,15 @@ class MyCustomClassLoader implements IClassLoader
* @param object $class
* @param string $method
* @param array $parameters
* @return string
* @return object
*/
public function loadClassMethod($class, string $method, array $parameters)
{
return (string)$this->container->call([$class, $method], $parameters);
try {
return $this->container->call([$class, $method], $parameters);
} catch (\Exception $e) {
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
}
}
/**
@@ -1790,11 +1798,15 @@ class MyCustomClassLoader implements IClassLoader
*
* @param Callable $closure
* @param array $parameters
* @return string
* @return mixed
*/
public function loadClosure(callable $closure, array $parameters)
{
return (string)$this->container->call($closure, $parameters);
try {
return $this->container->call($closure, $parameters);
} catch (\Exception $e) {
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
}
}
}
```
+10 -14
View File
@@ -82,10 +82,6 @@ class InputHandler
if ($post !== false) {
$this->originalPost += $post;
}
} else {
$post = [];
parse_str($contents, $post);
$this->originalPost += $post;
}
}
@@ -112,7 +108,7 @@ class InputHandler
foreach ($files as $key => $value) {
// Parse multi dept file array
if (isset($value['name']) === false && is_array($value) === true) {
if(isset($value['name']) === false && is_array($value) === true) {
$list[$key] = $this->parseFiles($value, $key);
continue;
}
@@ -165,12 +161,12 @@ class InputHandler
try {
$file = InputFile::createFromArray([
'index' => ($key === '' && $originalIndex !== '') ? $originalIndex : $key,
'name' => $original['name'][$key],
'error' => $original['error'][$key],
'index' => ($key === '' && $originalIndex !== '') ? $originalIndex : $key,
'name' => $original['name'][$key],
'error' => $original['error'][$key],
'tmp_name' => $original['tmp_name'][$key],
'type' => $original['type'][$key],
'size' => $original['size'][$key],
'type' => $original['type'][$key],
'size' => $original['size'][$key],
]);
if (isset($output[$key]) === true) {
@@ -235,7 +231,7 @@ class InputHandler
{
$element = null;
if (count($methods) > 0) {
if(count($methods) > 0) {
$methods = is_array(...$methods) ? array_values(...$methods) : $methods;
}
@@ -307,9 +303,9 @@ class InputHandler
public function exists($index, ...$methods): bool
{
// Check array
if (is_array($index) === true) {
foreach ($index as $key) {
if ($this->value($key, null, ...$methods) === null) {
if(is_array($index) === true) {
foreach($index as $key) {
if($this->value($key, null, ...$methods) === null) {
return false;
}
}
@@ -98,7 +98,7 @@ class BaseCsrfVerifier implements IMiddleware
*/
public function handle(Request $request): void
{
if ($this->skip($request) === false && ($request->isPostBack() === true || $request->isPostBack() === true && $this->isIncluded($request) === true)) {
if ($this->skip($request) === false && ($request->isPostBack() === true || $this->isIncluded($request) === true)) {
$token = $request->getInputHandler()->value(
static::POST_KEY,
+10 -15
View File
@@ -130,9 +130,9 @@ class Request
// Check if special IIS header exist, otherwise use default.
$url = $this->getHeader('unencoded-url');
if ($url !== null) {
if($url !== null){
$this->setUrl(new Url($url));
} else {
}else{
$this->setUrl(new Url(urldecode((string)$this->getHeader('request-uri'))));
}
$this->setContentType((string)$this->getHeader('content-type'));
@@ -225,7 +225,7 @@ class Request
public function getIp(bool $safeMode = false): ?string
{
$headers = [];
if ($safeMode === false) {
if($safeMode === false) {
$headers = [
'http-cf-connecting-ip',
'http-client-ip',
@@ -303,9 +303,9 @@ class Request
*/
public function getFirstHeader(array $headers, $defaultValue = null)
{
foreach ($headers as $header) {
foreach($headers as $header) {
$header = $this->getHeader($header);
if ($header !== null) {
if($header !== null) {
return $header;
}
}
@@ -329,7 +329,7 @@ class Request
*/
protected function setContentType(string $contentType): self
{
if (strpos($contentType, ';') > 0) {
if(strpos($contentType, ';') > 0) {
$this->contentType = strtolower(substr($contentType, 0, strpos($contentType, ';')));
} else {
$this->contentType = strtolower($contentType);
@@ -371,7 +371,7 @@ class Request
/**
* Returns true when request-method is type that could contain data in the page body.
*
*
* @return bool
*/
public function isPostBack(): bool
@@ -395,11 +395,11 @@ class Request
{
$this->url = $url;
if ($this->getHost() !== null) {
$url->setHost($this->getHost());
if ($this->url->getHost() === null) {
$this->url->setHost((string)$this->getHost());
}
if ($this->isSecure() === true) {
if($this->isSecure() === true) {
$this->url->setScheme('https');
}
}
@@ -409,11 +409,6 @@ class Request
*/
public function setHost(?string $host): void
{
// Strip any potential ports from hostname
if (strpos((string)$host, ':') !== false) {
$host = strstr($host, strrchr($host, ':'), true);
}
$this->host = $host;
}
+1 -3
View File
@@ -32,8 +32,6 @@ class Response
*
* @param string $url
* @param ?int $httpCode
*
* @return never
*/
public function redirect(string $url, ?int $httpCode = null): void
{
@@ -129,4 +127,4 @@ class Response
return $this;
}
}
}
+6 -20
View File
@@ -67,26 +67,19 @@ class Url implements JsonSerializable
public function __construct(?string $url)
{
$this->originalUrl = $url;
$this->parse($url, true);
}
public function parse(?string $url, bool $setOriginalPath = false): self
{
if ($url !== null) {
if ($url !== null && $url !== '/') {
$data = $this->parseUrl($url);
$this->scheme = $data['scheme'] ?? $this->scheme;
$this->host = $data['host'] ?? $this->host;
$this->scheme = $data['scheme'] ?? null;
$this->host = $data['host'] ?? null;
$this->port = $data['port'] ?? null;
$this->username = $data['user'] ?? null;
$this->password = $data['pass'] ?? null;
if (isset($data['path']) === true) {
$this->setPath($data['path']);
if ($setOriginalPath === true) {
$this->originalPath = $data['path'];
}
$this->originalPath = $data['path'];
}
$this->fragment = $data['fragment'] ?? null;
@@ -95,8 +88,6 @@ class Url implements JsonSerializable
$this->setQueryString($data['query']);
}
}
return $this;
}
/**
@@ -145,15 +136,10 @@ class Url implements JsonSerializable
/**
* Get url host
*
* @param bool $includeTrails Prepend // in front of hostname
* @return string|null
*/
public function getHost(bool $includeTrails = false): ?string
public function getHost(): ?string
{
if ((string)$this->host !== '' && $includeTrails === true) {
return '//' . $this->host;
}
return $this->host;
}
@@ -536,7 +522,7 @@ class Url implements JsonSerializable
*/
public function jsonSerialize(): string
{
return $this->getHost(true) . $this->getRelativeUrl();
return $this->getRelativeUrl();
}
public function __toString(): string
+7 -10
View File
@@ -6,7 +6,6 @@ use Pecee\Http\Middleware\IMiddleware;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Exceptions\HttpException;
use Pecee\SimpleRouter\Router;
use Pecee\SimpleRouter\SimpleRouter;
abstract class LoadableRoute extends Route implements ILoadableRoute
{
@@ -139,6 +138,12 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
{
$url = $this->getUrl();
$group = $this->getGroup();
if ($group !== null && count($group->getDomains()) !== 0) {
$url = '//' . $group->getDomains()[0] . $url;
}
/* Create the param string - {parameter} */
$param1 = $this->paramModifiers[0] . '%s' . $this->paramModifiers[1];
@@ -172,15 +177,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
}
}
$url = rtrim('/' . ltrim($url, '/'), '/') . '/';
$group = $this->getGroup();
if ($group !== null && count($group->getDomains()) !== 0 && SimpleRouter::request()->getHost() !== $group->getDomains()[0]) {
$url = '//' . $group->getDomains()[0] . $url;
}
return $url;
return rtrim('/' . ltrim($url, '/'), '/') . '/';
}
/**
@@ -3,7 +3,6 @@
namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Request;
use Pecee\SimpleRouter\SimpleRouter;
class RouteController extends LoadableRoute implements IControllerRoute
{
@@ -78,15 +77,13 @@ class RouteController extends LoadableRoute implements IControllerRoute
$group = $this->getGroup();
$url .= '/' . trim($this->getUrl(), '/') . '/' . strtolower((string)$method) . implode('/', $parameters);
$url = '/' . trim($url, '/') . '/';
if ($group !== null && count($group->getDomains()) !== 0 && SimpleRouter::request()->getHost() !== $group->getDomains()[0]) {
$url = '//' . $group->getDomains()[0] . $url;
if ($group !== null && count($group->getDomains()) !== 0) {
$url .= '//' . $group->getDomains()[0];
}
return $url;
$url .= '/' . trim($this->getUrl(), '/') . '/' . strtolower((string)$method) . implode('/', $parameters);
return '/' . trim($url, '/') . '/';
}
public function matchRoute(string $url, Request $request): bool
+1 -1
View File
@@ -220,7 +220,7 @@ class RouteGroup extends Route implements IGroupRoute
$this->setExceptionHandlers((array)$settings['exceptionHandler']);
}
if (isset($settings['domain']) === true) {
if ($merge === false && isset($settings['domain']) === true) {
$this->setDomains((array)$settings['domain']);
}
@@ -3,7 +3,6 @@
namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Request;
use Pecee\SimpleRouter\SimpleRouter;
class RouteResource extends LoadableRoute implements IControllerRoute
{
@@ -81,14 +80,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
return rtrim($this->url . $parametersUrl . $this->urls[$url], '/') . '/';
}
$url = $this->url . $parametersUrl;
$group = $this->getGroup();
if ($group !== null && count($group->getDomains()) !== 0 && SimpleRouter::request()->getHost() !== $group->getDomains()[0]) {
$url = '//' . $group->getDomains()[0] . $url;
}
return $url;
return $this->url . $parametersUrl;
}
protected function call($method): bool
+21 -6
View File
@@ -690,7 +690,10 @@ class Router
/* If nothing is defined and a route is loaded we use that */
if ($name === null && $loadedRoute !== null) {
return $this->request->getUrlCopy()->parse($loadedRoute->findUrl($loadedRoute->getMethod(), $parameters, $name))->setParams($getParams);
return $this->request
->getUrlCopy()
->setPath($loadedRoute->findUrl($loadedRoute->getMethod(), $parameters, $name))
->setParams($getParams);
}
if ($name !== null) {
@@ -698,7 +701,10 @@ class Router
$route = $this->findRoute($name);
if ($route !== null) {
return $this->request->getUrlCopy()->parse($route->findUrl($route->getMethod(), $parameters, $name))->setParams($getParams);
return $this->request
->getUrlCopy()
->setPath($route->findUrl($route->getMethod(), $parameters, $name))
->setParams($getParams);
}
}
@@ -713,12 +719,18 @@ class Router
/* Check if the route contains the name/alias */
if ($processedRoute->hasName($controller) === true) {
return $this->request->getUrlCopy()->parse($processedRoute->findUrl($method, $parameters, $name))->setParams($getParams);
return $this->request
->getUrlCopy()
->setPath($processedRoute->findUrl($method, $parameters, $name))
->setParams($getParams);
}
/* Check if the route controller is equal to the name */
if ($processedRoute instanceof IControllerRoute && strtolower($processedRoute->getController()) === strtolower($controller)) {
return $this->request->getUrlCopy()->parse($processedRoute->findUrl($method, $parameters, $name))->setParams($getParams);
return $this->request
->getUrlCopy()
->setPath($processedRoute->findUrl($method, $parameters, $name))
->setParams($getParams);
}
}
@@ -728,7 +740,10 @@ class Router
$url = trim(implode('/', array_merge((array)$name, (array)$parameters)), '/');
$url = (($url === '') ? '/' : '/' . $url . '/');
return $this->request->getUrlCopy()->parse($url)->setParams($getParams);
return $this->request
->getUrlCopy()
->setPath($url)
->setParams($getParams);
}
/**
@@ -958,4 +973,4 @@ class Router
return $this;
}
}
}
@@ -94,13 +94,11 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase
public function testSimilarUrls()
{
TestRouter::reset();
// Match normal route on alias
TestRouter::get('/url11', 'DummyController@method1');
TestRouter::get('/url22', 'DummyController@method2');
TestRouter::get('/url33', 'DummyController@method2')->name('match');
TestRouter::debugNoReset('/url33', 'get');
$this->assertEquals(TestRouter::getUrl('match'), TestRouter::getUrl());
+1 -1
View File
@@ -17,7 +17,7 @@ class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
{
$request = static::request();
$request->setUrl((new \Pecee\Http\Url($testUrl)));
$request->setUrl((new \Pecee\Http\Url($testUrl))->setHost('local.unitTest'));
$request->setMethod($testMethod);
static::start();