Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/**
4
 * This file is part of the Carbon package.
5
 *
6
 * (c) Brian Nesbitt <brian@nesbot.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Carbon\Laravel;
13
 
14
use Carbon\Carbon;
15
use Carbon\CarbonImmutable;
16
use Carbon\CarbonInterval;
17
use Carbon\CarbonPeriod;
18
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
19
use Illuminate\Events\Dispatcher;
20
use Illuminate\Events\EventDispatcher;
21
use Illuminate\Support\Carbon as IlluminateCarbon;
22
use Illuminate\Support\Facades\Date;
23
use Throwable;
24
 
25
class ServiceProvider extends \Illuminate\Support\ServiceProvider
26
{
27
    /** @var callable|null */
28
    protected $appGetter = null;
29
 
30
    /** @var callable|null */
31
    protected $localeGetter = null;
32
 
33
    public function setAppGetter(?callable $appGetter): void
34
    {
35
        $this->appGetter = $appGetter;
36
    }
37
 
38
    public function setLocaleGetter(?callable $localeGetter): void
39
    {
40
        $this->localeGetter = $localeGetter;
41
    }
42
 
43
    public function boot()
44
    {
45
        $this->updateLocale();
46
 
47
        if (!$this->app->bound('events')) {
48
            return;
49
        }
50
 
51
        $service = $this;
52
        $events = $this->app['events'];
53
 
54
        if ($this->isEventDispatcher($events)) {
55
            $events->listen(class_exists('Illuminate\Foundation\Events\LocaleUpdated') ? 'Illuminate\Foundation\Events\LocaleUpdated' : 'locale.changed', function () use ($service) {
56
                $service->updateLocale();
57
            });
58
        }
59
    }
60
 
61
    public function updateLocale()
62
    {
63
        $locale = $this->getLocale();
64
 
65
        if ($locale === null) {
66
            return;
67
        }
68
 
69
        Carbon::setLocale($locale);
70
        CarbonImmutable::setLocale($locale);
71
        CarbonPeriod::setLocale($locale);
72
        CarbonInterval::setLocale($locale);
73
 
74
        if (class_exists(IlluminateCarbon::class)) {
75
            IlluminateCarbon::setLocale($locale);
76
        }
77
 
78
        if (class_exists(Date::class)) {
79
            try {
80
                $root = Date::getFacadeRoot();
81
                $root->setLocale($locale);
82
            } catch (Throwable $e) {
83
                // Non Carbon class in use in Date facade
84
            }
85
        }
86
    }
87
 
88
    public function register()
89
    {
90
        // Needed for Laravel < 5.3 compatibility
91
    }
92
 
93
    protected function getLocale()
94
    {
95
        if ($this->localeGetter) {
96
            return ($this->localeGetter)();
97
        }
98
 
99
        $app = $this->getApp();
100
        $app = $app && method_exists($app, 'getLocale')
101
            ? $app
102
            : $this->getGlobalApp('translator');
103
 
104
        return $app ? $app->getLocale() : null;
105
    }
106
 
107
    protected function getApp()
108
    {
109
        if ($this->appGetter) {
110
            return ($this->appGetter)();
111
        }
112
 
113
        return $this->app ?? $this->getGlobalApp();
114
    }
115
 
116
    protected function getGlobalApp(...$args)
117
    {
118
        return \function_exists('app') ? \app(...$args) : null;
119
    }
120
 
121
    protected function isEventDispatcher($instance)
122
    {
123
        return $instance instanceof EventDispatcher
124
            || $instance instanceof Dispatcher
125
            || $instance instanceof DispatcherContract;
126
    }
127
}