From e6f3427734cfb9d85c49703f802aca377cf0753b Mon Sep 17 00:00:00 2001 From: Janur Date: Tue, 21 Jun 2022 20:02:59 +0600 Subject: [PATCH] Feat: WIP Laravel Octane support 1. binding in service provider changed to a way proposed by Laravel Octane in case if we detect it 2. introduced a listener to hook into Octane's RequestReceived event Refs: #780 --- .../LaravelLocalizationServiceProvider.php | 29 ++++++++++++- .../Listeners/LoadLocalizedRoutesCache.php | 43 +++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 src/Mcamara/LaravelLocalization/Listeners/LoadLocalizedRoutesCache.php diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalizationServiceProvider.php b/src/Mcamara/LaravelLocalization/LaravelLocalizationServiceProvider.php index 788d8b3..982b584 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalizationServiceProvider.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalizationServiceProvider.php @@ -51,8 +51,25 @@ public function register() */ protected function registerBindings() { - $this->app->singleton(LaravelLocalization::class, function () { - return new LaravelLocalization(); + tap(fn () => new LaravelLocalization(), function ($fn) { + // the conditional check below is important + // when you do caching routes via `php artisan route:trans:cache` if binding + // via `bind` used you will get incorrect serialized translated routes in cache + // files and that's why you'll get broken translatable route URLs in UI + + // again, if you don't use translatable routes, you may get rid of this check + // and leave only 'bind()' here + if ($this->runningInOctane()) { + // the 3rd parameter is important to be passed to 'bind()' + // otherwise the package's instance will be instantiated every time + // you reference it and it won't get proper data for 'serialized translatable routes' + // class variable, this will make impossible to use translatable routes properly + // but overall the package will still work stable except generating the same URLs + // for translatable routes independently of locale + $this->app->bind(LaravelLocalization::class, $fn, true); + } else { + $this->app->singleton(LaravelLocalization::class, $fn); + } }); $this->app->alias(LaravelLocalization::class, 'laravellocalization'); @@ -73,4 +90,12 @@ protected function registerCommands() 'laravellocalizationroutecache.list', ]); } + + /** + * Checks if we are up via Laravel Octane + */ + private function runningInOctane(): bool + { + return ! $this->app->runningInConsole() && env('LARAVEL_OCTANE'); + } } diff --git a/src/Mcamara/LaravelLocalization/Listeners/LoadLocalizedRoutesCache.php b/src/Mcamara/LaravelLocalization/Listeners/LoadLocalizedRoutesCache.php new file mode 100644 index 0000000..7c7ca17 --- /dev/null +++ b/src/Mcamara/LaravelLocalization/Listeners/LoadLocalizedRoutesCache.php @@ -0,0 +1,43 @@ +request->segment(1)); + + $path = $this->makeLocaleRoutesPath($event->sandbox, $locale); + + if (self::$lastLocale != $locale && is_file($path)) { + self::$lastLocale = $locale; + include $path; + } + } + + protected function makeLocaleRoutesPath(Application $app, $locale = ''): string + { + $path = $app->getCachedRoutesPath(); + + if (! $locale) { + return $path; + } + + return substr($path, 0, -4) . '_' . $locale . '.php'; + } +}