PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/aurora/url.php

https://gitlab.com/Bartwillemsen/aurora-framework
PHP | 250 lines | 80 code | 31 blank | 139 comment | 10 complexity | 6be0d950876bb00072218e5ba108e646 MD5 | raw file
  1. <?php
  2. namespace Aurora;
  3. class URL
  4. {
  5. /**
  6. * Get the base URL of the application.
  7. *
  8. * If the application URL is explicitly defined in the application configuration
  9. * file, that URL will be returned. Otherwise, the URL will be guessed based on
  10. * the host and script name available in the global $_SERVER array.
  11. *
  12. * @return string
  13. */
  14. public static function base()
  15. {
  16. if (($base = Config::$items['application']['url']) !== '') return $base;
  17. if (isset($_SERVER['HTTP_HOST'])) {
  18. $protocol = (Request::secure()) ? 'https://' : 'http://';
  19. // By removing the basename of the script, we should be left with the path
  20. // in which the framework is installed. For example, if the framework is
  21. // installed to http://localhost/aurora/public, the path we'll get from
  22. // this statement will be "/aurora/public".
  23. $path = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
  24. return rtrim($protocol.$_SERVER['HTTP_HOST'].$path, '/');
  25. }
  26. return 'http://localhost';
  27. }
  28. /**
  29. * Generate an application URL.
  30. *
  31. * <code>
  32. * // Create a URL to a location within the application
  33. * $url = URL::to('user/profile');
  34. *
  35. * // Create a HTTPS URL to a location within the application
  36. * $url = URL::to('user/profile', true);
  37. * </code>
  38. *
  39. * @param string $url
  40. * @param bool $https
  41. * @return string
  42. */
  43. public static function to($url = '', $https = false)
  44. {
  45. if (filter_var($url, FILTER_VALIDATE_URL) !== false) return $url;
  46. $root = static::base().'/'.Config::$items['application']['index'];
  47. // Since SSL is often not used while developing the application, we allow the
  48. // developer to disable SSL on all framework generated links to make it more
  49. // convenient to work with the site while developing.
  50. if ($https and Config::$items['application']['ssl']) {
  51. $root = preg_replace('~http://~', 'https://', $root, 1);
  52. }
  53. return rtrim($root, '/').'/'.ltrim($url, '/');
  54. }
  55. /**
  56. * Generate an application URL with HTTPS.
  57. *
  58. * @param string $url
  59. * @return string
  60. */
  61. public static function to_secure($url = '')
  62. {
  63. return static::to($url, true);
  64. }
  65. /**
  66. * Generate an application URL to an asset.
  67. *
  68. * @param string $url
  69. * @param bool $https
  70. * @return string
  71. */
  72. public static function to_asset($url, $https = null)
  73. {
  74. if (is_null($https)) $https = Request::secure();
  75. $url = static::to($url, $https);
  76. // Since assets are not served by Aurora, we do not need to come through
  77. // the front controller. We'll remove the application index specified in
  78. // the application configuration from the generated URL.
  79. if (($index = Config::$items['application']['index']) !== '') {
  80. $url = str_replace($index.'/', '', $url);
  81. }
  82. return $url;
  83. }
  84. /**
  85. * Generate a URL from a route name.
  86. *
  87. * For routes that have wildcard parameters, an array may be passed as the
  88. * second parameter to the method. The values of this array will be used to
  89. * fill the wildcard segments of the route URI.
  90. *
  91. * <code>
  92. * // Create a URL to the "profile" named route
  93. * $url = URL::to_route('profile');
  94. *
  95. * // Create a URL to the "profile" named route with wildcard parameters
  96. * $url = URL::to_route('profile', array($username));
  97. * </code>
  98. *
  99. * @param string $name
  100. * @param array $parameters
  101. * @param bool $https
  102. * @return string
  103. */
  104. public static function to_route($name, $parameters = array(), $https = false)
  105. {
  106. if (! is_null($route = IoC::core('routing.router')->find($name))) {
  107. $uris = explode(', ', key($route));
  108. $uri = substr($uris[0], strpos($uris[0], '/'));
  109. // Spin through each route parameter and replace the route wildcard
  110. // segment with the corresponding parameter passed to the method.
  111. // Afterwards, we will replace all of the remaining optional URI
  112. // segments with spaces since they may not have been specified
  113. // in the array of parameters.
  114. foreach ((array) $parameters as $parameter) {
  115. $uri = preg_replace('/\(.+?\)/', $parameter, $uri, 1);
  116. }
  117. return static::to(str_replace(array('/(:any?)', '/(:num?)'), '', $uri), $https);
  118. }
  119. throw new \OutOfBoundsException("Error creating URL for undefined route [$name].");
  120. }
  121. /**
  122. * Generate a HTTPS URL from a route name.
  123. *
  124. * @param string $name
  125. * @param array $parameters
  126. * @return string
  127. */
  128. public static function to_secure_route($name, $parameters = array())
  129. {
  130. return static::to_route($name, $parameters, true);
  131. }
  132. /**
  133. * Generate a URL to a controller action.
  134. *
  135. * <code>
  136. * // Generate a URL to the "index" method of the "user" controller
  137. * $url = URL::to_action('user@index');
  138. *
  139. * // Generate a URL to http://example.com/user/profile/harry
  140. * $url = URL::to_action('user@profile', array('harry'));
  141. * </code>
  142. *
  143. * @param string $action
  144. * @param array $parameters
  145. * @param bool $https
  146. * @return string
  147. */
  148. public static function to_action($action, $parameters = array(), $https = false)
  149. {
  150. $action = str_replace(array('.', '@'), '/', $action);
  151. return static::to($action.'/'.implode('/', $parameters), $https);
  152. }
  153. /**
  154. * Generate a HTTPS URL to a controller action.
  155. *
  156. * <code>
  157. * // Generate a HTTPS URL to the "index" method of the "user" controller
  158. * $url = URL::to_action('user@index');
  159. * </code>
  160. *
  161. * @param string $action
  162. * @param array $parameters
  163. * @param bool $https
  164. * @return string
  165. */
  166. public static function to_secure_action($action, $parameters = array())
  167. {
  168. return static::to_action($action, $parameters, true);
  169. }
  170. /**
  171. * Generate a URL friendly "slug".
  172. *
  173. * <code>
  174. * // Returns "this-is-my-blog-post
  175. * $slug = URL::slug('This is my blog post!');
  176. *
  177. * // Returns "this_is_my_blog_post
  178. * $slug = URL::slug('This is my blog post!', '_');
  179. * </code>
  180. *
  181. * @param string $title
  182. * @param string $separator
  183. * @return string
  184. */
  185. public static function slug($title, $separator = '-')
  186. {
  187. $title = Str::ascii($title);
  188. // Remove all characters that are not the seperator, letters, numbers, or whitespace.
  189. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', Str::lower($title));
  190. // Replace all seperator characters and whitespace by a single seperator
  191. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  192. return trim($title, $separator);
  193. }
  194. /**
  195. * Magic Method for dynamically creating URLs to named routes.
  196. *
  197. * <code>
  198. * // Create a URL to the "profile" named route
  199. * $url = URL::to_profile();
  200. *
  201. * // Create a URL to the "profile" named route with wildcard segments
  202. * $url = URL::to_profile(array($username));
  203. *
  204. * // Create a URL to the "profile" named route using HTTPS
  205. * $url = URL::to_secure_profile();
  206. * </code>
  207. */
  208. public static function __callStatic($method, $parameters)
  209. {
  210. $parameters = (isset($parameters[0])) ? $parameters[0] : array();
  211. if (strpos($method, 'to_secure_') === 0) {
  212. return static::to_route(substr($method, 10), $parameters, true);
  213. }
  214. if (strpos($method, 'to_') === 0) {
  215. return static::to_route(substr($method, 3), $parameters);
  216. }
  217. throw new \BadMethodCallException("Method [$method] is not defined in the URL class.");
  218. }
  219. }