1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CaribouFute\LocaleRoute\Prefix; |
4
|
|
|
|
5
|
|
|
use App; |
6
|
|
|
use CaribouFute\LocaleRoute\Prefix\Base; |
7
|
|
|
use Illuminate\Routing\Router as IlluminateRouter; |
8
|
|
|
use Illuminate\Routing\UrlGenerator; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
class Route extends Base |
12
|
|
|
{ |
13
|
|
|
protected $separator = '.'; |
14
|
|
|
protected $url; |
15
|
|
|
protected $router; |
16
|
|
|
|
17
|
57 |
|
public function __construct(UrlGenerator $url, IlluminateRouter $router) |
18
|
|
|
{ |
19
|
57 |
|
$this->url = $url; |
20
|
57 |
|
$this->router = $router; |
21
|
57 |
|
} |
22
|
|
|
|
23
|
11 |
|
public function localeRoute($locale = null, $name = null, $parameters = [], $absolute = true) |
24
|
|
|
{ |
25
|
11 |
|
$locale = $locale ?: App::getLocale(); |
26
|
11 |
|
$name = $name ?: $this->getCurrentRouteName(); |
27
|
11 |
|
$localeName = $this->switchLocale($locale, $name); |
28
|
|
|
|
29
|
11 |
|
return $this->getLocaleOrNotLocaleRouteUrl($localeName, $name, $parameters, $absolute); |
30
|
|
|
} |
31
|
|
|
|
32
|
11 |
|
protected function getLocaleOrNotLocaleRouteUrl($localeName = null, $name = null, $parameters = [], $absolute = true) |
33
|
|
|
{ |
34
|
|
|
try { |
35
|
11 |
|
$url = $this->url->route($localeName, $parameters, $absolute); |
36
|
2 |
|
} catch (InvalidArgumentException $e) { |
37
|
2 |
|
$url = $this->url->route($name, $parameters, $absolute); |
38
|
|
|
} |
39
|
|
|
|
40
|
11 |
|
$url = rtrim($url, '?'); |
41
|
|
|
|
42
|
11 |
|
return $url; |
43
|
|
|
} |
44
|
|
|
|
45
|
4 |
|
public function getCurrentRouteName() |
46
|
|
|
{ |
47
|
4 |
|
$currentRoute = $this->router->current(); |
48
|
4 |
|
$currentRouteName = $currentRoute ? $currentRoute->getName() : ''; |
49
|
|
|
|
50
|
4 |
|
return $currentRouteName; |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
public function otherLocale($locale = null, $parameters = null, $absolute = true) |
54
|
|
|
{ |
55
|
2 |
|
$name = $this->getCurrentRouteName(); |
56
|
2 |
|
$parameters = $parameters ?: $this->getCurrentRouteParameters(); |
57
|
|
|
|
58
|
2 |
|
return $this->localeRoute($locale, $name, $parameters, $absolute); |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
public function getCurrentRouteParameters() |
62
|
|
|
{ |
63
|
2 |
|
$currentRoute = $this->router->current(); |
64
|
2 |
|
$currentRouteParameters = $currentRoute ? $currentRoute->parameters() : []; |
65
|
|
|
|
66
|
2 |
|
return $currentRouteParameters; |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
public function otherRoute($name, $parameters = null, $absolute = true) |
70
|
|
|
{ |
71
|
2 |
|
return $this->localeRoute(null, $name, $parameters, $absolute); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|