Completed
Push — master ( 7d660e...c11331 )
by Avtandil
02:52
created

MultiLangServiceProvider::provides()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 7
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 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
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 38
    public function boot()
34
    {
35
36 38
        $this->setRoutes();
37
38
        // Publish config files
39 38
        $this->publishes([
40 38
            __DIR__ . '/../config/config.php' => config_path('multilang.php'),
41 38
            __DIR__ . '/../views' => base_path('resources/views/vendor/multilang'),
42
        ]);
43
44
        // Append the country settings
45 38
        $this->mergeConfigFrom(
46 38
            __DIR__ . '/../config/config.php',
47 38
            'multilang'
48
        );
49
50
        // Register blade directives
51
        Blade::directive('t', function ($expression) {
52
            return "<?php echo e(t({$expression})); ?>";
53 38
        });
54
55
        $this->app['events']->listen('locale.changed', function ($locale) {
56 1
            $this->app['multilang']->setLocale($locale);
57 38
        });
58
59 38
        $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 38
    }
68
69 38
    public function setRoutes()
70
    {
71 38
        $locales = $this->app['config']->get('multilang.locales', []);
72 38
        $route = $this->app['config']->get('multilang.text-route.route', 'texts');
73 38
        $controller = $this->app['config']->get('multilang.text-route.controller', '\Longman\LaravelMultiLang\Controllers\TextsController');
74 38
        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 38
    }
87
88
    /**
89
     * Register any application services.
90
     *
91
     * @return void
92
     */
93 38
    public function register()
94
    {
95 38
        $configPath = __DIR__ . '/../config/config.php';
96 38
        $this->mergeConfigFrom($configPath, 'debugbar');
97
98
        $this->app->singleton('multilang', function ($app) {
99 4
            $environment = $app->environment();
100 4
            $config = $app['config']->get('multilang');
101
102 4
            $multilang = new \Longman\LaravelMultiLang\MultiLang(
103
                $environment,
104
                $config,
105 4
                $app['cache'],
106 4
                $app['db']
107
            );
108
109 4
            if ($multilang->autoSaveIsAllowed()) {
110
                $app->terminating(function () use ($multilang) {
111
                    return $multilang->saveTexts();
112
                });
113
            }
114
115 4
            return $multilang;
116 38
        });
117
118 38
        $this->app->alias('multilang', 'Longman\LaravelMultiLang\MultiLang');
119
120 38
        $this->app['command.multilang.migration'] = $this->app->share(
121
            function () {
122 4
                return new MigrationCommand();
123 38
            }
124
        );
125
126 38
        $this->app['command.multilang.texts'] = $this->app->share(
127 38
            function () {
128 4
                return new TextsCommand();
129 38
            }
130
        );
131
132 38
        $this->commands(['command.multilang.migration', 'command.multilang.texts']);
133 38
    }
134
135
    /**
136
     * Get the services provided by the provider.
137
     *
138
     * @return array
139
     */
140 3
    public function provides()
141
    {
142
        return [
143 3
            'multilang', 'command.multilang.migration',
144
            'command.multilang.texts', 'Longman\LaravelMultiLang\MultiLang',
145
        ];
146
    }
147
}
148