2016-10-20 22:09:36 -04:00
|
|
|
<?php declare(strict_types=1);
|
2015-09-14 19:54:34 -04:00
|
|
|
/**
|
2017-02-16 11:09:37 -05:00
|
|
|
* Hummingbird Anime List Client
|
2015-11-16 11:40:01 -05:00
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2015-11-16 11:40:01 -05:00
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
2016-08-30 10:01:18 -04:00
|
|
|
*
|
2022-03-04 15:50:35 -05:00
|
|
|
* @copyright 2015 - 2022 Timothy J. Warren <tim@timshome.page>
|
2016-08-30 10:01:18 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2022-03-04 15:50:35 -05:00
|
|
|
* @link https://git.timshome.page/timw4mail/HummingBirdAnimeClient
|
2015-09-14 19:54:34 -04:00
|
|
|
*/
|
2016-10-20 22:09:36 -04:00
|
|
|
|
2015-09-15 13:19:29 -04:00
|
|
|
namespace Aviat\AnimeClient;
|
2015-06-26 16:39:10 -04:00
|
|
|
|
2015-10-20 15:59:51 -04:00
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
2022-03-03 17:26:09 -05:00
|
|
|
use Aviat\Ion\Di\Exception\{ContainerException, NotFoundException};
|
2017-02-15 15:56:10 -05:00
|
|
|
use InvalidArgumentException;
|
2015-10-20 15:59:51 -04:00
|
|
|
|
2015-06-26 12:03:42 -04:00
|
|
|
/**
|
2015-09-14 10:54:50 -04:00
|
|
|
* UrlGenerator class.
|
2015-06-26 12:03:42 -04:00
|
|
|
*/
|
2022-03-03 17:26:09 -05:00
|
|
|
class UrlGenerator extends RoutingBase
|
|
|
|
{
|
2015-10-20 15:59:51 -04:00
|
|
|
/**
|
|
|
|
* The current HTTP host
|
|
|
|
*/
|
2020-04-10 20:01:46 -04:00
|
|
|
protected string $host;
|
2015-10-20 15:59:51 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws ContainerException
|
|
|
|
* @throws NotFoundException
|
2015-10-20 15:59:51 -04:00
|
|
|
*/
|
|
|
|
public function __construct(ContainerInterface $container)
|
|
|
|
{
|
|
|
|
parent::__construct($container);
|
2018-10-11 09:53:14 -04:00
|
|
|
|
2016-02-16 16:28:44 -05:00
|
|
|
$this->host = $container->get('request')->getServerParams()['HTTP_HOST'];
|
2015-10-20 15:59:51 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
/**
|
|
|
|
* Get the base url for css/js/images
|
|
|
|
*/
|
2018-02-02 09:50:58 -05:00
|
|
|
public function assetUrl(string ...$args): string
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
2018-02-02 09:50:58 -05:00
|
|
|
$baseUrl = rtrim($this->url(''), '/')
|
2018-11-09 10:38:35 -05:00
|
|
|
. $this->config->get('asset_path');
|
2015-09-14 15:49:20 -04:00
|
|
|
|
2017-02-15 15:56:10 -05:00
|
|
|
array_unshift($args, $baseUrl);
|
2015-07-02 14:04:04 -04:00
|
|
|
|
2018-02-02 09:50:58 -05:00
|
|
|
return implode('/', $args);
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
|
|
|
|
2015-09-14 10:54:50 -04:00
|
|
|
/**
|
|
|
|
* Generate a proper url from the path
|
|
|
|
*/
|
2017-02-17 10:55:17 -05:00
|
|
|
public function url(string $path): string
|
2015-09-14 10:54:50 -04:00
|
|
|
{
|
|
|
|
$path = trim($path, '/');
|
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
$path = preg_replace('`{/.*?}`i', '', $path) ?? '';
|
2015-09-14 10:54:50 -04:00
|
|
|
|
2015-10-20 15:59:51 -04:00
|
|
|
// Remove any optional parameters from the route
|
|
|
|
// and replace them with existing route parameters, if they exist
|
2020-03-16 15:06:55 -04:00
|
|
|
$pathSegments = explode('/', $path);
|
|
|
|
$segmentCount = count($pathSegments);
|
2015-10-20 15:59:51 -04:00
|
|
|
$segments = $this->segments();
|
|
|
|
|
2020-03-16 15:06:55 -04:00
|
|
|
for ($i = 0; $i < $segmentCount; $i++)
|
2015-10-20 15:59:51 -04:00
|
|
|
{
|
|
|
|
if ( ! array_key_exists($i + 1, $segments))
|
|
|
|
{
|
2018-11-09 10:38:35 -05:00
|
|
|
$segments[$i + 1] = '';
|
2015-10-20 15:59:51 -04:00
|
|
|
}
|
|
|
|
|
2021-02-12 19:17:39 -05:00
|
|
|
$pathSegments[$i] = preg_replace('`{.*?}`', $segments[$i + 1], $pathSegments[$i] ?? '');
|
2015-10-20 15:59:51 -04:00
|
|
|
}
|
2022-03-03 13:25:10 -05:00
|
|
|
|
2020-03-16 15:06:55 -04:00
|
|
|
$path = implode('/', $pathSegments);
|
2015-09-14 10:54:50 -04:00
|
|
|
|
2018-10-11 09:53:14 -04:00
|
|
|
$scheme = $this->config->get('secure_urls') !== FALSE ? 'https:' : 'http:';
|
2018-10-09 18:10:20 -04:00
|
|
|
|
|
|
|
return "{$scheme}//{$this->host}/{$path}";
|
2015-09-14 10:54:50 -04:00
|
|
|
}
|
|
|
|
|
2015-09-14 15:49:20 -04:00
|
|
|
/**
|
|
|
|
* Full default path for the list pages
|
|
|
|
*
|
2017-02-15 15:56:10 -05:00
|
|
|
* @throws InvalidArgumentException
|
2015-09-14 15:49:20 -04:00
|
|
|
*/
|
2017-02-17 10:55:17 -05:00
|
|
|
public function defaultUrl(string $type): string
|
2015-09-14 10:54:50 -04:00
|
|
|
{
|
|
|
|
$type = trim($type);
|
2018-11-09 10:38:35 -05:00
|
|
|
$defaultPath = $this->config->get("default_{$type}_list_path");
|
2015-09-14 10:54:50 -04:00
|
|
|
|
2018-02-02 09:50:58 -05:00
|
|
|
if ($defaultPath !== NULL)
|
2015-09-14 10:54:50 -04:00
|
|
|
{
|
2020-12-10 17:04:45 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
2017-02-15 15:56:10 -05:00
|
|
|
return $this->url("{$type}/{$defaultPath}");
|
2020-12-10 17:04:45 -05:00
|
|
|
// @codeCoverageIgnoreEnd
|
2015-09-14 10:54:50 -04:00
|
|
|
}
|
|
|
|
|
2017-02-15 15:56:10 -05:00
|
|
|
throw new InvalidArgumentException("Invalid default type: '{$type}'");
|
2015-09-14 10:54:50 -04:00
|
|
|
}
|
2015-06-26 12:03:42 -04:00
|
|
|
}
|
2022-03-03 13:25:10 -05:00
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
// End of UrlGenerator.php
|