From 82d06c66cdf1e3ed17783842886d87cbd5aadd43 Mon Sep 17 00:00:00 2001 From: Conrad Carpenter Date: Sun, 8 Sep 2019 15:56:19 +0200 Subject: [PATCH] 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 --- .../RouteTranslationsCacheCommand.php | 50 +++++-------------- .../Commands/RouteTranslationsListCommand.php | 17 +++---- .../Traits/LoadsTranslatedCachedRoutes.php | 16 +++++- 3 files changed, 33 insertions(+), 50 deletions(-) diff --git a/src/Mcamara/LaravelLocalization/Commands/RouteTranslationsCacheCommand.php b/src/Mcamara/LaravelLocalization/Commands/RouteTranslationsCacheCommand.php index 52822d3..c54d250 100644 --- a/src/Mcamara/LaravelLocalization/Commands/RouteTranslationsCacheCommand.php +++ b/src/Mcamara/LaravelLocalization/Commands/RouteTranslationsCacheCommand.php @@ -2,14 +2,12 @@ namespace Mcamara\LaravelLocalization\Commands; +use Illuminate\Foundation\Console\RouteCacheCommand; use Mcamara\LaravelLocalization\LaravelLocalization; use Mcamara\LaravelLocalization\Traits\TranslatedRouteCommandContext; -use Illuminate\Console\Command; -use Illuminate\Contracts\Console\Kernel; -use Illuminate\Filesystem\Filesystem; use Illuminate\Routing\RouteCollection; -class RouteTranslationsCacheCommand extends Command +class RouteTranslationsCacheCommand extends RouteCacheCommand { use TranslatedRouteCommandContext; @@ -23,24 +21,6 @@ class RouteTranslationsCacheCommand extends Command */ protected $description = 'Create a route cache file for faster route registration for all locales'; - /** - * The filesystem instance. - * - * @var Filesystem - */ - protected $files; - - /** - * Create a new route command instance. - * - * @param Filesystem $files - */ - public function __construct(Filesystem $files) - { - parent::__construct(); - - $this->files = $files; - } /** * Execute the console command. @@ -67,7 +47,7 @@ protected function cacheRoutesPerLocale() foreach ($allLocales as $locale) { - $routes = $this->getFreshApplicationRoutes($locale); + $routes = $this->getFreshApplicationRoutesForLocale($locale); if (count($routes) == 0) { $this->error("Your application doesn't have any routes."); @@ -85,31 +65,27 @@ protected function cacheRoutesPerLocale() } /** - * Boot a fresh copy of the application and get the routes. + * Boot a fresh copy of the application and get the routes for a given locale. * * @param string|null $locale * @return \Illuminate\Routing\RouteCollection */ - protected function getFreshApplicationRoutes($locale = null) + protected function getFreshApplicationRoutesForLocale($locale = null) { - $app = require $this->getBootstrapPath() . '/app.php'; - - if (null !== $locale) { - - $key = LaravelLocalization::ENV_ROUTE_KEY; + if ($locale === null) { + return $this->getFreshApplicationRoutes(); + } - putenv("{$key}={$locale}"); - $app->make(Kernel::class)->bootstrap(); + $key = LaravelLocalization::ENV_ROUTE_KEY; - putenv("{$key}="); + putenv("{$key}={$locale}"); - } else { + $routes = $this->getFreshApplicationRoutes(); - $app->make(Kernel::class)->bootstrap(); - } + putenv("{$key}="); - return $app['router']->getRoutes(); + return $routes; } /** diff --git a/src/Mcamara/LaravelLocalization/Commands/RouteTranslationsListCommand.php b/src/Mcamara/LaravelLocalization/Commands/RouteTranslationsListCommand.php index fe78f32..13c8d4d 100644 --- a/src/Mcamara/LaravelLocalization/Commands/RouteTranslationsListCommand.php +++ b/src/Mcamara/LaravelLocalization/Commands/RouteTranslationsListCommand.php @@ -28,11 +28,6 @@ class RouteTranslationsListCommand extends RouteListCommand */ public function handle() { - if (count($this->routes) == 0) { - $this->error("Your application doesn't have any routes."); - return; - } - $locale = $this->argument('locale'); if ( ! $this->isSupportedLocale($locale)) { @@ -40,18 +35,18 @@ public function handle() return; } - $this->routes = $this->getFreshApplicationRoutes($locale); + $this->loadFreshApplicationRoutes($locale); - $this->displayRoutes($this->getRoutes()); + parent::handle(); } /** - * Boot a fresh copy of the application and get the routes. + * Boot a fresh copy of the application and replace the router/routes. * * @param string $locale - * @return \Illuminate\Routing\RouteCollection + * @return void */ - protected function getFreshApplicationRoutes($locale) + protected function loadFreshApplicationRoutes($locale) { $app = require $this->getBootstrapPath() . '/app.php'; @@ -63,7 +58,7 @@ protected function getFreshApplicationRoutes($locale) putenv("{$key}="); - return $app['router']->getRoutes(); + $this->router = $app['router']; } /** diff --git a/src/Mcamara/LaravelLocalization/Traits/LoadsTranslatedCachedRoutes.php b/src/Mcamara/LaravelLocalization/Traits/LoadsTranslatedCachedRoutes.php index 783c046..b0d9d04 100644 --- a/src/Mcamara/LaravelLocalization/Traits/LoadsTranslatedCachedRoutes.php +++ b/src/Mcamara/LaravelLocalization/Traits/LoadsTranslatedCachedRoutes.php @@ -58,8 +58,12 @@ protected function makeLocaleRoutesPath($locale, $localeKeys) { $path = $this->getDefaultCachedRoutePath(); - $localeSegment = request()->segment(1); - if ( ! $localeSegment || ! in_array($localeSegment, $localeKeys)) { + // If we have no specifically forced locale, use default or let the request determine it. + if ($locale === null) { + $locale = $this->getLocaleFromRequest(); + } + + if ( ! $locale || ! in_array($locale, $localeKeys)) { return $path; } @@ -76,6 +80,14 @@ protected function getDefaultCachedRoutePath() return $this->app->getCachedRoutesPath(); } + /** + * @return string|null + */ + protected function getLocaleFromRequest() + { + return request()->segment(1); + } + /** * @return \Mcamara\LaravelLocalization\LaravelLocalization */