|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SimpleCMS\Company; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Illuminate\Support\Facades\File; |
|
8
|
|
|
use Illuminate\Support\Facades\Event; |
|
9
|
|
|
use SimpleCMS\Company\Models\Company; |
|
10
|
|
|
use SimpleCMS\Company\Events\ApiLogin; |
|
11
|
|
|
use Illuminate\Support\ServiceProvider; |
|
12
|
|
|
use SimpleCMS\Company\Events\LoginFailed; |
|
13
|
|
|
use SimpleCMS\Company\Events\LoginSuccess; |
|
14
|
|
|
use SimpleCMS\Company\Events\CompanyRequest; |
|
15
|
|
|
use SimpleCMS\Company\Models\CompanyAccount; |
|
16
|
|
|
use SimpleCMS\Company\Abstracts\CompanyAbstract; |
|
17
|
|
|
use SimpleCMS\Company\Listeners\ApiLoginListener; |
|
18
|
|
|
use SimpleCMS\Company\Listeners\LoginFailedListener; |
|
19
|
|
|
use SimpleCMS\Company\Listeners\LoginSuccessListener; |
|
20
|
|
|
use SimpleCMS\Company\Listeners\CompanyRequestListener; |
|
21
|
|
|
use SimpleCMS\Company\Http\Middleware\CompanyLogMiddleware; |
|
22
|
|
|
|
|
23
|
|
|
class CompanyServiceProvider extends ServiceProvider |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Register any application services. |
|
27
|
|
|
* |
|
28
|
|
|
* @return void |
|
29
|
|
|
*/ |
|
30
|
|
|
public function register() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->registerEvents(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Bootstrap services. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function boot(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->bootConfig(); |
|
41
|
|
|
$this->loadFacades(); |
|
42
|
|
|
$this->bindListeners(); |
|
43
|
|
|
$this->bindCompanyRelationships(); |
|
44
|
|
|
$this->bindObservers(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* 加载模型事件 |
|
49
|
|
|
* |
|
50
|
|
|
* @author Dennis Lui <[email protected]> |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function bindObservers(): void |
|
54
|
|
|
{ |
|
55
|
|
|
Company::observe(\SimpleCMS\Company\Observers\CompanyObserver::class); |
|
56
|
|
|
CompanyAccount::observe(\SimpleCMS\Company\Observers\CompanyAccountObserver::class); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* 动态绑定 Company 模型的关系 |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function bindCompanyRelationships(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$modelsPath = app_path('Models'); |
|
65
|
|
|
$namespace = 'App\\Models\\'; |
|
66
|
|
|
|
|
67
|
|
|
// 获取所有模型文件 |
|
68
|
|
|
$modelFiles = File::allFiles($modelsPath); |
|
69
|
|
|
|
|
70
|
|
|
foreach ($modelFiles as $file) { |
|
71
|
|
|
$modelClass = $namespace . $file->getFilenameWithoutExtension(); |
|
72
|
|
|
|
|
73
|
|
|
// 检查模型是否继承了 CompanyAbstract |
|
74
|
|
|
if (is_subclass_of($modelClass, CompanyAbstract::class)) { |
|
75
|
|
|
$this->relationUsing($modelClass); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function relationUsing(mixed $modelClass):void |
|
81
|
|
|
{ |
|
82
|
|
|
if(method_exists($modelClass,'companyRelations')) |
|
83
|
|
|
{ |
|
84
|
|
|
foreach($modelClass::companyRelations() as $relationName => $key) |
|
85
|
|
|
{ |
|
86
|
|
|
Company::resolveRelationUsing($relationName, function (Company $company) use ($modelClass,$key) { |
|
87
|
|
|
return $company->hasMany($modelClass,$key); |
|
88
|
|
|
}); |
|
89
|
|
|
} |
|
90
|
|
|
}else{ |
|
91
|
|
|
$relationName = Str::plural(Str::camel(class_basename($modelClass))); |
|
92
|
|
|
|
|
93
|
|
|
Company::resolveRelationUsing($relationName, function (Company $company) use ($modelClass) { |
|
94
|
|
|
return $company->hasMany($modelClass); |
|
95
|
|
|
}); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* 注册事件 |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function registerEvents(): void |
|
103
|
|
|
{ |
|
104
|
|
|
$this->app->bind('simplecms.plugin.company.api_login', function (CompanyAccount $account) { |
|
105
|
|
|
return new ApiLogin($account); |
|
106
|
|
|
}); |
|
107
|
|
|
$this->app->bind('simplecms.plugin.company.login_failed', function (CompanyAccount $account) { |
|
108
|
|
|
return new LoginFailed($account); |
|
109
|
|
|
}); |
|
110
|
|
|
$this->app->bind('simplecms.plugin.company.login_success', function (CompanyAccount $account) { |
|
111
|
|
|
return new LoginSuccess($account); |
|
112
|
|
|
}); |
|
113
|
|
|
$this->app->bind('simplecms.plugin.company.company_request', function (Request $request, bool $status) { |
|
114
|
|
|
return new CompanyRequest($request, $status); |
|
115
|
|
|
}); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* 绑定监听器 |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function bindListeners(): void |
|
122
|
|
|
{ |
|
123
|
|
|
Event::listen(ApiLogin::class, [ApiLoginListener::class, 'handle']); |
|
124
|
|
|
Event::listen(LoginFailed::class, [LoginFailedListener::class, 'handle']); |
|
125
|
|
|
Event::listen(LoginSuccess::class, [LoginSuccessListener::class, 'handle']); |
|
126
|
|
|
Event::listen(CompanyRequest::class, [CompanyRequestListener::class, 'handle']); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* 绑定 Facades |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function loadFacades(): void |
|
133
|
|
|
{ |
|
134
|
|
|
$this->app->bind('company_authenticatable', fn() => new \SimpleCMS\Company\Packages\CompanyAuthenticatable); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* 初始化配置文件 |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function bootConfig(): void |
|
141
|
|
|
{ |
|
142
|
|
|
$this->publishes([ |
|
143
|
|
|
__DIR__ . '/../database/migrations' => database_path('migrations'), |
|
144
|
|
|
__DIR__ . '/../database/seeders' => database_path('seeders'), |
|
145
|
|
|
], 'simplecms'); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* 注册Middleware |
|
151
|
|
|
* |
|
152
|
|
|
* @author Dennis Lui <[email protected]> |
|
153
|
|
|
* @return void |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function registerMiddleware(): void |
|
156
|
|
|
{ |
|
157
|
|
|
$this->app->singleton(CompanyLogMiddleware::class); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|