2015-06-26 12:03:42 -04:00
|
|
|
<?php
|
2015-09-14 19:54:34 -04:00
|
|
|
/**
|
2015-11-16 11:40:01 -05:00
|
|
|
* Hummingbird Anime Client
|
|
|
|
*
|
|
|
|
* An API client for Hummingbird to manage anime and manga watch lists
|
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren
|
2016-01-04 16:58:33 -05:00
|
|
|
* @copyright Copyright (c) 2015 - 2016
|
2015-11-16 11:40:01 -05:00
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient
|
|
|
|
* @license MIT
|
2015-09-14 19:54:34 -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;
|
|
|
|
|
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
|
|
|
*/
|
2015-09-14 15:49:20 -04:00
|
|
|
class UrlGenerator extends RoutingBase {
|
2015-07-02 14:04:04 -04:00
|
|
|
|
2015-10-20 15:59:51 -04:00
|
|
|
/**
|
|
|
|
* The current HTTP host
|
|
|
|
*/
|
|
|
|
protected $host;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param ContainerInterface $container
|
|
|
|
*/
|
|
|
|
public function __construct(ContainerInterface $container)
|
|
|
|
{
|
|
|
|
parent::__construct($container);
|
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
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-09-14 10:54:50 -04:00
|
|
|
public function asset_url(/*...*/)
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
|
|
|
$args = func_get_args();
|
2015-09-14 15:49:20 -04:00
|
|
|
$base_url = rtrim($this->url(""), '/');
|
|
|
|
|
|
|
|
$base_url = "{$base_url}" . $this->__get("asset_path");
|
2015-07-02 14:04:04 -04:00
|
|
|
|
|
|
|
array_unshift($args, $base_url);
|
|
|
|
|
|
|
|
return implode("/", $args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the base url from the config
|
|
|
|
*
|
|
|
|
* @param string $type - (optional) The controller
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-10-06 10:24:48 -04:00
|
|
|
public function base_url($type = "anime")
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
|
|
|
$config_path = trim($this->__get("{$type}_path"), "/");
|
|
|
|
|
|
|
|
$path = ($config_path !== '') ? $config_path : "";
|
|
|
|
|
2015-10-20 15:59:51 -04:00
|
|
|
return implode("/", ['/', $this->host, $path]);
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
|
|
|
|
2015-09-14 10:54:50 -04:00
|
|
|
/**
|
|
|
|
* Generate a proper url from the path
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function url($path)
|
|
|
|
{
|
|
|
|
$path = trim($path, '/');
|
|
|
|
|
|
|
|
$path = preg_replace('`{/.*?}`i', '', $path);
|
|
|
|
|
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
|
|
|
|
$path_segments = explode('/', $path);
|
2015-11-04 16:53:22 -05:00
|
|
|
$segment_count = count($path_segments);
|
2015-10-20 15:59:51 -04:00
|
|
|
$segments = $this->segments();
|
|
|
|
|
2015-11-04 16:53:22 -05:00
|
|
|
for ($i = 0; $i < $segment_count; $i++)
|
2015-10-20 15:59:51 -04:00
|
|
|
{
|
|
|
|
if ( ! array_key_exists($i + 1, $segments))
|
|
|
|
{
|
|
|
|
$segments[$i + 1] = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
$path_segments[$i] = preg_replace('`{.*?}`i', $segments[$i + 1], $path_segments[$i]);
|
|
|
|
}
|
|
|
|
$path = implode('/', $path_segments);
|
2015-09-14 10:54:50 -04:00
|
|
|
|
2015-10-20 15:59:51 -04:00
|
|
|
return "//{$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
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-09-14 10:54:50 -04:00
|
|
|
public function default_url($type)
|
|
|
|
{
|
|
|
|
$type = trim($type);
|
2015-10-20 15:59:51 -04:00
|
|
|
$default_path = $this->__get("default_{$type}_list_path");
|
2015-09-14 10:54:50 -04:00
|
|
|
|
|
|
|
if ( ! is_null($default_path))
|
|
|
|
{
|
2015-10-20 15:59:51 -04:00
|
|
|
return $this->url("{$type}/{$default_path}");
|
2015-09-14 10:54:50 -04:00
|
|
|
}
|
|
|
|
|
2015-10-20 16:41:51 -04:00
|
|
|
throw new \InvalidArgumentException("Invalid default type: '{$type}'");
|
2015-09-14 10:54:50 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
/**
|
|
|
|
* Generate full url path from the route path based on config
|
|
|
|
*
|
|
|
|
* @param string $path - (optional) The route path
|
|
|
|
* @param string $type - (optional) The controller (anime or manga), defaults to anime
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-10-06 10:24:48 -04:00
|
|
|
public function full_url($path = "", $type = "anime")
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
|
|
|
$config_default_route = $this->__get("default_{$type}_path");
|
|
|
|
|
|
|
|
// Remove beginning/trailing slashes
|
|
|
|
$path = trim($path, '/');
|
|
|
|
|
|
|
|
// Set the default view
|
|
|
|
if ($path === '')
|
|
|
|
{
|
|
|
|
$path .= trim($config_default_route, '/');
|
2015-11-09 11:10:15 -05:00
|
|
|
if ($this->__get('default_to_list_view'))
|
|
|
|
{
|
|
|
|
$path .= '/list';
|
|
|
|
}
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
|
|
|
|
2015-10-20 15:59:51 -04:00
|
|
|
return $this->url($path);
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
2015-06-26 12:03:42 -04:00
|
|
|
}
|
2015-10-06 12:15:19 -04:00
|
|
|
// End of UrlGenerator.php
|