CoreServiceProvider::publish()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Modules\Core\Providers;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Factory;
7
use Illuminate\Support\Facades\Config;
8
use Illuminate\Support\Facades\Event;
9
use Illuminate\Support\ServiceProvider;
10
use Illuminate\Support\Str;
11
use Prettus\Repository\Events\RepositoryEventBase;
12
13
class CoreServiceProvider extends ServiceProvider
14
{
15
    /**
16
     * Indicates if loading of the provider is deferred.
17
     *
18
     * @var bool
19
     */
20
    protected $defer = false;
21
22
    /**
23
     * Boot the application events.
24
     *
25
     * @return void
26
     */
27
    public function boot()
28
    {
29
        $this->setLocale();
30
        $this->registerTranslations();
31
        $this->registerConfig();
32
        $this->registerViews();
33
        $this->registerFactories();
34
        $this->registerObservers();
35
        $this->registerTelescope();
36
        $this->loadMigrationsFrom(__DIR__.'/../Database/Migrations');
37
        $this->publish();
38
    }
39
40
    /**
41
     * Register the service provider.
42
     *
43
     * @return void
44
     */
45
    public function register()
46
    {
47
        $this->app->register(RouteServiceProvider::class);
48
        $this->app->register(ControllerServiceProvider::class);
49
        $this->app->register(ServiceServiceProvider::class);
50
        $this->app->register(RepositoryServiceProvider::class);
51
    }
52
53
    /**
54
     * Register config.
55
     *
56
     * @return void
57
     */
58
    protected function registerConfig()
59
    {
60
        $this->publishes([
61
            __DIR__.'/../Config/config.php' => config_path('core.php'),
62
        ], 'config');
63
        $this->mergeConfigFrom(
64
            __DIR__.'/../Config/config.php', 'core'
65
        );
66
    }
67
68
    /**
69
     * Register views.
70
     *
71
     * @return void
72
     */
73
    public function registerViews()
74
    {
75
        $viewPath = resource_path('views/modules/core');
76
77
        $sourcePath = __DIR__.'/../Resources/views';
78
79
        $this->publishes([
80
            $sourcePath => $viewPath,
81
        ], 'views');
82
83
        $this->loadViewsFrom(array_merge(array_map(function ($path) {
84
            return $path.'/modules/core';
85
        }, Config::get('view.paths')), [$sourcePath]), 'core');
86
    }
87
88
    /**
89
     * Register translations.
90
     *
91
     * @return void
92
     */
93
    public function registerTranslations()
94
    {
95
        $langPath = resource_path('lang/modules/core');
96
97
        if (is_dir($langPath)) {
98
            $this->loadTranslationsFrom($langPath, 'core');
99
        } else {
100
            $this->loadTranslationsFrom(__DIR__.'/../Resources/lang', 'core');
101
        }
102
    }
103
104
    /**
105
     * Register an additional directory of factories.
106
     *
107
     * @return void
108
     */
109
    public function registerFactories()
110
    {
111
        if (!$this->app->environment('production')) {
112
            app(Factory::class)->load(__DIR__.'/../Database/factories');
113
        }
114
    }
115
116
    /**
117
     * Get the services provided by the provider.
118
     *
119
     * @return array
120
     */
121
    public function provides()
122
    {
123
        return [];
124
    }
125
126
    public function setLocale()
127
    {
128
        Carbon::setLocale('zh');
129
    }
130
131
    public function registerObservers()
132
    {
133
        Event::/* @scrutinizer ignore-call */ listen(RepositoryEventBase::class, function (
134
            RepositoryEventBase $repositoryEntityCreated
135
        ) {
136
            $model = $repositoryEntityCreated->getModel();
137
            $method = $repositoryEntityCreated->getAction();
138
            $class = get_class($model);
139
            $namespace = Str::before($class, 'Entities');
140
            $basename = class_basename($model);
141
            $observerClass = "{$namespace}Observers\\{$basename}Observer";
142
            if (class_exists($observerClass)) {
143
                $observer = app("{$namespace}Observers\\{$basename}Observer");
144
                $observer->$method($model);
145
            }
146
        });
147
    }
148
149
    public function registerTelescope()
150
    {
151
        if ($this->app->isLocal() && class_exists('Laravel\\Telescope\\TelescopeApplicationServiceProvider')) {
152
            $this->app->register(TelescopeServiceProvider::class);
153
        }
154
    }
155
156
    public function publish()
157
    {
158
        $this->publishes([
159
            dirname(__DIR__).'/config.php' => config_path('modules.php'),
160
        ], 'modular-config');
161
    }
162
}
163