API client for Kitsu.io, with optional Anime collection, and optional Anilist syncing.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
2.6KB

  1. <?php declare(strict_types=1);
  2. /**
  3. * Hummingbird Anime List Client
  4. *
  5. * An API client for Kitsu to manage anime and manga watch lists
  6. *
  7. * PHP version 7.1
  8. *
  9. * @package HummingbirdAnimeClient
  10. * @author Timothy J. Warren <tim@timshomepage.net>
  11. * @copyright 2015 - 2018 Timothy J. Warren
  12. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  13. * @version 4.1
  14. * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
  15. */
  16. namespace Aviat\AnimeClient;
  17. use Aviat\Ion\Di\ContainerInterface;
  18. use InvalidArgumentException;
  19. /**
  20. * UrlGenerator class.
  21. */
  22. class UrlGenerator extends RoutingBase {
  23. /**
  24. * The current HTTP host
  25. * @var string
  26. */
  27. protected $host;
  28. /**
  29. * Constructor
  30. *
  31. * @param ContainerInterface $container
  32. * @throws \Aviat\Ion\Di\Exception\ContainerException
  33. * @throws \Aviat\Ion\Di\Exception\NotFoundException
  34. */
  35. public function __construct(ContainerInterface $container)
  36. {
  37. parent::__construct($container);
  38. $this->host = $container->get('request')->getServerParams()['HTTP_HOST'];
  39. }
  40. /**
  41. * Get the base url for css/js/images
  42. *
  43. * @param string[] $args
  44. * @return string
  45. */
  46. public function assetUrl(string ...$args): string
  47. {
  48. $baseUrl = rtrim($this->url(''), '/')
  49. . $this->config->get('asset_path');
  50. array_unshift($args, $baseUrl);
  51. return implode('/', $args);
  52. }
  53. /**
  54. * Generate a proper url from the path
  55. *
  56. * @param string $path
  57. * @return string
  58. */
  59. public function url(string $path): string
  60. {
  61. $path = trim($path, '/');
  62. $path = preg_replace('`{/.*?}`i', '', $path);
  63. // Remove any optional parameters from the route
  64. // and replace them with existing route parameters, if they exist
  65. $path_segments = explode('/', $path);
  66. $segment_count = count($path_segments);
  67. $segments = $this->segments();
  68. for ($i = 0; $i < $segment_count; $i++)
  69. {
  70. if ( ! array_key_exists($i + 1, $segments))
  71. {
  72. $segments[$i + 1] = '';
  73. }
  74. $path_segments[$i] = preg_replace('`{.*?}`', $segments[$i + 1], $path_segments[$i]);
  75. }
  76. $path = implode('/', $path_segments);
  77. $scheme = $this->config->get('secure_urls') !== FALSE ? 'https:' : 'http:';
  78. return "{$scheme}//{$this->host}/{$path}";
  79. }
  80. /**
  81. * Full default path for the list pages
  82. *
  83. * @param string $type
  84. * @throws InvalidArgumentException
  85. * @return string
  86. */
  87. public function defaultUrl(string $type): string
  88. {
  89. $type = trim($type);
  90. $defaultPath = $this->config->get("default_{$type}_list_path");
  91. if ($defaultPath !== NULL)
  92. {
  93. return $this->url("{$type}/{$defaultPath}");
  94. }
  95. throw new InvalidArgumentException("Invalid default type: '{$type}'");
  96. }
  97. }
  98. // End of UrlGenerator.php