TranslationServiceProvider::boot()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
3
namespace MikeZange\LaravelDatabaseTranslation;
4
5
use Illuminate\Cache\Repository as CacheRepository;
6
use Illuminate\Support\ServiceProvider as ServiceProvider;
7
use Illuminate\Translation\Translator;
8
use MikeZange\LaravelDatabaseTranslation\Commands\LoadTranslationsFromFiles;
9
use MikeZange\LaravelDatabaseTranslation\Commands\RemoveTranslationsForLocale;
10
use MikeZange\LaravelDatabaseTranslation\Loaders\DatabaseLoader;
11
use MikeZange\LaravelDatabaseTranslation\Repositories\TranslationRepository;
12
13
/**
14
 * Class TranslationServiceProvider.
15
 */
16
class TranslationServiceProvider extends ServiceProvider
17
{
18
    /**
19
     * Bootstrap the application services.
20
     *
21
     * @return void
22
     */
23
    public function boot()
24
    {
25
        $this->publishes([
26
            __DIR__.'/../config/database.translations.php' => config_path('database.translations.php'),
27
        ]);
28
29
        $this->loadMigrationsFrom(__DIR__.'/Migrations');
30
31
        if ($this->app->runningInConsole()) {
32
            $this->commands([
33
                LoadTranslationsFromFiles::class,
34
                RemoveTranslationsForLocale::class,
35
            ]);
36
        }
37
    }
38
39
    /**
40
     * Register the application services.
41
     *
42
     * @return void
43
     */
44
    public function register()
45
    {
46
        $this->mergeConfigFrom(__DIR__.'/../config/database.translations.php', 'database.translations');
47
48
        $this->registerLoader();
49
50
        $this->app->singleton('translator', function ($app) {
51
            $loader = $app['translation.loader'];
52
53
            // When registering the translator component, we'll need to set the default
54
            // locale as well as the fallback locale. So, we'll grab the application
55
            // configuration so we can easily get both of these values from there.
56
            $locale = $app['config']['app.locale'];
57
58
            $trans = new Translator($loader, $locale);
59
60
            $trans->setFallback($app['config']['app.fallback_locale']);
61
62
            return $trans;
63
        });
64
    }
65
66
    /**
67
     * Register the translation loader.
68
     *
69
     * @return void
70
     */
71
    protected function registerLoader()
72
    {
73
        $this->app->singleton('translation.loader', function ($app) {
74
            return new DatabaseLoader(
75
                $app['files'],
76
                $app['path.lang'],
77
                $this->app->make(TranslationRepository::class),
78
                $this->app->make(CacheRepository::class)
79
            );
80
        });
81
    }
82
}
83