2017-01-10 22:37:29 +00:00
|
|
|
<?php
|
2019-10-03 19:24:12 +00:00
|
|
|
|
2017-01-10 22:37:29 +00:00
|
|
|
/**
|
|
|
|
* UglyRouter class.
|
|
|
|
*/
|
2017-01-10 22:39:58 +00:00
|
|
|
|
2017-01-10 22:37:29 +00:00
|
|
|
namespace Alltube;
|
|
|
|
|
2018-02-05 15:48:58 +00:00
|
|
|
use InvalidArgumentException;
|
2017-01-10 22:37:29 +00:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2018-02-05 15:48:58 +00:00
|
|
|
use RuntimeException;
|
2020-10-21 23:20:43 +00:00
|
|
|
use Slim\Http\Uri;
|
2017-01-10 22:39:58 +00:00
|
|
|
use Slim\Router;
|
2017-01-10 22:37:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extend Slim's router class in order to disable URL rewriting.
|
|
|
|
*/
|
|
|
|
class UglyRouter extends Router
|
|
|
|
{
|
|
|
|
/**
|
2017-01-10 22:39:58 +00:00
|
|
|
* Dispatch router for HTTP request.
|
2017-01-10 22:37:29 +00:00
|
|
|
*
|
2017-01-10 22:39:58 +00:00
|
|
|
* @param ServerRequestInterface $request The current HTTP request object
|
2017-01-10 22:37:29 +00:00
|
|
|
*
|
2022-02-03 19:21:25 +00:00
|
|
|
* @return int[]|string[]|array[]
|
2017-01-10 22:37:29 +00:00
|
|
|
*
|
|
|
|
* @link https://github.com/nikic/FastRoute/blob/master/src/Dispatcher.php
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function dispatch(ServerRequestInterface $request): array
|
2017-01-10 22:37:29 +00:00
|
|
|
{
|
2020-10-21 23:20:43 +00:00
|
|
|
$params = $request->getQueryParams();
|
|
|
|
$uri = new Uri('', '');
|
|
|
|
|
|
|
|
if (isset($params['page'])) {
|
|
|
|
// Build an URI that the router can understand.
|
|
|
|
$uri = $uri->withPath($params['page']);
|
2017-01-10 22:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->createDispatcher()->dispatch(
|
|
|
|
$request->getMethod(),
|
2020-10-21 23:20:43 +00:00
|
|
|
(string) $uri
|
2017-01-10 22:37:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-10 22:39:58 +00:00
|
|
|
* Build the path for a named route including the base path.
|
2017-01-10 22:37:29 +00:00
|
|
|
*
|
2020-09-27 13:53:53 +00:00
|
|
|
* @param mixed $name Route name
|
2020-05-13 20:28:05 +00:00
|
|
|
* @param string[] $data Named argument replacement data
|
|
|
|
* @param string[] $queryParams Optional query string parameters
|
2017-01-10 22:37:29 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return string
|
2018-02-05 15:48:58 +00:00
|
|
|
* @throws InvalidArgumentException If required data not provided
|
2020-05-13 20:28:05 +00:00
|
|
|
* @throws RuntimeException If named route does not exist
|
2017-01-10 22:37:29 +00:00
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function pathFor($name, array $data = [], array $queryParams = []): string
|
2017-01-10 22:37:29 +00:00
|
|
|
{
|
2022-02-23 20:27:22 +00:00
|
|
|
$queryParams['page'] = $this->relativePathFor($name, $data);
|
2020-10-21 23:20:43 +00:00
|
|
|
$url = Uri::createFromString($this->relativePathFor($name, $data, $queryParams))->withPath('');
|
2017-01-10 22:37:29 +00:00
|
|
|
|
|
|
|
if ($this->basePath) {
|
2020-10-21 23:20:43 +00:00
|
|
|
$url = $url->withBasePath($this->basePath);
|
2017-01-10 22:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
}
|