@@ -10,16 +10,23 @@ class UrlGenerator
10
10
/**
11
11
* The application instance.
12
12
*
13
- * @var Application
13
+ * @var \Laravel\Lumen\ Application
14
14
*/
15
15
protected $ app ;
16
16
17
17
/**
18
- * The cached URL scheme for generating URLs .
18
+ * The forced URL root .
19
19
*
20
- * @var string|null
20
+ * @var string
21
+ */
22
+ protected $ forcedRoot ;
23
+
24
+ /**
25
+ * The forced schema for URLs.
26
+ *
27
+ * @var string
21
28
*/
22
- protected $ cachedScheme ;
29
+ protected $ forceScheme ;
23
30
24
31
/**
25
32
* The cached URL root.
@@ -29,16 +36,16 @@ class UrlGenerator
29
36
protected $ cachedRoot ;
30
37
31
38
/**
32
- * The URL schema to be forced on all generated URLs .
39
+ * A cached copy of the URL schema for the current request .
33
40
*
34
41
* @var string|null
35
42
*/
36
- protected $ forceSchema ;
43
+ protected $ cachedSchema ;
37
44
38
45
/**
39
46
* Create a new URL redirector instance.
40
47
*
41
- * @param Application $app
48
+ * @param \Laravel\Lumen\ Application $app
42
49
* @return void
43
50
*/
44
51
public function __construct (Application $ app )
@@ -127,7 +134,7 @@ public function asset($path, $secure = null)
127
134
// Once we get the root URL, we will check to see if it contains an index.php
128
135
// file in the paths. If it does, we will remove it since it is not needed
129
136
// for asset paths, but only for routes to endpoints in the application.
130
- $ root = $ this ->getRootUrl ($ this ->getScheme ($ secure ));
137
+ $ root = $ this ->getRootUrl ($ this ->formatScheme ($ secure ));
131
138
132
139
return $ this ->removeIndex ($ root ).'/ ' .trim ($ path , '/ ' );
133
140
}
@@ -145,7 +152,7 @@ public function assetFrom($root, $path, $secure = null)
145
152
// Once we get the root URL, we will check to see if it contains an index.php
146
153
// file in the paths. If it does, we will remove it since it is not needed
147
154
// for asset paths, but only for routes to endpoints in the application.
148
- $ root = $ this ->getRootUrl ($ this ->getScheme ($ secure ), $ root );
155
+ $ root = $ this ->getRootUrl ($ this ->formatScheme ($ secure ), $ root );
149
156
150
157
return $ this ->removeIndex ($ root ).'/ ' .trim ($ path , '/ ' );
151
158
}
@@ -179,25 +186,55 @@ public function secureAsset($path)
179
186
*
180
187
* @param bool|null $secure
181
188
* @return string
189
+ * @deprecated v5.5.x
182
190
*/
183
191
protected function getScheme ($ secure )
184
192
{
185
- if (is_null ($ secure )) {
186
- return $ this ->forceSchema ?: $ this ->app ->make ('request ' )->getScheme ().':// ' ;
187
- }
188
-
189
- return $ secure ? 'https:// ' : 'http:// ' ;
193
+ return $ this ->formatScheme ($ secure );
190
194
}
191
195
192
196
/**
193
197
* Force the schema for URLs.
194
198
*
195
199
* @param string $schema
196
200
* @return void
201
+ * @deprecated v5.5.x
197
202
*/
198
203
public function forceSchema ($ schema )
199
204
{
200
- $ this ->forceSchema = $ schema .':// ' ;
205
+ $ this ->forceScheme ($ schema );
206
+ }
207
+
208
+ /**
209
+ * Force the schema for URLs.
210
+ *
211
+ * @param string $schema
212
+ * @return void
213
+ */
214
+ public function forceScheme ($ schema )
215
+ {
216
+ $ this ->cachedSchema = null ;
217
+
218
+ $ this ->forceScheme = $ schema .':// ' ;
219
+ }
220
+
221
+ /**
222
+ * Get the default scheme for a raw URL.
223
+ *
224
+ * @param bool|null $secure
225
+ * @return string
226
+ */
227
+ public function formatScheme ($ secure )
228
+ {
229
+ if (! is_null ($ secure )) {
230
+ return $ secure ? 'https:// ' : 'http:// ' ;
231
+ }
232
+
233
+ if (is_null ($ this ->cachedSchema )) {
234
+ <
9E88
div class="diff-text-inner"> $ this ->cachedSchema = $ this ->forceScheme ?: $ this ->app ->make ('request ' )->getScheme ().':// ' ;
235
+ }
236
+
237
+ return $ this ->cachedSchema ;
201
238
}
202
239
203
240
/**
@@ -239,7 +276,7 @@ public function route($name, $parameters = [], $secure = null)
239
276
* @param string $path
240
277
* @return bool
241
278
*/
242
- protected function isValidUrl ($ path )
279
+ public function isValidUrl ($ path )
243
280
{
244
281
if (starts_with ($ path , ['# ' , '// ' , 'mailto: ' , 'tel: ' , 'http:// ' , 'https:// ' ])) {
245
282
return true ;
@@ -257,11 +294,11 @@ protected function isValidUrl($path)
257
294
protected function getSchemeForUrl ($ secure )
258
295
{
259
296
if (is_null ($ secure )) {
260
- if (is_null ($ this ->cachedScheme )) {
261
- $ this ->cachedScheme = $ this ->getScheme ($ secure );
297
+ if (is_null ($ this ->cachedSchema )) {
298
+ $ this ->cachedSchema = $ this ->formatScheme ($ secure );
262
299
}
263
300
264
- return $ this ->cachedScheme ;
301
+ return $ this ->cachedSchema ;
265
302
}
266
303
267
304
return $ secure ? 'https:// ' : 'http:// ' ;
@@ -308,7 +345,7 @@ protected function getRootUrl($scheme, $root = null)
308
345
{
309
346
if (is_null ($ root )) {
310
347
if (is_null ($ this ->cachedRoot )) {
311
- $ this ->cachedRoot = $ this ->app ->make ('request ' )->root ();
348
+ $ this ->cachedRoot = $ this ->forcedRoot ?: $ this -> app ->make ('request ' )->root ();
312
349
}
313
350
314
351
$ root = $ this ->cachedRoot ;
@@ -319,6 +356,19 @@ protected function getRootUrl($scheme, $root = null)
319
356
return preg_replace ('~ ' .$ start .'~ ' , $ scheme , $ root , 1 );
320
357
}
321
358
359
+ /**
360
+ * Set the forced root URL.
361
+ *
362
+ * @param string $root
363
+ * @return void
364
+ */
365
+ public function forceRootUrl ($ root )
366
+ {
367
+ $ this ->forcedRoot = rtrim ($ root , '/ ' );
368
+
369
+ $ this ->cachedRoot = null ;
370
+ }
371
+
322
372
/**
323
373
* Format the given URL segments into a single URL.
324
374
*
0 commit comments