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