1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CaribouFute\LocaleRoute\Routing; |
4
|
|
|
|
5
|
|
|
use CaribouFute\LocaleRoute\Prefix\Route as PrefixRoute; |
6
|
|
|
use CaribouFute\LocaleRoute\Prefix\Url as PrefixUrl; |
7
|
|
|
use CaribouFute\LocaleRoute\Routing\Router; |
8
|
|
|
use CaribouFute\LocaleRoute\Traits\ConfigParams; |
9
|
|
|
use CaribouFute\LocaleRoute\Traits\ConvertToControllerAction; |
10
|
|
|
|
11
|
|
|
class LocaleRouter |
12
|
|
|
{ |
13
|
|
|
use ConvertToControllerAction; |
14
|
|
|
use ConfigParams; |
15
|
|
|
|
16
|
|
|
protected $router; |
17
|
|
|
protected $prefixRoute; |
18
|
|
|
protected $prefixUrl; |
19
|
|
|
|
20
|
46 |
|
public function __construct(Router $router, PrefixRoute $prefixRoute, PrefixUrl $prefixUrl) |
21
|
|
|
{ |
22
|
46 |
|
$this->router = $router; |
23
|
46 |
|
$this->prefixRoute = $prefixRoute; |
24
|
46 |
|
$this->prefixUrl = $prefixUrl; |
25
|
46 |
|
} |
26
|
|
|
|
27
|
12 |
|
public function get($route, $action, array $options = []) |
28
|
|
|
{ |
29
|
12 |
|
$this->makeRoutes('get', $route, $action, $options); |
30
|
12 |
|
} |
31
|
|
|
|
32
|
2 |
|
public function post($route, $action, array $options = []) |
33
|
|
|
{ |
34
|
2 |
|
$this->makeRoutes('post', $route, $action, $options); |
35
|
2 |
|
} |
36
|
|
|
|
37
|
2 |
|
public function put($route, $action, array $options = []) |
38
|
|
|
{ |
39
|
2 |
|
$this->makeRoutes('put', $route, $action, $options); |
40
|
2 |
|
} |
41
|
|
|
|
42
|
2 |
|
public function patch($route, $action, array $options = []) |
43
|
|
|
{ |
44
|
2 |
|
$this->makeRoutes('patch', $route, $action, $options); |
45
|
2 |
|
} |
46
|
|
|
|
47
|
2 |
|
public function delete($route, $action, array $options = []) |
48
|
|
|
{ |
49
|
2 |
|
$this->makeRoutes('delete', $route, $action, $options); |
50
|
2 |
|
} |
51
|
|
|
|
52
|
2 |
|
public function options($route, $action, array $options = []) |
53
|
|
|
{ |
54
|
2 |
|
$this->makeRoutes('options', $route, $action, $options); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
22 |
|
public function makeRoutes($method, $route, $action, array $options = []) |
58
|
|
|
{ |
59
|
22 |
|
foreach ($this->locales() as $locale) { |
60
|
22 |
|
$this->makeRoute($locale, $method, $route, $action, $options); |
61
|
|
|
} |
62
|
22 |
|
} |
63
|
|
|
|
64
|
22 |
|
public function makeRoute($locale, $method, $route, $action, array $options = []) |
65
|
|
|
{ |
66
|
22 |
|
$url = $this->prefixUrl->rawRouteUrl($locale, $route, $options); |
67
|
|
|
|
68
|
22 |
|
$action = $this->convertToControllerAction($action); |
69
|
|
|
|
70
|
22 |
|
$action['locale'] = $locale; |
71
|
22 |
|
$action['as'] = $route; |
72
|
22 |
|
$middleware = isset($options['middleware']) ? $options['middleware'] : []; |
73
|
|
|
|
74
|
22 |
|
$this->router |
75
|
22 |
|
->$method($url, $action) |
76
|
22 |
|
->middleware($middleware); |
77
|
22 |
|
} |
78
|
|
|
} |
79
|
|
|
|