Passed
Push — master ( bd9b88...11a7f9 )
by Fu
08:59 queued 03:53
created

CoreServiceProvider::publish()   A

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