url.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php namespace System;
  2. class URL {
  3. /**
  4. * Generate an application URL.
  5. *
  6. * @param string $url
  7. * @return string
  8. */
  9. public static function to($url = '', $https = false)
  10. {
  11. // ----------------------------------------------------
  12. // Return the URL unchanged if it is already formed.
  13. // ----------------------------------------------------
  14. if (strpos($url, '://') !== false)
  15. {
  16. return $url;
  17. }
  18. $base = Config::get('application.url');
  19. // ----------------------------------------------------
  20. // Does the URL need an HTTPS protocol?
  21. // ----------------------------------------------------
  22. if (strpos($base, 'http://') === 0 and $https)
  23. {
  24. $base = 'https://'.substr($base, 7);
  25. }
  26. return rtrim($base, '/').'/'.ltrim($url, '/');
  27. }
  28. /**
  29. * Generate an application URL with HTTPS.
  30. *
  31. * @param string $url
  32. * @return string
  33. */
  34. public static function to_secure($url = '')
  35. {
  36. return static::to($url, true);
  37. }
  38. /**
  39. * Generate a URL from a route name.
  40. *
  41. * @param string $name
  42. * @param array $parameters
  43. * @param bool $https
  44. * @return string
  45. */
  46. public static function to_route($name, $parameters = array(), $https = false)
  47. {
  48. if ( ! is_null($route = Route\Finder::find($name)))
  49. {
  50. // ----------------------------------------------------
  51. // Get the first URI assigned to the route.
  52. // ----------------------------------------------------
  53. $uris = explode(', ', key($route));
  54. $uri = substr($uris[0], strpos($uris[0], '/'));
  55. // ----------------------------------------------------
  56. // Replace any parameters in the URI.
  57. // ----------------------------------------------------
  58. foreach ($parameters as $parameter)
  59. {
  60. $uri = preg_replace('/\(\:any\)|\(\:num\)|\(.+\)/', $parameter, $uri, 1);
  61. }
  62. return static::to($uri, $https);
  63. }
  64. throw new \Exception("Error generating named route for route [$name]. Route is not defined.");
  65. }
  66. /**
  67. * Generate a HTTPS URL from a route name.
  68. *
  69. * @param string $name
  70. * @param array $parameters
  71. * @return string
  72. */
  73. public static function to_secure_route($name, $parameters = array())
  74. {
  75. return static::to_route($name, $parameters, true);
  76. }
  77. /**
  78. * Generate a URL friendly "slug".
  79. *
  80. * @param string $title
  81. * @param string $separator
  82. * @return string
  83. */
  84. public static function slug($title, $separator = '-')
  85. {
  86. // ----------------------------------------------------
  87. // Remove all characters that are not the separator,
  88. // letters, numbers, or whitespace.
  89. // ----------------------------------------------------
  90. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', Str::lower($title));
  91. // ----------------------------------------------------
  92. // Replace all separator characters and whitespace by
  93. // a single separator
  94. // ----------------------------------------------------
  95. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  96. return trim($title, $separator);
  97. }
  98. /**
  99. * Magic Method for dynamically creating route URLs.
  100. */
  101. public static function __callStatic($method, $parameters)
  102. {
  103. // ----------------------------------------------------
  104. // Dynamically create a secure route URL.
  105. // ----------------------------------------------------
  106. if (strpos($method, 'to_secure_') === 0)
  107. {
  108. return static::to_route(substr($method, 10), $parameters, true);
  109. }
  110. // ----------------------------------------------------
  111. // Dynamically create a route URL.
  112. // ----------------------------------------------------
  113. if (strpos($method, 'to_') === 0)
  114. {
  115. return static::to_route(substr($method, 3), $parameters);
  116. }
  117. throw new \Exception("Method [$method] is not defined on the URL class.");
  118. }
  119. }