1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Foundation\Overrides\Illuminate\Routing; |
6
|
|
|
|
7
|
|
|
use Illuminate\Routing\UrlGenerator as BaseUrlGenerator; |
8
|
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization; |
9
|
|
|
|
10
|
|
|
class UrlGenerator extends BaseUrlGenerator |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Generate an absolute URL to the given path. |
14
|
|
|
* |
15
|
|
|
* @param string $path |
16
|
|
|
* @param mixed $extra |
17
|
|
|
* @param bool|null $secure |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
public function to($path, $extra = [], $secure = null) |
21
|
|
|
{ |
22
|
|
|
if (! config('cortex.foundation.route.trailing_slash')) { |
23
|
|
|
return parent::to($path, $extra, $secure); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// First we will check if the URL is already a valid URL. If it is we will not |
27
|
|
|
// try to generate a new one but will simply return the URL as is, which is |
28
|
|
|
// convenient since developers do not always have to check if it's valid. |
29
|
|
|
if ($this->isValidUrl($path)) { |
30
|
|
|
return $path; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$tail = implode('/', array_map( |
34
|
|
|
'rawurlencode', (array) $this->formatParameters($extra)) |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
// Once we have the scheme we will compile the "tail" by collapsing the values |
38
|
|
|
// into a single string delimited by slashes. This just makes it convenient |
39
|
|
|
// for passing the array of parameters to this URL as a list of segments. |
40
|
|
|
$root = $this->formatRoot($this->formatScheme($secure)); |
41
|
|
|
|
42
|
|
|
list($path, $query) = $this->extractQueryString($path); |
43
|
|
|
|
44
|
|
|
return $this->format( |
45
|
|
|
$root, '/'.trim($path.'/'.$tail, '/') |
46
|
|
|
).'/'.$query; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
protected function routeUrl() |
53
|
|
|
{ |
54
|
|
|
if (config('cortex.foundation.route.trailing_slash') && ! $this->routeGenerator) { |
55
|
|
|
$this->routeGenerator = new RouteUrlGenerator($this, $this->request); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->routeGenerator; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function previous($fallback = false) |
65
|
|
|
{ |
66
|
|
|
return ($previousUrl = $this->request->input('previous_url')) ? $this->to($previousUrl) : parent::previous($fallback); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
protected function toRoute($route, $parameters, $absolute) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
// Bind {locale} route parameter |
75
|
|
|
if (config('cortex.foundation.route.locale_prefix') && ! isset($parameters['locale'])) { |
76
|
|
|
$urlLocale = $this->request->segment(1); |
77
|
|
|
$sessionLocale = session('locale', $defaultLocale = app('laravellocalization')->getCurrentLocale()); |
78
|
|
|
|
79
|
|
|
$parameters['locale'] = app('laravellocalization')->checkLocaleInSupportedLocales($urlLocale) ? $urlLocale |
80
|
|
|
: (app('laravellocalization')->checkLocaleInSupportedLocales($sessionLocale) ? $sessionLocale : $defaultLocale); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->routeUrl()->to( |
84
|
|
|
$route, $this->formatParameters($parameters), $absolute |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.