Url.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * Pimf
  4. *
  5. * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de)
  6. * @license http://krsteski.de/new-bsd-license New BSD License
  7. */
  8. namespace Pimf;
  9. use Pimf\Util\String as Str;
  10. /**
  11. * URL
  12. *
  13. * <code>
  14. * // create a URL to a location within the application
  15. * $url = Url::to('user/profile');
  16. *
  17. * // create a HTTPS URL to a location within the application
  18. * $url = Url::to('user/profile', true);
  19. * </code>
  20. *
  21. * @package Pimf
  22. * @author Gjero Krsteski <[email protected]>
  23. */
  24. class Url
  25. {
  26. /**
  27. * The cached base URL.
  28. *
  29. * @var string
  30. */
  31. public static $base;
  32. /**
  33. * Get the full URI including the query string.
  34. *
  35. * @return string
  36. */
  37. public static function full()
  38. {
  39. return static::to(Uri::full());
  40. }
  41. /**
  42. * Get the full URL for the current request.
  43. *
  44. * @return string
  45. */
  46. public static function current()
  47. {
  48. return static::to(Uri::current(), null, false);
  49. }
  50. /**
  51. * Get the URL for the application root.
  52. *
  53. * @param null|bool $https
  54. *
  55. * @return string
  56. */
  57. public static function home($https = null)
  58. {
  59. return static::to('/', $https);
  60. }
  61. /**
  62. * Get the base URL of the application.
  63. *
  64. * @return string
  65. */
  66. public static function base()
  67. {
  68. if (isset(static::$base)) {
  69. return static::$base;
  70. }
  71. $conf = Registry::get('conf');
  72. $url = $conf['app']['url'];
  73. if ($url !== '') {
  74. $base = $url;
  75. } else {
  76. $base = Registry::get('env')->getUrl();
  77. }
  78. return static::$base = $base;
  79. }
  80. /**
  81. * Generate an application URL.
  82. *
  83. * @param string $url
  84. * @param null|bool $https
  85. * @param bool $asset
  86. *
  87. * @return string
  88. */
  89. public static function to($url = '', $https = null, $asset = false)
  90. {
  91. $url = trim($url, '/');
  92. if (static::valid($url)) {
  93. return $url;
  94. }
  95. $root = self::format($https, $asset);
  96. return rtrim($root, '/') . '/' . ltrim($url, '/');
  97. }
  98. /**
  99. * Computes the URl method
  100. *
  101. * @param null|bool $https
  102. * @param bool $asset
  103. *
  104. * @return string
  105. */
  106. private static function format($https = null, $asset = false)
  107. {
  108. $root = static::base();
  109. $conf = Registry::get('conf');
  110. if (!$asset) {
  111. $root .= '/' . $conf['app']['index'];
  112. }
  113. // Unless $https is specified we set https for all secure links.
  114. if (is_null($https)) {
  115. $https = Registry::get('env')->isHttps();
  116. }
  117. // disable SSL on all framework generated links to make it more
  118. // convenient to work with the site while developing locally.
  119. if ($https and $conf['ssl']) {
  120. return preg_replace('~http://~', 'https://', $root, 1);
  121. }
  122. return preg_replace('~https://~', 'http://', $root, 1);
  123. }
  124. /**
  125. * Generate an application URL with HTTPS.
  126. *
  127. * @param string $url
  128. *
  129. * @return string
  130. */
  131. public static function as_https($url = '')
  132. {
  133. return static::to($url, true);
  134. }
  135. /**
  136. * Generate an application URL to an asset.
  137. *
  138. * @param string $url
  139. * @param bool $https
  140. *
  141. * @return string
  142. */
  143. public static function to_asset($url, $https = null)
  144. {
  145. if (static::valid($url) or static::valid('http:' . $url)) {
  146. return $url;
  147. }
  148. $conf = Registry::get('conf');
  149. $root = ($conf['app']['asset_url'] != '') ? $conf['app']['asset_url'] : false;
  150. // shoot us through a different server or third-party content delivery network.
  151. if ($root) {
  152. return rtrim($root, '/') . '/' . ltrim($url, '/');
  153. }
  154. $url = static::to($url, $https, true);
  155. // we do not need to come through the front controller.
  156. if ($conf['app']['index'] !== '') {
  157. $url = str_replace($conf['app']['index'] . '/', '', $url);
  158. }
  159. return $url;
  160. }
  161. /**
  162. * Determine if the given URL is valid.
  163. *
  164. * @param string $url
  165. *
  166. * @return bool
  167. */
  168. public static function valid($url)
  169. {
  170. if (Str::startsWith($url, '//')) {
  171. return true;
  172. }
  173. return filter_var($url, FILTER_VALIDATE_URL) !== false;
  174. }
  175. /**
  176. * Get cleaner URLs or old-fashioned » RFC 3986 URL-query string.
  177. *
  178. * @param string $route controller/action
  179. * @param array $params
  180. * @param null $https
  181. * @param bool $asset
  182. *
  183. * @return string
  184. */
  185. public static function compute($route = '', array $params = array(), $https = null, $asset = false)
  186. {
  187. // if your application should work with RFC 3986 URL-query strings
  188. $conf = Registry::get('conf');
  189. if ($conf['app']['routeable'] === false) {
  190. list($controller, $action) = explode('/', $route);
  191. $params = array_merge(compact('controller', 'action'), $params);
  192. return Str::ensureTrailing('/', self::format($https, $asset)) . '?' . http_build_query($params, null, '&');
  193. }
  194. // otherwise PIMF will serve you cleaner URLs
  195. $slug = implode('/', $params);
  196. if ($slug != '') {
  197. $slug = '/' . $slug;
  198. }
  199. return self::to($route, $https, $asset) . $slug;
  200. }
  201. }