Completed
Push — master ( ef417d...8503aa )
by
unknown
09:00
created

NovaTranslationServiceProvider::serveNova()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BBSLab\NovaTranslation;
4
5
use BBSLab\NovaTranslation\Models\Locale;
6
use BBSLab\NovaTranslation\Models\Observers\LocaleObserver;
7
use Illuminate\Support\Facades\Route;
8
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
9
10
class NovaTranslationServiceProvider extends BaseServiceProvider
11
{
12
    /**
13
     * Package ID.
14
     *
15
     * @var string
16
     */
17
    const PACKAGE_ID = 'nova-translation';
18
19
    /**
20
     * Bootstrap Kernel.
21
     *
22
     * @return void
23
     */
24
    public function boot()
25
    {
26
        $this->bootPackage();
27
28
        Locale::observe(LocaleObserver::class);
29
30
        if ($this->isNovaInstalled()) {
31
            $this->app->booted(function () {
32
                $this->bootRoutes();
33
            });
34
35
            $this->serveNova();
36
        }
37
    }
38
39
    /**
40
     * Boot Laravel package.
41
     *
42
     * @return void
43
     */
44
    protected function bootPackage()
45
    {
46
        $this->mergeConfigFrom(__DIR__.'/../config/config.php', static::PACKAGE_ID);
47
        $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
48
        $this->loadTranslationsFrom(__DIR__.'/../resources/lang', static::PACKAGE_ID);
49
50
        $this->publishes([
51
            __DIR__.'/../../../../config/config.php' => base_path('config/'.static::PACKAGE_ID.'.php'),
52
        ]);
53
    }
54
55
    /**
56
     * Check if Laravel Nova is installed.
57
     *
58
     * @return bool
59
     */
60
    protected function isNovaInstalled()
61
    {
62
        return class_exists('Laravel\Nova\Nova');
63
    }
64
65
    /**
66
     * Register the tool's routes.
67
     *
68
     * @return void
69
     */
70
    protected function bootRoutes()
71
    {
72
        if ($this->app->routesAreCached()) {
73
            return;
74
        }
75
76
        Route::middleware(['nova'])
77
            ->namespace('BBSLab\NovaTranslation\Http\Controllers')
78
            ->domain(config('nova.domain', null))
79
            ->prefix('nova-api')
80
            ->as('nova.api.')
81
            ->group(__DIR__.'/../routes/nova.php');
82
83
        Route::middleware(['nova', \BBSLab\NovaTranslation\Http\Middleware\Authorize::class])
84
            ->prefix('nova-vendor/'.static::PACKAGE_ID)
85
            ->group(__DIR__.'/../routes/api.php');
86
    }
87
88
    /**
89
     * Serve Laravel Nova.
90
     *
91
     * @return void
92
     */
93
    protected function serveNova()
94
    {
95
        \Laravel\Nova\Nova::serving(function (\Laravel\Nova\Events\ServingNova $event) {
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
            $this->loadNovaTranslations();
97
98
            \Laravel\Nova\Nova::provideToScript([
99
                'locale' => app()->getLocale(),
100
            ]);
101
        });
102
    }
103
104
    /**
105
     * Load prefixed Nova translations.
106
     *
107
     * @return void
108
     */
109
    protected function loadNovaTranslations()
110
    {
111
        $file = __DIR__.'/../resources/lang/'.app()->getLocale().'.json';
112
        if (! file_exists($file)) {
113
            $file = __DIR__.'/../resources/lang/en.json';
114
        }
115
116
        $translations = json_decode(file_get_contents($file), true);
117
        $translations = collect($translations)->mapWithKeys(function ($value, $key) {
118
            return [static::PACKAGE_ID.'::'.$key => $value];
119
        })->toArray();
120
121
        \Laravel\Nova\Nova::translations($translations);
122
    }
123
}
124