-
Notifications
You must be signed in to change notification settings - Fork 14
/
Main.php
175 lines (154 loc) · 3.76 KB
/
Main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace Modules\MyBlog\Providers;
use App\Models\Auth\User;
use App\Models\Setting\Category;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider as Provider;
class Main extends Provider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->loadRoutes();
}
/**
* Boot the application events.
*
* @return void
*/
public function boot()
{
$this->loadViews();
$this->loadViewComponents();
$this->loadTranslations();
$this->loadMigrations();
$this->loadConfig();
$this->loadDynamicRelationships();
$this->loadCommands();
$this->scheduleCommands();
}
/**
* Load views.
*
* @return void
*/
public function loadViews()
{
$this->loadViewsFrom(__DIR__ . '/../Resources/views', 'my-blog');
}
/**
* Load view components.
*
* @return void
*/
public function loadViewComponents()
{
Blade::componentNamespace('Modules\MyBlog\View\Components', 'my-blog');
}
/**
* Load translations.
*
* @return void
*/
public function loadTranslations()
{
$this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'my-blog');
}
/**
* Load migrations.
*
* @return void
*/
public function loadMigrations()
{
$this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
}
/**
* Load config.
*
* @return void
*/
public function loadConfig()
{
$merge_to_core_configs = ['search-string', 'type'];
foreach ($merge_to_core_configs as $config) {
Config::set($config, array_merge_recursive(
Config::get($config),
require __DIR__ . '/../Config/' . $config . '.php'
));
}
$this->mergeConfigFrom(__DIR__ . '/../Config/my-blog.php', 'my-blog');
}
/**
* Load dynamic relationships.
*
* @return void
*/
public function loadDynamicRelationships()
{
User::resolveRelationUsing('my_blog_posts', function ($user) {
return $user->hasMany('Modules\MyBlog\Models\Post', 'created_by', 'id');
});
User::resolveRelationUsing('my_blog_comments', function ($user) {
return $user->hasMany('Modules\MyBlog\Models\Comment', 'created_by', 'id');
});
Category::resolveRelationUsing('my_blog_posts', function ($category) {
return $category->hasMany('Modules\MyBlog\Models\Post', 'category_id', 'id');
});
}
/**
* Load commands.
*
* @return void
*/
public function loadCommands()
{
$this->commands(\Modules\MyBlog\Console\Inspire::class);
}
/**
* Schedule commands.
*
* @return void
*/
public function scheduleCommands()
{
$this->app->booted(function () {
$schedule_time = config('app.schedule_time', '09:00');
app(Schedule::class)->command('my-blog:inspire')->dailyAt($schedule_time);
});
}
/**
* Load routes.
*
* @return void
*/
public function loadRoutes()
{
if (app()->routesAreCached()) {
return;
}
$routes = [
'admin.php',
'portal.php',
'api.php',
];
foreach ($routes as $route) {
$this->loadRoutesFrom(__DIR__ . '/../Routes/' . $route);
}
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [];
}
}