Completed
Push — master ( 5f83dd...427d1b )
by Avtandil
03:09
created

MultiLangServiceProvider::provides()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
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
19
class MultiLangServiceProvider extends ServiceProvider
20
{
21
    /**
22
     * Indicates if loading of the provider is deferred.
23
     *
24
     * @var bool
25
     */
26
    protected $defer = false;
27
28
    /**
29
     * Bootstrap any application services.
30
     *
31
     * @return void
32
     */
33 33
    public function boot()
34
    {
35
36 33
        $this->setRoutes();
37
38
        // Publish config files
39
        $this->publishes([
40
            __DIR__ . '/../config/config.php' => config_path('multilang.php'),
41
            __DIR__ . '/../views' => base_path('resources/views/vendor/multilang'),
42
        ]);
43
44
        // Append the country settings
45
        $this->mergeConfigFrom(
46
            __DIR__ . '/../config/config.php',
47
            'multilang'
48
        );
49
50
        // Register blade directives
51
        Blade::directive('t', function ($expression) {
52
            return "<?php echo e(t({$expression})); ?>";
53
        });
54
55
        $this->app['events']->listen('locale.changed', function ($locale) {
56
            $this->app['multilang']->setLocale($locale);
57
        });
58
59
        $this->loadViewsFrom(__DIR__ . '/../views', 'multilang');
60
61
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62
        $this->app['events']->listen(RouteMatched::class, function () {
63
        dump($this->app['router']);
64
        die();
65
        });
66
        */
67
    }
68
69 33
    public function setRoutes()
70
    {
71 33
        $locales = $this->app['config']->get('multilang.locales');
72 33
        $route = $this->app['config']->get('multilang.text-route.route', 'texts');
73 33
        $controller = $this->app['config']->get('multilang.text-route.controller', '\Longman\LaravelMultiLang\Controllers\TextsController');
74 33
        foreach($locales as $locale => $value) {
75
            Route::group(['prefix' => $locale], function ($router) use ($route, $controller) {
76
                $router->get(
77
                    $route,
78
                    $controller . '@index'
79
                );
80
                $router->post(
81
                    $route,
82
                    $controller . '@save'
83
                );
84
            });
85
86
        }
87
    }
88
89
    /**
90
     * Register any application services.
91
     *
92
     * @return void
93
     */
94 33
    public function register()
95
    {
96 33
        $configPath = __DIR__ . '/../config/config.php';
97 33
        $this->mergeConfigFrom($configPath, 'debugbar');
98
99
        $this->app->singleton('multilang', function ($app) {
100
            $environment = $app->environment();
101
            $config = $app['config']->get('multilang');
102
103
            $multilang = new \Longman\LaravelMultiLang\MultiLang(
104
                $environment,
105
                $config,
106
                $app['cache'],
107
                $app['db']
108
            );
109
110
            if ($multilang->autoSaveIsAllowed()) {
111
                $app->terminating(function () use ($multilang) {
112
                    return $multilang->saveTexts();
113
                });
114
            }
115
116
            return $multilang;
117 33
        });
118
119 33
        $this->app->alias('multilang', 'Longman\LaravelMultiLang\MultiLang');
120
121 33
        $this->app['command.multilang.migration'] = $this->app->share(
122
            function () {
123
                return new MigrationCommand();
124 33
            }
125
        );
126
127 33
        $this->app['command.multilang.texts'] = $this->app->share(
128 33
            function () {
129
                return new TextsCommand();
130 33
            }
131
        );
132
133 33
        $this->commands(['command.multilang.migration', 'command.multilang.texts']);
134 33
    }
135
136
    /**
137
     * Get the services provided by the provider.
138
     *
139
     * @return array
140
     */
141
    public function provides()
142
    {
143
        return [
144
            'multilang', 'command.multilang.migration',
145
            'command.multilang.texts', 'Longman\LaravelMultiLang\MultiLang',
146
        ];
147
    }
148
}
149