Defer Carbon package discovery until it is needed.
Keep Laravel from auto-discovering nesbot/carbon so the app can boot the lazy Carbon provider instead.
This commit is contained in:
@@ -0,0 +1,98 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Carbon\CarbonImmutable;
|
||||||
|
use Carbon\CarbonInterval;
|
||||||
|
use Carbon\CarbonPeriod;
|
||||||
|
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
|
||||||
|
use Illuminate\Foundation\Events\LocaleUpdated;
|
||||||
|
use Illuminate\Support\Carbon as IlluminateCarbon;
|
||||||
|
use Illuminate\Support\Facades\Date;
|
||||||
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
final class LazyCarbonServiceProvider extends ServiceProvider
|
||||||
|
{
|
||||||
|
private bool $localeListenerRegistered = false;
|
||||||
|
|
||||||
|
public function register(): void
|
||||||
|
{
|
||||||
|
$registerListener = function (DispatcherContract $events): void {
|
||||||
|
if ($this->localeListenerRegistered) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->localeListenerRegistered = true;
|
||||||
|
|
||||||
|
$events->listen(
|
||||||
|
LocaleUpdated::class,
|
||||||
|
function (LocaleUpdated $event): void {
|
||||||
|
$this->synchronizeCarbonLocale($event->locale);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
if ($this->app->resolved('events')) {
|
||||||
|
$registerListener(
|
||||||
|
$this->app->make('events')
|
||||||
|
);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->app->afterResolving(
|
||||||
|
'events',
|
||||||
|
$registerListener
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function synchronizeCarbonLocale(string $locale): void
|
||||||
|
{
|
||||||
|
$fallback = (string) $this->app['config']->get(
|
||||||
|
'app.fallback_locale',
|
||||||
|
'en'
|
||||||
|
);
|
||||||
|
|
||||||
|
Carbon::setLocale($locale);
|
||||||
|
CarbonImmutable::setLocale($locale);
|
||||||
|
CarbonPeriod::setLocale($locale);
|
||||||
|
CarbonInterval::setLocale($locale);
|
||||||
|
|
||||||
|
Carbon::setFallbackLocale($fallback);
|
||||||
|
CarbonImmutable::setFallbackLocale($fallback);
|
||||||
|
CarbonPeriod::setFallbackLocale($fallback);
|
||||||
|
CarbonInterval::setFallbackLocale($fallback);
|
||||||
|
|
||||||
|
IlluminateCarbon::setLocale($locale);
|
||||||
|
|
||||||
|
if (
|
||||||
|
method_exists(
|
||||||
|
IlluminateCarbon::class,
|
||||||
|
'setFallbackLocale'
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
IlluminateCarbon::setFallbackLocale($fallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$date = Date::getFacadeRoot();
|
||||||
|
|
||||||
|
if ($date && method_exists($date, 'setLocale')) {
|
||||||
|
$date->setLocale($locale);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
$date &&
|
||||||
|
method_exists($date, 'setFallbackLocale')
|
||||||
|
) {
|
||||||
|
$date->setFallbackLocale($fallback);
|
||||||
|
}
|
||||||
|
} catch (Throwable) {
|
||||||
|
// Preserve Carbon provider tolerance for custom Date drivers.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ return [
|
|||||||
App\Providers\AppServiceProvider::class,
|
App\Providers\AppServiceProvider::class,
|
||||||
App\Providers\AuthServiceProvider::class,
|
App\Providers\AuthServiceProvider::class,
|
||||||
App\Providers\HorizonServiceProvider::class,
|
App\Providers\HorizonServiceProvider::class,
|
||||||
|
App\Providers\LazyCarbonServiceProvider::class,
|
||||||
Klevze\ControlPanel\ServiceProvider::class,
|
Klevze\ControlPanel\ServiceProvider::class,
|
||||||
cPad\Plugins\Artworks\ServiceProvider::class,
|
cPad\Plugins\Artworks\ServiceProvider::class,
|
||||||
cPad\Plugins\Forum\ServiceProvider::class,
|
cPad\Plugins\Forum\ServiceProvider::class,
|
||||||
|
|||||||
+3
-1
@@ -102,7 +102,9 @@
|
|||||||
},
|
},
|
||||||
"extra": {
|
"extra": {
|
||||||
"laravel": {
|
"laravel": {
|
||||||
"dont-discover": []
|
"dont-discover": [
|
||||||
|
"nesbot/carbon"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
|
|||||||
Reference in New Issue
Block a user