Skip to content

Commit 82d06c6

Browse files
czimMarc Cámara
authored and
Marc Cámara
committed
Fixed issues with translated route caching (#646)
* Fixed loading of translated cached routes in console context Used to only work for requests, since it depended on the request segment to read the locale. Now, LaravelLocalization's current locale is used. * Fixed route cache and list command for Laravel 5.8
1 parent e43deb6 commit 82d06c6

File tree

3 files changed

+33
-50
lines changed

3 files changed

+33
-50
lines changed

src/Mcamara/LaravelLocalization/Commands/RouteTranslationsCacheCommand.php

+13-37
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
namespace Mcamara\LaravelLocalization\Commands;
44

5+
use Illuminate\Foundation\Console\RouteCacheCommand;
56
use Mcamara\LaravelLocalization\LaravelLocalization;
67
use Mcamara\LaravelLocalization\Traits\TranslatedRouteCommandContext;
7-
use Illuminate\Console\Command;
8-
use Illuminate\Contracts\Console\Kernel;
9-
use Illuminate\Filesystem\Filesystem;
108
use Illuminate\Routing\RouteCollection;
119

12-
class RouteTranslationsCacheCommand extends Command
10+
class RouteTranslationsCacheCommand extends RouteCacheCommand
1311
{
1412
use TranslatedRouteCommandContext;
1513

@@ -23,24 +21,6 @@ class RouteTranslationsCacheCommand extends Command
2321
*/
2422
protected $description = 'Create a route cache file for faster route registration for all locales';
2523

26-
/**
27-
* The filesystem instance.
28-
*
29-
* @var Filesystem
30-
*/
31-
protected $files;
32-
33-
/**
34-
* Create a new route command instance.
35-
*
36-
* @param Filesystem $files
37-
*/
38-
public function __construct(Filesystem $files)
39-
{
40-
parent::__construct();
41-
42-
$this->files = $files;
43-
}
4424

4525
/**
4626
* Execute the console command.
@@ -67,7 +47,7 @@ protected function cacheRoutesPerLocale()
6747

6848
foreach ($allLocales as $locale) {
6949

70-
$routes = $this->getFreshApplicationRoutes($locale);
50+
$routes = $this->getFreshApplicationRoutesForLocale($locale);
7151

7252
if (count($routes) == 0) {
7353
$this->error("Your application doesn't have any routes.");
@@ -85,31 +65,27 @@ protected function cacheRoutesPerLocale()
8565
}
8666

8767
/**
88-
* Boot a fresh copy of the application and get the routes.
68+
* Boot a fresh copy of the application and get the routes for a given locale.
8969
*
9070
* @param string|null $locale
9171
* @return \Illuminate\Routing\RouteCollection
9272
*/
93-
protected function getFreshApplicationRoutes($locale = null)
73+
protected function getFreshApplicationRoutesForLocale($locale = null)
9474
{
95-
$app = require $this->getBootstrapPath() . '/app.php';
96-
97-
if (null !== $locale) {
98-
99-
$key = LaravelLocalization::ENV_ROUTE_KEY;
75+
if ($locale === null) {
76+
return $this->getFreshApplicationRoutes();
77+
}
10078

101-
putenv("{$key}={$locale}");
10279

103-
$app->make(Kernel::class)->bootstrap();
80+
$key = LaravelLocalization::ENV_ROUTE_KEY;
10481

105-
putenv("{$key}=");
82+
putenv("{$key}={$locale}");
10683

107-
} else {
84+
$routes = $this->getFreshApplicationRoutes();
10885

109-
$app->make(Kernel::class)->bootstrap();
110-
}
86+
putenv("{$key}=");
11187

112-
return $app['router']->getRoutes();
88+
return $routes;
11389
}
11490

11591
/**

src/Mcamara/LaravelLocalization/Commands/RouteTranslationsListCommand.php

+6-11
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,25 @@ class RouteTranslationsListCommand extends RouteListCommand
2828
*/
2929
public function handle()
3030
{
31-
if (count($this->routes) == 0) {
32-
$this->error("Your application doesn't have any routes.");
33-
return;
34-
}
35-
3631
$locale = $this->argument('locale');
3732

3833
if ( ! $this->isSupportedLocale($locale)) {
3934
$this->error("Unsupported locale: '{$locale}'.");
4035
return;
4136
}
4237

43-
$this->routes = $this->getFreshApplicationRoutes($locale);
38+
$this->loadFreshApplicationRoutes($locale);
4439

45-
$this->displayRoutes($this->getRoutes());
40+
parent::handle();
4641
}
4742

4843
/**
49-
* Boot a fresh copy of the application and get the routes.
44+
* Boot a fresh copy of the application and replace the router/routes.
5045
*
5146
* @param string $locale
52-
* @return \Illuminate\Routing\RouteCollection
47+
* @return void
5348
*/
54-
protected function getFreshApplicationRoutes($locale)
49+
protected function loadFreshApplicationRoutes($locale)
5550
{
5651
$app = require $this->getBootstrapPath() . '/app.php';
5752

@@ -63,7 +58,7 @@ protected function getFreshApplicationRoutes($locale)
6358

6459
putenv("{$key}=");
6560

66-
return $app['router']->getRoutes();
61+
$this->router = $app['router'];
6762
}
6863

6964
/**

src/Mcamara/LaravelLocalization/Traits/LoadsTranslatedCachedRoutes.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ protected function makeLocaleRoutesPath($locale, $localeKeys)
5858
{
5959
$path = $this->getDefaultCachedRoutePath();
6060

61-
$localeSegment = request()->segment(1);
62-
if ( ! $localeSegment || ! in_array($localeSegment, $localeKeys)) {
61+
// If we have no specifically forced locale, use default or let the request determine it.
62+
if ($locale === null) {
63+
$locale = $this->getLocaleFromRequest();
64+
}
65+
66+
if ( ! $locale || ! in_array($locale, $localeKeys)) {
6367
return $path;
6468
}
6569

@@ -76,6 +80,14 @@ protected function getDefaultCachedRoutePath()
7680
return $this->app->getCachedRoutesPath();
7781
}
7882

83+
/**
84+
* @return string|null
85+
*/
86+
protected function getLocaleFromRequest()
87+
{
88+
return request()->segment(1);
89+
}
90+
7991
/**
8092
* @return \Mcamara\LaravelLocalization\LaravelLocalization
8193
*/

0 commit comments

Comments
 (0)