url.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 = Router::find($name)))
  49. {
  50. $uris = explode(', ', key($route));
  51. // ----------------------------------------------------
  52. // Get the first URI assigned to the route.
  53. // ----------------------------------------------------
  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 URL friendly "slug".
  68. *
  69. * @param string $title
  70. * @param string $separator
  71. * @return string
  72. */
  73. public static function slug($title, $separator = '-')
  74. {
  75. // ----------------------------------------------------
  76. // Remove all characters that are not the separator,
  77. // letters, numbers, or whitespace.
  78. // ----------------------------------------------------
  79. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', Str::lower($title));
  80. // ----------------------------------------------------
  81. // Replace all separator characters and whitespace by
  82. // a single separator
  83. // ----------------------------------------------------
  84. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  85. return trim($title, $separator);
  86. }
  87. /**
  88. * Magic Method for dynamically creating route URLs.
  89. */
  90. public static function __callStatic($method, $parameters)
  91. {
  92. // ----------------------------------------------------
  93. // Dynamically create a secure route URL.
  94. // ----------------------------------------------------
  95. if (strpos($method, 'to_secure_') === 0)
  96. {
  97. return static::to_route(substr($method, 10), $parameters, true);
  98. }
  99. // ----------------------------------------------------
  100. // Dynamically create a route URL.
  101. // ----------------------------------------------------
  102. if (strpos($method, 'to_') === 0)
  103. {
  104. return static::to_route(substr($method, 3), $parameters);
  105. }
  106. throw new \Exception("Method [$method] is not defined on the URL class.");
  107. }
  108. }