| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
namespace Carbon\Laravel; |
| 15 |
|
| 16 |
use Carbon\Carbon; |
| 17 |
use Carbon\CarbonImmutable; |
| 18 |
use Carbon\CarbonInterval; |
| 19 |
use Carbon\CarbonPeriod; |
| 20 |
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; |
| 21 |
use Illuminate\Events\Dispatcher; |
| 22 |
use Illuminate\Events\EventDispatcher; |
| 23 |
use Illuminate\Support\Carbon as IlluminateCarbon; |
| 24 |
use Illuminate\Support\Facades\Date; |
| 25 |
use Throwable; |
| 26 |
|
| 27 |
class ServiceProvider extends \Illuminate\Support\ServiceProvider |
| 28 |
{ |
| 29 |
|
| 30 |
protected $appGetter = null; |
| 31 |
|
| 32 |
|
| 33 |
protected $localeGetter = null; |
| 34 |
|
| 35 |
|
| 36 |
protected $fallbackLocaleGetter = null; |
| 37 |
|
| 38 |
public function setAppGetter(?callable $appGetter): void |
| 39 |
{ |
| 40 |
$this->appGetter = $appGetter; |
| 41 |
} |
| 42 |
|
| 43 |
public function setLocaleGetter(?callable $localeGetter): void |
| 44 |
{ |
| 45 |
$this->localeGetter = $localeGetter; |
| 46 |
} |
| 47 |
|
| 48 |
public function setFallbackLocaleGetter(?callable $fallbackLocaleGetter): void |
| 49 |
{ |
| 50 |
$this->fallbackLocaleGetter = $fallbackLocaleGetter; |
| 51 |
} |
| 52 |
|
| 53 |
public function boot() |
| 54 |
{ |
| 55 |
$this->updateLocale(); |
| 56 |
$this->updateFallbackLocale(); |
| 57 |
|
| 58 |
if (!$this->app->bound('events')) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
$service = $this; |
| 63 |
$events = $this->app['events']; |
| 64 |
|
| 65 |
if ($this->isEventDispatcher($events)) { |
| 66 |
$events->listen(class_exists('Illuminate\Foundation\Events\LocaleUpdated') ? 'Illuminate\Foundation\Events\LocaleUpdated' : 'locale.changed', function () use ($service) { |
| 67 |
$service->updateLocale(); |
| 68 |
}); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public function updateLocale() |
| 73 |
{ |
| 74 |
$locale = $this->getLocale(); |
| 75 |
|
| 76 |
if ($locale === null) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
Carbon::setLocale($locale); |
| 81 |
CarbonImmutable::setLocale($locale); |
| 82 |
CarbonPeriod::setLocale($locale); |
| 83 |
CarbonInterval::setLocale($locale); |
| 84 |
|
| 85 |
if (class_exists(IlluminateCarbon::class)) { |
| 86 |
IlluminateCarbon::setLocale($locale); |
| 87 |
} |
| 88 |
|
| 89 |
if (class_exists(Date::class)) { |
| 90 |
try { |
| 91 |
$root = Date::getFacadeRoot(); |
| 92 |
$root->setLocale($locale); |
| 93 |
} catch (Throwable) { |
| 94 |
|
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public function updateFallbackLocale() |
| 100 |
{ |
| 101 |
$locale = $this->getFallbackLocale(); |
| 102 |
|
| 103 |
if ($locale === null) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
Carbon::setFallbackLocale($locale); |
| 108 |
CarbonImmutable::setFallbackLocale($locale); |
| 109 |
CarbonPeriod::setFallbackLocale($locale); |
| 110 |
CarbonInterval::setFallbackLocale($locale); |
| 111 |
|
| 112 |
if (class_exists(IlluminateCarbon::class) && method_exists(IlluminateCarbon::class, 'setFallbackLocale')) { |
| 113 |
IlluminateCarbon::setFallbackLocale($locale); |
| 114 |
} |
| 115 |
|
| 116 |
if (class_exists(Date::class)) { |
| 117 |
try { |
| 118 |
$root = Date::getFacadeRoot(); |
| 119 |
$root->setFallbackLocale($locale); |
| 120 |
} catch (Throwable) { |
| 121 |
|
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
public function register() |
| 127 |
{ |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
protected function getLocale() |
| 132 |
{ |
| 133 |
if ($this->localeGetter) { |
| 134 |
return ($this->localeGetter)(); |
| 135 |
} |
| 136 |
|
| 137 |
$app = $this->getApp(); |
| 138 |
$app = $app && method_exists($app, 'getLocale') |
| 139 |
? $app |
| 140 |
: $this->getGlobalApp('translator'); |
| 141 |
|
| 142 |
return $app ? $app->getLocale() : null; |
| 143 |
} |
| 144 |
|
| 145 |
protected function getFallbackLocale() |
| 146 |
{ |
| 147 |
if ($this->fallbackLocaleGetter) { |
| 148 |
return ($this->fallbackLocaleGetter)(); |
| 149 |
} |
| 150 |
|
| 151 |
$app = $this->getApp(); |
| 152 |
|
| 153 |
return $app && method_exists($app, 'getFallbackLocale') |
| 154 |
? $app->getFallbackLocale() |
| 155 |
: $this->getGlobalApp('translator')?->getFallback(); |
| 156 |
} |
| 157 |
|
| 158 |
protected function getApp() |
| 159 |
{ |
| 160 |
if ($this->appGetter) { |
| 161 |
return ($this->appGetter)(); |
| 162 |
} |
| 163 |
|
| 164 |
return $this->app ?? $this->getGlobalApp(); |
| 165 |
} |
| 166 |
|
| 167 |
protected function getGlobalApp(...$args) |
| 168 |
{ |
| 169 |
return \function_exists('app') ? \app(...$args) : null; |
| 170 |
} |
| 171 |
|
| 172 |
protected function isEventDispatcher($instance) |
| 173 |
{ |
| 174 |
return $instance instanceof EventDispatcher |
| 175 |
|| $instance instanceof Dispatcher |
| 176 |
|| $instance instanceof DispatcherContract; |
| 177 |
} |
| 178 |
} |