Skip to content

Commit

Permalink
Fixed issues with translated route caching (#646)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
czim authored and Marc Cámara committed Sep 8, 2019
1 parent e43deb6 commit 82d06c6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
Expand All @@ -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.");
Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,25 @@ 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)) {
$this->error("Unsupported locale: '{$locale}'.");
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';

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

putenv("{$key}=");

return $app['router']->getRoutes();
$this->router = $app['router'];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
*/
Expand Down

0 comments on commit 82d06c6

Please sign in to comment.