File tree 3 files changed +102
-0
lines changed
3 files changed +102
-0
lines changed Original file line number Diff line number Diff line change 5
5
namespace LaravelLang \Routes ;
6
6
7
7
use Closure ;
8
+ use Illuminate \Foundation \Application ;
9
+ use Illuminate \Http \Request ;
8
10
use Illuminate \Support \Facades \Route ;
9
11
use Illuminate \Support \ServiceProvider as BaseServiceProvider ;
10
12
use LaravelLang \Routes \Facades \LocalizationRoute ;
13
+ use LaravelLang \Routes \Services \UrlGenerator ;
11
14
12
15
class ServiceProvider extends BaseServiceProvider
13
16
{
14
17
public function boot (): void
18
+ {
19
+ $ this ->registerGroup ();
20
+ $ this ->urlGenerator ();
21
+ }
22
+
23
+ protected function registerGroup (): void
15
24
{
16
25
Route::macro ('localizedGroup ' , fn (Closure $ callback ) => LocalizationRoute::group ($ callback ));
17
26
}
27
+
28
+ protected function urlGenerator (): void
29
+ {
30
+ $ this ->app ->singleton ('url ' , function ($ app ) {
31
+ $ routes = $ app ['router ' ]->getRoutes ();
32
+
33
+ $ app ->instance ('routes ' , $ routes );
34
+
35
+ return new UrlGenerator (
36
+ $ routes ,
37
+ $ app ->rebinding ('request ' , $ this ->requestRebinder ()),
38
+ $ app ['config ' ]['app.asset_url ' ]
39
+ );
40
+ });
41
+ }
42
+
43
+ protected function requestRebinder (): Closure
44
+ {
45
+ return fn (Application $ app , Request $ request ) => $ app ['url ' ]->setRequest ($ request );
46
+ }
18
47
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace LaravelLang \Routes \Services ;
6
+
7
+ use Illuminate \Routing \UrlGenerator as BaseGenerator ;
8
+ use Illuminate \Support \Str ;
9
+ use LaravelLang \Config \Facades \Config ;
10
+
11
+ use function app ;
12
+ use function array_merge ;
13
+ use function is_null ;
14
+
15
+ class UrlGenerator extends BaseGenerator
16
+ {
17
+ public function route ($ name , $ parameters = [], $ absolute = true ): string
18
+ {
19
+ if ($ this ->isLocalizedGroupRoute ($ name ) && ! is_null ($ route = $ this ->routes ->getByName ($ name ))) {
20
+ return $ this ->toRoute ($ route , $ this ->resolveLocalizedParameters ($ parameters ), $ absolute );
21
+ }
22
+
23
+ return parent ::route ($ name , $ parameters , $ absolute );
24
+ }
25
+
26
+ protected function isLocalizedGroupRoute (string $ name ): bool
27
+ {
28
+ return Str::startsWith ($ name , $ this ->localizedRoutePrefix ());
29
+ }
30
+
31
+ protected function resolveLocalizedParameters (array $ parameters ): array
32
+ {
33
+ return array_merge ([
34
+ $ this ->localizeRouteParameter () => app ()->getLocale (),
35
+ ], $ parameters );
36
+ }
37
+
38
+ protected function localizedRoutePrefix (): string
39
+ {
40
+ return Config::shared ()->routes ->namePrefix ;
41
+ }
42
+
43
+ protected function localizeRouteParameter (): string
44
+ {
45
+ return Config::shared ()->routes ->names ->parameter ;
46
+ }
47
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ use LaravelLang \Config \Facades \Config ;
6
+ use Tests \Constants \LocaleValue ;
7
+
8
+ test ('localized route generation ' , function () {
9
+ $ name = Config::shared ()->routes ->namePrefix ;
10
+ $ locale = LocaleValue::LocaleMain;
11
+ $ fallback = LocaleValue::LocaleAliasParent;
12
+
13
+ expect (route ($ name . 'via.group.facade ' , ['foo ' => 'bar ' ]))
14
+ ->toEndWith ("localhost/ $ locale/group/facade/bar " );
15
+
16
+ expect (route ($ name . 'via.group.macro ' , ['foo ' => 'bar ' , 'locale ' => $ fallback ]))
17
+ ->toEndWith ("localhost/ $ fallback/group/macro/bar " );
18
+ });
19
+
20
+ test ('non-localized route generation ' , function () {
21
+ expect (route ('via.group.facade ' , ['foo ' => 'bar ' ]))
22
+ ->toEndWith ('localhost/group/facade/bar ' );
23
+
24
+ expect (route ('via.group.macro ' , ['foo ' => 'bar ' ]))
25
+ ->toEndWith ('localhost/group/macro/bar ' );
26
+ });
You can’t perform that action at this time.
0 commit comments