2015-06-26 12:03:42 -04:00
|
|
|
<?php
|
2015-09-14 19:54:34 -04:00
|
|
|
/**
|
|
|
|
* Generate full urls from fragments
|
|
|
|
*/
|
2015-09-15 13:19:29 -04:00
|
|
|
namespace Aviat\AnimeClient;
|
2015-06-26 16:39:10 -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
|
|
|
*/
|
2015-09-14 15:49:20 -04:00
|
|
|
class UrlGenerator extends RoutingBase {
|
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"), "/");
|
|
|
|
|
|
|
|
// Set the appropriate HTTP host
|
2015-09-14 10:54:50 -04:00
|
|
|
$host = $_SERVER['HTTP_HOST'];
|
2015-07-02 14:04:04 -04:00
|
|
|
$path = ($config_path !== '') ? $config_path : "";
|
|
|
|
|
|
|
|
return implode("/", ['/', $host, $path]);
|
|
|
|
}
|
|
|
|
|
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, '/');
|
|
|
|
|
|
|
|
// Remove any optional parameters from the route
|
|
|
|
$path = preg_replace('`{/.*?}`i', '', $path);
|
|
|
|
|
|
|
|
// Set the appropriate HTTP host
|
|
|
|
$host = $_SERVER['HTTP_HOST'];
|
|
|
|
|
|
|
|
return "//{$host}/{$path}";
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
$default_path = $this->__get("default_{$type}_path");
|
|
|
|
|
|
|
|
if ( ! is_null($default_path))
|
|
|
|
{
|
|
|
|
return $this->url($default_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
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_path = trim($this->__get("{$type}_path"), "/");
|
|
|
|
$config_default_route = $this->__get("default_{$type}_path");
|
|
|
|
|
|
|
|
// Remove beginning/trailing slashes
|
|
|
|
$config_path = trim($config_path, '/');
|
|
|
|
$path = trim($path, '/');
|
|
|
|
|
|
|
|
// Remove any optional parameters from the route
|
|
|
|
$path = preg_replace('`{/.*?}`i', '', $path);
|
|
|
|
|
|
|
|
// Set the appropriate HTTP host
|
2015-09-14 10:54:50 -04:00
|
|
|
$host = $_SERVER['HTTP_HOST'];
|
2015-07-02 14:04:04 -04:00
|
|
|
|
|
|
|
// Set the default view
|
|
|
|
if ($path === '')
|
|
|
|
{
|
|
|
|
$path .= trim($config_default_route, '/');
|
|
|
|
if ($this->__get('default_to_list_view')) $path .= '/list';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set an leading folder
|
2015-09-14 10:54:50 -04:00
|
|
|
/*if ($config_path !== '')
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
|
|
|
$path = "{$config_path}/{$path}";
|
2015-09-14 10:54:50 -04:00
|
|
|
}*/
|
2015-07-02 14:04:04 -04:00
|
|
|
|
|
|
|
return "//{$host}/{$path}";
|
|
|
|
}
|
2015-06-26 12:03:42 -04:00
|
|
|
}
|
2015-09-17 23:11:18 -04:00
|
|
|
// End of UrlGenerator.php
|