Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: WIP Laravel Octane support #833

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Comment on lines +62 to +72
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

singleton() is just a bind() with true as a third parameter, so this check shouldn't make any difference.

https://github.com/laravel/framework/blob/9.x/src/Illuminate/Container/Container.php#L388

});

$this->app->alias(LaravelLocalization::class, 'laravellocalization');
Expand All @@ -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');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Mcamara\LaravelLocalization\Listeners;

use Illuminate\Foundation\Application;
use Laravel\Octane\Events\RequestReceived;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class LoadLocalizedRoutesCache
{
private static $lastLocale;


public function handle(RequestReceived $event): void
{
// passing request segment is crucial because the package doesn't
// know the current locale as it was instantiated in service provider

// (there is also an option to don't pass the request segment in case
// you don't use translatable routes (transRoute() in web.php) in your project
// in this case the package will correctly resolve the locale and you
// don't need to pass the 3rd param when binding in service provider)
$locale = LaravelLocalization::setLocale($event->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';
}
}