1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Locale\Parsers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Illuminate\Translation\Translator; |
8
|
|
|
|
9
|
|
|
class RouteParser implements Parser |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var UrlParser |
13
|
|
|
*/ |
14
|
|
|
private $parser; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Translator |
18
|
|
|
*/ |
19
|
|
|
private $translator; |
20
|
|
|
|
21
|
|
|
private $name; |
22
|
|
|
private $parameters = []; |
23
|
|
|
private $localeslug; |
24
|
|
|
|
25
|
50 |
|
public function __construct(UrlParser $parser, Translator $translator) |
26
|
|
|
{ |
27
|
50 |
|
$this->parser = $parser; |
28
|
50 |
|
$this->translator = $translator; |
29
|
50 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Set the routename. |
33
|
|
|
* |
34
|
|
|
* @param $name |
35
|
|
|
* |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
33 |
|
public function set($name) |
39
|
|
|
{ |
40
|
33 |
|
$this->name = $name; |
41
|
|
|
|
42
|
33 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Retrieve the generated / altered url |
47
|
|
|
* If no translated routekey is found, it means the route itself does not need to be |
48
|
|
|
* translated and we allow the native url generator to deal with the route generation. |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
33 |
|
public function get() |
53
|
|
|
{ |
54
|
33 |
|
$routekey = $this->translator->get('routes.'.$this->name, [], $this->localeslug); |
55
|
|
|
|
56
|
33 |
|
$uri = ($routekey === 'routes.'.$this->name) |
57
|
27 |
|
? $this->parser->resolveRoute($this->name, $this->parameters) |
58
|
32 |
|
: $this->replaceParameters($routekey, $this->parameters); |
59
|
|
|
|
60
|
32 |
|
return $this->parser->set($uri)->get(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Place locale segment in front of url path |
65
|
|
|
* e.g. /foo/bar is transformed into /en/foo/bar. |
66
|
|
|
* |
67
|
|
|
* @param null $localeslug |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
32 |
|
public function localize($localeslug = null) |
72
|
|
|
{ |
73
|
32 |
|
$this->localeslug = $localeslug; |
74
|
32 |
|
$this->parser->localize($localeslug); |
75
|
|
|
|
76
|
32 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $parameters |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
32 |
|
public function parameters(array $parameters = []) |
85
|
|
|
{ |
86
|
32 |
|
$this->parameters = $parameters; |
87
|
|
|
|
88
|
32 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param bool $secure |
93
|
|
|
* |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
1 |
|
public function secure($secure = true) |
97
|
|
|
{ |
98
|
1 |
|
$this->parser->secure($secure); |
99
|
|
|
|
100
|
1 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Replace route parameters. |
105
|
|
|
* |
106
|
|
|
* @param $uri |
107
|
|
|
* @param array $parameters |
108
|
|
|
* |
109
|
|
|
* @return mixed|string |
110
|
|
|
*/ |
111
|
6 |
|
protected function replaceParameters($uri, $parameters = []) |
112
|
|
|
{ |
113
|
6 |
|
$parameters = (array) $parameters; |
114
|
|
|
|
115
|
6 |
|
$uri = $this->replaceRouteParameters($uri, $parameters); |
116
|
6 |
|
$uri = str_replace('//', '/', $uri); |
117
|
|
|
|
118
|
6 |
|
return $uri; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Replace all of the wildcard parameters for a route path. |
123
|
|
|
* |
124
|
|
|
* @note: based on the Illuminate\Routing\UrlGenerator code |
125
|
|
|
* |
126
|
|
|
* @param string $path |
127
|
|
|
* @param array $parameters |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
6 |
|
protected function replaceRouteParameters($path, array $parameters) |
132
|
|
|
{ |
133
|
6 |
|
$path = $this->replaceNamedParameters($path, $parameters); |
134
|
|
|
|
135
|
6 |
|
$path = preg_replace_callback('/\{.*?\}/', function ($match) use (&$parameters) { |
136
|
4 |
|
return (empty($parameters) && !Str::endsWith($match[0], '?}')) |
137
|
1 |
|
? $match[0] |
138
|
4 |
|
: array_shift($parameters); |
139
|
6 |
|
}, $path); |
140
|
|
|
|
141
|
6 |
|
return trim(preg_replace('/\{.*?\?\}/', '', $path), '/'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Replace all of the named parameters in the path. |
146
|
|
|
* |
147
|
|
|
* @note: based on the Illuminate\Routing\UrlGenerator code |
148
|
|
|
* |
149
|
|
|
* @param string $path |
150
|
|
|
* @param array $parameters |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
protected function replaceNamedParameters($path, &$parameters) |
155
|
|
|
{ |
156
|
6 |
|
return preg_replace_callback('/\{(.*?)\??\}/', function ($m) use (&$parameters) { |
157
|
6 |
|
return isset($parameters[$m[1]]) ? Arr::pull($parameters, $m[1]) : $m[0]; |
158
|
6 |
|
}, $path); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|