alltube/classes/UglyRouter.php

68 lines
1.8 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* UglyRouter class.
*/
2017-01-10 22:39:58 +00:00
namespace Alltube;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
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;
/**
* 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:39:58 +00:00
* @param ServerRequestInterface $request The current HTTP request object
*
2022-02-03 19:21:25 +00:00
* @return int[]|string[]|array[]
*
* @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
{
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']);
}
return $this->createDispatcher()->dispatch(
$request->getMethod(),
2020-10-21 23:20:43 +00:00
(string) $uri
);
}
/**
2017-01-10 22:39:58 +00:00
* Build the path for a named route including the base path.
*
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
*
2020-05-13 20:28:05 +00:00
* @return string
* @throws InvalidArgumentException If required data not provided
2020-05-13 20:28:05 +00:00
* @throws RuntimeException If named route does not exist
*/
2020-12-17 21:43:05 +00:00
public function pathFor($name, array $data = [], array $queryParams = []): string
{
$queryParams['page'] = $this->relativePathFor($name, $data);
2020-10-21 23:20:43 +00:00
$url = Uri::createFromString($this->relativePathFor($name, $data, $queryParams))->withPath('');
if ($this->basePath) {
2020-10-21 23:20:43 +00:00
$url = $url->withBasePath($this->basePath);
}
return $url;
}
}