Files
spa-api/system/url.php

145 lines
3.5 KiB
PHP
Raw Normal View History

2011-06-08 23:45:08 -05:00
<?php namespace System;
class URL {
/**
* Generate an application URL.
*
2011-07-25 13:15:24 -07:00
* If the given URL is already well-formed, it will be returned unchanged.
*
2011-06-08 23:45:08 -05:00
* @param string $url
* @param bool $https
2011-07-25 13:19:39 -07:00
* @param bool $asset
2011-06-08 23:45:08 -05:00
* @return string
*/
2011-07-25 13:19:39 -07:00
public static function to($url = '', $https = false, $asset = false)
2011-06-08 23:45:08 -05:00
{
2011-07-25 13:15:24 -07:00
if (filter_var($url, FILTER_VALIDATE_URL) !== false)
2011-06-08 23:45:08 -05:00
{
return $url;
}
2011-07-25 13:15:24 -07:00
$base = Config::get('application.url').'/'.Config::get('application.index');
2011-06-08 23:45:08 -05:00
2011-07-28 11:09:13 -05:00
if ($asset and Config::get('application.index') !== '')
{
$base = str_replace('/'.Config::get('application.index'), '', $base);
}
2011-07-25 13:19:39 -07:00
2011-07-28 11:09:13 -05:00
if ($https and strpos($base, 'http://') === 0)
{
$base = 'https://'.substr($base, 7);
}
2011-06-08 23:45:08 -05:00
2011-07-27 08:51:11 -05:00
return rtrim($base, '/').'/'.ltrim($url, '/');
2011-06-08 23:45:08 -05:00
}
/**
* Generate an application URL with HTTPS.
*
* @param string $url
* @return string
*/
public static function to_secure($url = '')
{
return static::to($url, true);
}
/**
* Generate an application URL to an asset. The index file
* will not be added to the URL.
*
* @param string $url
* @return string
*/
2011-07-25 13:15:24 -07:00
public static function to_asset($url)
{
2011-07-25 13:19:39 -07:00
return static::to($url, Request::is_secure(), true);
}
2011-06-08 23:45:08 -05:00
/**
* Generate a URL from a route name.
*
2011-07-08 06:35:42 -07:00
* For routes that have wildcard parameters, an array may be passed as the
* second parameter to the method. The values of this array will be used
* to fill the wildcard segments of the route URI.
*
2011-06-08 23:45:08 -05:00
* @param string $name
* @param array $parameters
* @param bool $https
* @return string
*/
public static function to_route($name, $parameters = array(), $https = false)
{
2011-08-01 15:39:58 -05:00
if ( ! is_null($route = Routing\Finder::find($name, Routing\Loader::all())))
2011-06-08 23:45:08 -05:00
{
2011-06-14 17:27:11 -05:00
$uris = explode(', ', key($route));
2011-06-08 23:45:08 -05:00
$uri = substr($uris[0], strpos($uris[0], '/'));
foreach ($parameters as $parameter)
{
2011-06-17 00:16:55 -05:00
$uri = preg_replace('/\(.+?\)/', $parameter, $uri, 1);
2011-06-08 23:45:08 -05:00
}
return static::to($uri, $https);
}
throw new \Exception("Error generating named route for route [$name]. Route is not defined.");
}
2011-06-14 17:27:11 -05:00
/**
* Generate a HTTPS URL from a route name.
*
* @param string $name
* @param array $parameters
* @return string
*/
public static function to_secure_route($name, $parameters = array())
{
return static::to_route($name, $parameters, true);
}
2011-06-08 23:45:08 -05:00
/**
* Generate a URL friendly "slug".
*
* @param string $title
* @param string $separator
* @return string
*/
public static function slug($title, $separator = '-')
{
$title = Str::ascii($title);
2011-07-07 07:21:15 -07:00
// Remove all characters that are not the separator, letters, numbers, or whitespace.
2011-06-08 23:45:08 -05:00
$title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', Str::lower($title));
2011-07-07 07:21:15 -07:00
// Replace all separator characters and whitespace by a single separator
2011-06-08 23:45:08 -05:00
$title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
return trim($title, $separator);
}
/**
* Magic Method for dynamically creating route URLs.
*/
public static function __callStatic($method, $parameters)
{
2011-06-30 07:45:05 -07:00
$parameters = (isset($parameters[0])) ? $parameters[0] : array();
2011-06-08 23:45:08 -05:00
// Dynamically create a secure route URL.
if (strpos($method, 'to_secure_') === 0)
{
return static::to_route(substr($method, 10), $parameters, true);
}
// Dynamically create a route URL.
if (strpos($method, 'to_') === 0)
{
return static::to_route(substr($method, 3), $parameters);
}
throw new \Exception("Method [$method] is not defined on the URL class.");
}
}