-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceProvider.php
237 lines (172 loc) · 11.3 KB
/
ServiceProvider.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
declare(strict_types=1);
/**
* Playground
*/
namespace Playground;
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider;
/**
* \Playground\ServiceProvider
*/
class ServiceProvider extends AuthServiceProvider
{
protected string $package = 'playground';
public const VERSION = '73.0.0';
public function boot(): void
{
/**
* @var array<string, mixed> $config
*/
$config = config($this->package);
if (! empty($config['load']) && is_array($config['load'])) {
if ($this->app->runningInConsole()) {
// Publish configuration
$this->publishes([
sprintf('%1$s/config/%2$s.php', dirname(__DIR__), $this->package) => config_path(sprintf('%1$s.php', $this->package)),
], 'playground-config');
// Publish migrations
$this->publishMigrations();
// Load migrations
if (! empty($config['load']['migrations'])) {
$this->loadMigrationsFrom(dirname(__DIR__).'/database/migrations');
}
}
if (! empty($config['about'])) {
$this->about($config);
}
}
}
public function publishMigrations(): void
{
$migrations = [];
foreach ([
'0001_01_01_000000_create_users_table.php',
'0001_01_01_000001_create_cache_table.php',
'0001_01_01_000002_create_jobs_table.php',
'2024_03_13_210031_create_personal_access_tokens_table.php',
] as $file) {
$migrations[dirname(__DIR__).'/database/migrations/'.$file] = database_path('migrations/'.$file);
}
$this->publishes($migrations, 'playground-migrations');
}
/**
* @param array<string, mixed> $config
*/
public function about(array $config): void
{
$packages = ! empty($config['packages']) && is_array($config['packages']) ? $config['packages'] : [];
/**
* @var class-string $auth_providers_users_model
*/
$auth_providers_users_model = config('auth.providers.users.model');
AboutCommand::add('Playground', fn () => [
'<fg=cyan;options=bold>User</> [auth.providers.users.model]' => sprintf('[%s]', is_string($auth_providers_users_model) ? $auth_providers_users_model : ''),
'<fg=cyan;options=bold>User Primary</>' => $this->userPrimaryKeyType($auth_providers_users_model),
'<fg=cyan;options=bold>User</> Illuminate\Database\Eloquent\Factories\HasFactory' => $this->userHasFactory ? '<fg=green;options=bold>USED</>' : '<fg=yellow;options=bold>NOT USED</>',
'<fg=cyan;options=bold>User</> Illuminate\Database\Eloquent\Model' => $this->userIsEloquentModel ? '<fg=green;options=bold>EXTENDED</>' : '<fg=yellow;options=bold>NOT EXTENDED</>',
'<fg=cyan;options=bold>User</> Illuminate\Database\Eloquent\Concerns\HasUuids' => $this->userHasEloquentUuids ? '<fg=green;options=bold>USED</>' : '<fg=yellow;options=bold>NOT USED</>',
'<fg=cyan;options=bold>User</> Illuminate\Notifications\Notifiable' => $this->userHasNotifiable ? '<fg=green;options=bold>USED</>' : '<fg=yellow;options=bold>NOT USED</>',
'<fg=cyan;options=bold>User</> Illuminate\Contracts\Auth\MustVerifyEmail' => $this->userHasMustVerifyEmail ? '<fg=green;options=bold>IMPLEMENTED</>' : '<fg=yellow;options=bold>NOT IMPLEMENTED</>',
'<fg=cyan;options=bold>User</> Illuminate\Auth\Passwords\CanResetPassword' => $this->userCanResetPasswordConcern ? '<fg=green;options=bold>USED</>' : '<fg=yellow;options=bold>NOT USED</>',
'<fg=cyan;options=bold>User</> Illuminate\Contracts\Auth\CanResetPassword' => $this->userCanResetPasswordContract ? '<fg=green;options=bold>IMPLEMENTED</>' : '<fg=yellow;options=bold>NOT IMPLEMENTED</>',
'<fg=cyan;options=bold>User</> Illuminate\Foundation\Auth\Access\Authorizable' => $this->userHasAuthorizableConcerns ? '<fg=green;options=bold>USED</>' : '<fg=yellow;options=bold>NOT USED</>',
'<fg=cyan;options=bold>User</> Illuminate\Contracts\Auth\Access\Authorizable' => $this->userHasAuthorizableContracts ? '<fg=green;options=bold>IMPLEMENTED</>' : '<fg=yellow;options=bold>NOT IMPLEMENTED</>',
'<fg=cyan;options=bold>User</> Illuminate\Auth\Authenticatable' => $this->userHasAuthenticatableConcerns ? '<fg=green;options=bold>USED</>' : '<fg=yellow;options=bold>NOT USED</>',
'<fg=cyan;options=bold>User</> Illuminate\Contracts\Auth\Authenticatable' => $this->userHasAuthenticatableContracts ? '<fg=green;options=bold>IMPLEMENTED</>' : '<fg=yellow;options=bold>NOT IMPLEMENTED</>',
'<fg=cyan;options=bold>User</> Playground\Models\Concerns\Abilities' => $this->userPlaygroundAbilitiesConcerns ? '<fg=green;options=bold>USED</>' : '<fg=yellow;options=bold>NOT USED</>',
'<fg=cyan;options=bold>User</> Playground\Models\Contracts\Abilities' => $this->userHasPlaygroundAbilitiesContracts ? '<fg=green;options=bold>IMPLEMENTED</>' : '<fg=yellow;options=bold>NOT IMPLEMENTED</>',
'<fg=cyan;options=bold>User</> Playground\Models\Concerns\Admin' => $this->userPlaygroundAdminConcerns ? '<fg=green;options=bold>USED</>' : '<fg=yellow;options=bold>NOT USED</>',
'<fg=cyan;options=bold>User</> Playground\Models\Contracts\Admin' => $this->userHasPlaygroundAdminContracts ? '<fg=green;options=bold>IMPLEMENTED</>' : '<fg=yellow;options=bold>NOT IMPLEMENTED</>',
'<fg=cyan;options=bold>User</> Playground\Models\Concerns\Privileges' => $this->userPlaygroundPrivilegesConcerns ? '<fg=green;options=bold>USED</>' : '<fg=yellow;options=bold>NOT USED</>',
'<fg=cyan;options=bold>User</> Playground\Models\Contracts\Privileges' => $this->userHasPlaygroundPrivilegesContracts ? '<fg=green;options=bold>IMPLEMENTED</>' : '<fg=yellow;options=bold>NOT IMPLEMENTED</>',
'<fg=cyan;options=bold>User</> Playground\Models\Concerns\Role' => $this->userPlaygroundRoleConcerns ? '<fg=green;options=bold>USED</>' : '<fg=yellow;options=bold>NOT USED</>',
'<fg=cyan;options=bold>User</> Playground\Models\Contracts\Role' => $this->userHasPlaygroundRoleContracts ? '<fg=green;options=bold>IMPLEMENTED</>' : '<fg=yellow;options=bold>NOT IMPLEMENTED</>',
'<fg=cyan;options=bold>User</> Playground\Models\Contracts\WithMatrix' => $this->userHasPlaygroundMatrixContracts ? '<fg=green;options=bold>IMPLEMENTED</>' : '<fg=yellow;options=bold>NOT IMPLEMENTED</>',
'Packages' => implode(', ', $packages),
'Package' => $this->package,
'Version' => ServiceProvider::VERSION,
]);
}
/**
* @param ?class-string $auth_providers_users_model
*/
public function userPrimaryKeyType(string $auth_providers_users_model = null): string
{
try {
if (! $auth_providers_users_model || ! class_exists($auth_providers_users_model)) {
return '<fg=yellow;options=bold>invalid</>';
}
return $this->userPrimaryKeyTypeParse($auth_providers_users_model);
} catch (\Throwable $th) {
\Log::debug($th->__toString());
return '<fg=red;options=bold>error</>';
}
}
protected bool $userHasFactory;
protected bool $userCanResetPasswordConcern;
protected bool $userCanResetPasswordContract;
protected bool $userHasAuthorizableConcerns;
protected bool $userHasAuthorizableContracts;
protected bool $userIsEloquentModel;
protected bool $userHasEloquentUuids;
protected bool $userHasNotifiable;
protected bool $userHasAuthenticatableConcerns;
protected bool $userHasAuthenticatableContracts;
protected bool $userHasMustVerifyEmail;
protected bool $userPlaygroundAbilitiesConcerns;
protected bool $userHasPlaygroundAbilitiesContracts;
protected bool $userPlaygroundAdminConcerns;
protected bool $userHasPlaygroundAdminContracts;
protected bool $userPlaygroundPrivilegesConcerns;
protected bool $userHasPlaygroundPrivilegesContracts;
protected bool $userPlaygroundRoleConcerns;
protected bool $userHasPlaygroundRoleContracts;
protected bool $userHasPlaygroundMatrixContracts;
/**
* @param class-string $auth_providers_users_model
*/
private function userPrimaryKeyTypeParse(string $auth_providers_users_model): string
{
$model_info = '';
/**
* @var \Illuminate\Contracts\Auth\Authenticatable
*/
$user = new $auth_providers_users_model;
$this->userHasFactory = in_array(\Illuminate\Database\Eloquent\Factories\HasFactory::class, class_uses_recursive($user));
$this->userCanResetPasswordConcern = in_array(\Illuminate\Auth\Passwords\CanResetPassword::class, class_uses_recursive($user));
$this->userCanResetPasswordContract = $user instanceof \Illuminate\Contracts\Auth\CanResetPassword;
$this->userHasAuthorizableConcerns = in_array(\Illuminate\Foundation\Auth\Access\Authorizable::class, class_uses_recursive($user));
$this->userHasAuthorizableContracts = $user instanceof \Illuminate\Contracts\Auth\Access\Authorizable;
$this->userIsEloquentModel = $user instanceof \Illuminate\Database\Eloquent\Model;
$this->userHasEloquentUuids = in_array(\Illuminate\Database\Eloquent\Concerns\HasUuids::class, class_uses_recursive($user));
$this->userHasNotifiable = in_array(\Illuminate\Notifications\Notifiable::class, class_uses_recursive($user));
$this->userHasAuthenticatableConcerns = in_array(\Illuminate\Auth\Authenticatable::class, class_uses_recursive($user));
$this->userHasAuthenticatableContracts = $user instanceof \Illuminate\Contracts\Auth\Authenticatable;
$this->userHasMustVerifyEmail = $user instanceof \Illuminate\Contracts\Auth\MustVerifyEmail;
$this->userPlaygroundAbilitiesConcerns = in_array(Models\Concerns\Abilities::class, class_uses_recursive($user));
$this->userHasPlaygroundAbilitiesContracts = $user instanceof Models\Contracts\Abilities;
$this->userPlaygroundAdminConcerns = in_array(Models\Concerns\Admin::class, class_uses_recursive($user));
$this->userHasPlaygroundAdminContracts = $user instanceof Models\Contracts\Admin;
$this->userPlaygroundPrivilegesConcerns = in_array(Models\Concerns\Privileges::class, class_uses_recursive($user));
$this->userHasPlaygroundPrivilegesContracts = $user instanceof Models\Contracts\Privileges;
$this->userPlaygroundRoleConcerns = in_array(Models\Concerns\Role::class, class_uses_recursive($user));
$this->userHasPlaygroundRoleContracts = $user instanceof Models\Contracts\Role;
$this->userHasPlaygroundMatrixContracts = $user instanceof Models\Contracts\WithMatrix;
if (in_array(\Illuminate\Database\Eloquent\Concerns\HasUuids::class, class_uses_recursive($user))
&& ! $user->getIncrementing()
) {
$model_info = '<fg=green;options=bold>UUID</>';
} elseif ($user->getIncrementing()) {
$model_info = '<fg=green;options=bold>increments</>';
}
return $model_info;
}
public function register(): void
{
$this->mergeConfigFrom(
sprintf('%1$s/config/%2$s.php', dirname(__DIR__), $this->package),
$this->package
);
}
}