Test Setup Failed
Push — master ( 35acdf...505488 )
by Avtandil
02:16
created

MultiLangServiceProvider::boot()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.1172

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 1
nop 0
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
ccs 13
cts 17
cp 0.7647
crap 3.1172
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\Foundation\Events\LocaleUpdated;
15
use Illuminate\Http\Request;
16
use Illuminate\Routing\Events\RouteMatched;
17
use Illuminate\Support\ServiceProvider;
18
use Longman\LaravelMultiLang\Console\ExportCommand;
19
use Longman\LaravelMultiLang\Console\ImportCommand;
20
use Longman\LaravelMultiLang\Console\MigrationCommand;
21
use Longman\LaravelMultiLang\Console\TextsCommand;
22
23
class MultiLangServiceProvider extends ServiceProvider
24
{
25
    /**
26
     * Indicates if loading of the provider is deferred.
27
     *
28
     * @var bool
29
     */
30
    protected $defer = false;
31
32
    /**
33
     * Bootstrap any application services.
34
     *
35
     * @return void
36
     */
37 42
    public function boot()
38
    {
39
        // Publish config files
40 42
        $this->publishes(
41
            [
42 42
                __DIR__ . '/../config/config.php' => config_path('multilang.php'),
43 42
                __DIR__ . '/../views'             => base_path('resources/views/vendor/multilang'),
44
            ]
45
        );
46
47
        // Append the country settings
48 42
        $this->mergeConfigFrom(
49 42
            __DIR__ . '/../config/config.php',
50 42
            'multilang'
51
        );
52
53
        // Register blade directives
54
        Blade::directive('t', function ($expression) {
55
            return "<?php echo e(t({$expression})); ?>";
56 42
        });
57
58
        $this->app['events']->listen(RouteMatched::class, function () {
59
            $scope = $this->app['config']->get('app.scope');
60
            if ($scope && $scope != 'global') {
61
                $this->app['multilang']->setScope($scope);
62
            }
63 42
        });
64
65
        $this->app['events']->listen(LocaleUpdated::class, function ($event) {
66 1
            $this->app['multilang']->setLocale($event->locale);
67 42
        });
68
69 42
        $this->loadViewsFrom(__DIR__ . '/../views', 'multilang');
70 42
    }
71
72
    /**
73
     * Register any application services.
74
     *
75
     * @return void
76
     */
77 42
    public function register()
78
    {
79 42
        $configPath = __DIR__ . '/../config/config.php';
80 42
        $this->mergeConfigFrom($configPath, 'debugbar');
81
82
        $this->app->singleton('multilang', function ($app) {
83 4
            $environment = $app->environment();
84 4
            $config = $app['config']->get('multilang');
85
86 4
            $multilang = new \Longman\LaravelMultiLang\MultiLang(
87 4
                $environment,
88 4
                $config,
89 4
                $app['cache'],
90 4
                $app['db']
91
            );
92
93 4
            if ($multilang->autoSaveIsAllowed()) {
94
                $app->terminating(function () use ($multilang) {
95
                    $scope = $this->app['config']->get('app.scope');
96
                    if ($scope && $scope != 'global') {
97
                        $multilang->setScope($scope);
98
                    }
99
100
                    return $multilang->saveTexts();
101
                });
102
            }
103
104 4
            return $multilang;
105 42
        });
106
107 42
        $this->app->alias('multilang', 'Longman\LaravelMultiLang\MultiLang');
108
109 42
        $this->app->singleton(
110 42
            'command.multilang.migration',
111
            function () {
112 5
                return new MigrationCommand();
113 42
            }
114
        );
115
116 42
        $this->app->singleton(
117 42
            'command.multilang.texts',
118
            function () {
119 5
                return new TextsCommand();
120 42
            }
121
        );
122
123 42
        $this->app->singleton(
124 42
            'command.multilang.import',
125
            function () {
126 5
                return new ImportCommand();
127 42
            }
128
        );
129
130 42
        $this->app->singleton(
131 42
            'command.multilang.export',
132
            function () {
133 5
                return new ExportCommand();
134 42
            }
135
        );
136
137 42
        $this->commands(
138
            [
139 42
                'command.multilang.migration',
140
                'command.multilang.texts',
141
                'command.multilang.import',
142
                'command.multilang.export',
143
            ]
144
        );
145
146 42
        Request::macro('locale', function () {
147
            return app('multilang')->getLocale();
148 42
        });
149 42
    }
150
151
    /**
152
     * Get the services provided by the provider.
153
     *
154
     * @return array
155
     */
156 3
    public function provides()
157
    {
158
        return [
159 3
            'multilang',
160
            'command.multilang.migration',
161
            'command.multilang.texts',
162
            'command.multilang.import',
163
            'command.multilang.export',
164
            'Longman\LaravelMultiLang\MultiLang',
165
        ];
166
    }
167
}
168