8000 [5.5] Improve compatibility between Laravel and Lumen UrlGenerator. · laravel/lumen-framework@9b2de84 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9b2de84

Browse files
committed
[5.5] Improve compatibility between Laravel and Lumen UrlGenerator.
* Opt for unified `URL::formatScheme()` over Lumen's `URL::getScheme()`. * Opt for unified `URL::forceScheme()` over Lumen's `URL::forceSchema()` * Add option to `forceRootUrl()` as available in Laravel. Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
1 parent 4ae8f78 commit 9b2de84

File tree

1 file changed

+70
-20
lines changed

1 file changed

+70
-20
lines changed

src/Routing/UrlGenerator.php

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,23 @@ class UrlGenerator
1010
/**
1111
* The application instance.
1212
*
13-
* @var Application
13+
* @var \Laravel\Lumen\Application
1414
*/
1515
protected $app;
1616

1717
/**
18-
* The cached URL scheme for generating URLs.
18+
* The forced URL root.
1919
*
20-
* @var string|null
20+
* @var string
21+
*/
22+
protected $forcedRoot;
23+
24+
/**
25+
* The forced schema for URLs.
26+
*
27+
* @var string
2128
*/
22-
protected $cachedScheme;
29+
protected $forceScheme;
2330

2431
/**
2532
* The cached URL root.
@@ -29,16 +36,16 @@ class UrlGenerator
2936
protected $cachedRoot;
3037

3138
/**
32-
* The URL schema to be forced on all generated URLs.
39+
* A cached copy of the URL schema for the current request.
3340
*
3441
* @var string|null
3542
*/
36-
protected $forceSchema;
43+
protected $cachedSchema;
3744

3845
/**
3946
* Create a new URL redirector instance.
4047
*
41-
* @param Application $app
48+
* @param \Laravel\Lumen\Application $app
4249
* @return void
4350
*/
4451
public function __construct(Application $app)
@@ -127,7 +134,7 @@ public function asset($path, $secure = null)
127134
// Once we get the root URL, we will check to see if it contains an index.php
128135
// file in the paths. If it does, we will remove it since it is not needed
129136
// 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));
131138

132139
return $this->removeIndex($root).'/'.trim($path, '/');
133140
}
@@ -145,7 +152,7 @@ public function assetFrom($root, $path, $secure = null)
145152
// Once we get the root URL, we will check to see if it contains an index.php
146153
// file in the paths. If it does, we will remove it since it is not needed
147154
// 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);
149156

150157
return $this->removeIndex($root).'/'.trim($path, '/');
151158
}
@@ -179,25 +186,55 @@ public function secureAsset($path)
179186
*
180187
* @param bool|null $secure
181188
* @return string
189+
* @deprecated v5.5.x
182190
*/
183191
protected function getScheme($secure)
184192
{
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);
190194
}
191195

192196
/**
193197
* Force the schema for URLs.
194198
*
195199
* @param string $schema
196200
* @return void
201+
* @deprecated v5.5.x
197202
*/
198203
public function forceSchema($schema)
199204
{
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;
201238
}
202239

203240
/**
@@ -239,7 +276,7 @@ public function route($name, $parameters = [], $secure = null)
239276
* @param string $path
240277
* @return bool
241278
*/
242-
protected function isValidUrl($path)
279+
public function isValidUrl($path)
243280
{
244281
if (starts_with($path, ['#', '//', 'mailto:', 'tel:', 'http://', 'https://'])) {
245282
return true;
@@ -257,11 +294,11 @@ protected function isValidUrl($path)
257294
protected function getSchemeForUrl($secure)
258295
{
259296
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);
262299
}
263300

264-
return $this->cachedScheme;
301+
return $this->cachedSchema;
265302
}
266303

267304
return $secure ? 'https://' : 'http://';
@@ -308,7 +345,7 @@ protected function getRootUrl($scheme, $root = null)
308345
{
309346
if (is_null($root)) {
310347
if (is_null($this->cachedRoot)) {
311-
$this->cachedRoot = $this->app->make('request')->root();
348+
$this->cachedRoot = $this->forcedRoot ?: $this->app->make('request')->root();
312349
}
313350

314351
$root = $this->cachedRoot;
@@ -319,6 +356,19 @@ protected function getRootUrl($scheme, $root = null)
319356
return preg_replace('~'.$start.'~', $scheme, $root, 1);
320357
}
321358

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+
322372
/**
323373
* Format the given URL segments into a single URL.
324374
*

0 commit comments

Comments
 (0)
0