Completed
Push — master ( b4e56c...1b6c56 )
by Avtandil
04:39
created

MultiLangServiceProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 78.18%

Importance

Changes 13
Bugs 7 Features 2
Metric Value
c 13
b 7
f 2
dl 0
loc 110
ccs 43
cts 55
cp 0.7818
rs 10
wmc 8
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
B boot() 0 29 3
B register() 0 45 4
A provides() 0 9 1
1
<?php
2
/*
3
 * This file is part of the Laravel MultiLang package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\LaravelMultiLang;
12
13
use Blade;
14
use Illuminate\Support\ServiceProvider;
15
use Longman\LaravelMultiLang\Console\MigrationCommand;
16
use Longman\LaravelMultiLang\Console\TextsCommand;
17
use Route;
18
use Illuminate\Routing\Events\RouteMatched;
19
20
class MultiLangServiceProvider extends ServiceProvider
21
{
22
    /**
23
     * Indicates if loading of the provider is deferred.
24
     *
25
     * @var bool
26
     */
27
    protected $defer = false;
28
29
    /**
30
     * Bootstrap any application services.
31
     *
32
     * @return void
33
     */
34 38
    public function boot()
35
    {
36
        // Publish config files
37 38
        $this->publishes([
38 38
                             __DIR__ . '/../config/config.php' => config_path('multilang.php'),
39 38
                             __DIR__ . '/../views'             => base_path('resources/views/vendor/multilang'),
40 38
                         ]);
41
42
        // Append the country settings
43 38
        $this->mergeConfigFrom(
44 38
            __DIR__ . '/../config/config.php',
45
            'multilang'
46 38
        );
47
48
        // Register blade directives
49
        Blade::directive('t', function ($expression) {
50
            return "<?php echo e(t({$expression})); ?>";
51 38
        });
52
53
        $this->app['events']->listen(RouteMatched::class, function () {
54
            $scope = $this->app['config']->get('app.scope');
55
            if ($scope && $scope != 'global') {
56
                $this->app['multilang']->setScope($scope);
57
            }
58
            $this->app['multilang']->setLocale($this->app->getLocale());
59 38
        });
60
61 38
        $this->loadViewsFrom(__DIR__ . '/../views', 'multilang');
62 38
    }
63
64
    /**
65
     * Register any application services.
66
     *
67
     * @return void
68
     */
69 38
    public function register()
70
    {
71 38
        $configPath = __DIR__ . '/../config/config.php';
72 38
        $this->mergeConfigFrom($configPath, 'debugbar');
73
74
        $this->app->singleton('multilang', function ($app) {
75 3
            $environment = $app->environment();
76 3
            $config      = $app['config']->get('multilang');
77
78 3
            $multilang = new \Longman\LaravelMultiLang\MultiLang(
79 3
                $environment,
80 3
                $config,
81 3
                $app['cache'],
82 3
                $app['db']
83 38
            );
84
85 3
            if ($multilang->autoSaveIsAllowed()) {
86
                $app->terminating(function () use ($multilang) {
87
                    $scope = $this->app['config']->get('app.scope');
88 1
                    if ($scope && $scope != 'global') {
89
                        $multilang->setScope($scope);
90
                    }
91
                    return $multilang->saveTexts();
92
                });
93
            }
94
95 3
            return $multilang;
96 38
        });
97
98 38
        $this->app->alias('multilang', 'Longman\LaravelMultiLang\MultiLang');
99
100 38
        $this->app['command.multilang.migration'] = $this->app->share(
101
            function () {
102 4
                return new MigrationCommand();
103
            }
104 38
        );
105
106 38
        $this->app['command.multilang.texts'] = $this->app->share(
107 4
            function () {
108 4
                return new TextsCommand();
109
            }
110 38
        );
111
112 38
        $this->commands(['command.multilang.migration', 'command.multilang.texts']);
113 38
    }
114
115
    /**
116
     * Get the services provided by the provider.
117
     *
118
     * @return array
119
     */
120 3
    public function provides()
121
    {
122
        return [
123 3
            'multilang',
124 3
            'command.multilang.migration',
125 3
            'command.multilang.texts',
126 3
            'Longman\LaravelMultiLang\MultiLang',
127 3
        ];
128
    }
129
}
130