1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CaribouFute\LocaleRoute\Prefix; |
4
|
|
|
|
5
|
|
|
use CaribouFute\LocaleRoute\LocaleConfig; |
6
|
|
|
use Illuminate\Foundation\Application; |
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
|
|
|
|
15
|
|
|
protected $localeConfig; |
16
|
|
|
protected $url; |
17
|
|
|
protected $router; |
18
|
|
|
protected $app; |
19
|
|
|
|
20
|
53 |
|
public function __construct(LocaleConfig $localeConfig, UrlGenerator $url, IlluminateRouter $router, Application |
21
|
|
|
$app) |
22
|
|
|
{ |
23
|
53 |
|
parent::__construct($localeConfig); |
24
|
53 |
|
$this->url = $url; |
25
|
53 |
|
$this->router = $router; |
26
|
53 |
|
$this->app = $app; |
27
|
|
|
} |
28
|
|
|
|
29
|
11 |
|
public function localeRoute( |
30
|
|
|
$locale = null, |
31
|
|
|
$name = null, |
32
|
|
|
$parameters = [], |
33
|
|
|
$absolute = true |
34
|
|
|
) { |
35
|
11 |
|
$locale = $locale ?: $this->app->getLocale(); |
36
|
11 |
|
$name = $name ?: $this->getCurrentRouteName(); |
37
|
11 |
|
$localeName = $this->switchLocale($locale, $name); |
38
|
|
|
|
39
|
11 |
|
return $this->getLocaleOrNotLocaleRouteUrl($localeName, $name, $parameters, $absolute); |
40
|
|
|
} |
41
|
|
|
|
42
|
11 |
|
protected function getLocaleOrNotLocaleRouteUrl( |
43
|
|
|
$localeName = null, |
44
|
|
|
$name = null, |
45
|
|
|
$parameters = [], |
46
|
|
|
$absolute = true |
47
|
|
|
) { |
48
|
|
|
try { |
49
|
11 |
|
$url = $this->url->route($localeName, $parameters, $absolute); |
50
|
2 |
|
} catch (InvalidArgumentException $e) { |
51
|
2 |
|
$url = $this->url->route($name, $parameters, $absolute); |
52
|
|
|
} |
53
|
|
|
|
54
|
11 |
|
$url = rtrim($url, '?'); |
55
|
|
|
|
56
|
11 |
|
return $url; |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
public function getCurrentRouteName() |
60
|
|
|
{ |
61
|
4 |
|
$currentRoute = $this->router->current(); |
62
|
4 |
|
$currentRouteName = $currentRoute ? $currentRoute->getName() : ''; |
63
|
|
|
|
64
|
4 |
|
return $currentRouteName; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
public function otherLocale($locale = null, $parameters = null, $absolute = true) |
68
|
|
|
{ |
69
|
2 |
|
$name = $this->getCurrentRouteName(); |
70
|
2 |
|
$parameters = $parameters ?: $this->getCurrentRouteParameters(); |
71
|
|
|
|
72
|
2 |
|
return $this->localeRoute($locale, $name, $parameters, $absolute); |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
public function getCurrentRouteParameters() |
76
|
|
|
{ |
77
|
2 |
|
$currentRoute = $this->router->current(); |
78
|
2 |
|
$currentRouteParameters = $currentRoute ? $currentRoute->parameters() : []; |
79
|
|
|
|
80
|
2 |
|
return $currentRouteParameters; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
public function otherRoute($name, $parameters = null, $absolute = true) |
84
|
|
|
{ |
85
|
2 |
|
return $this->localeRoute(null, $name, $parameters, $absolute); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|