Completed
Push — master ( 0f92a6...d02b5c )
by Richan
01:33
created

ServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 4
dl 0
loc 65
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 5 1
A publishAssets() 0 14 1
A register() 0 8 1
A registerMacro() 0 12 3
1
<?php
2
3
namespace RichanFongdasen\I18n;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\ServiceProvider as Provider;
8
9
class ServiceProvider extends Provider
10
{
11
    /**
12
     * Bootstrap the application services.
13
     *
14
     * @return void
15
     */
16
    public function boot()
17
    {
18
        $this->publishAssets();
19
        $this->registerMacro();
20
    }
21
22
    /**
23
     * Publish package assets.
24
     *
25
     * @return void
26
     */
27
    protected function publishAssets()
28
    {
29
        $this->publishes([
30
            realpath(__DIR__.'/../config/i18n.php') => config_path('i18n.php'),
31
        ], 'config');
32
33
        $this->publishes([
34
            realpath(__DIR__.'/../database/migrations/') => database_path('migrations'),
35
        ], 'migrations');
36
37
        $this->publishes([
38
            realpath(__DIR__.'/../storage/i18n/') => storage_path('i18n'),
39
        ], 'languages.json');
40
    }
41
42
    /**
43
     * Register the application services.
44
     *
45
     * @return void
46
     */
47
    public function register()
48
    {
49
        $this->mergeConfigFrom(realpath(__DIR__.'/../config/i18n.php'), 'i18n');
50
51
        $this->app->singleton(I18nService::class, function () {
52
            return new I18nService(request());
53
        });
54
    }
55
56
    /**
57
     * Register macro for Collection class.
58
     *
59
     * @return void
60
     */
61
    protected function registerMacro()
62
    {
63
        Collection::macro('translate', function ($locale) {
64
            $this->each(function ($item, $key) use ($locale) {
65
                if (($item instanceof Model) && method_exists($item, 'translate')) {
66
                    $item->translate($locale);
67
                }
68
69
                return $key;
70
            });
71
        });
72
    }
73
}
74