|
28 | 28 | use A17\Twill\Commands\Update;
|
29 | 29 | use A17\Twill\Commands\UpdateExampleCommand;
|
30 | 30 | use A17\Twill\Commands\UpdateMorphMapReferences;
|
| 31 | +use A17\Twill\Facades\TwillRoutes; |
31 | 32 | use A17\Twill\Http\ViewComposers\CurrentUser;
|
32 | 33 | use A17\Twill\Http\ViewComposers\FilesUploaderConfig;
|
33 | 34 | use A17\Twill\Http\ViewComposers\Localization;
|
|
42 | 43 | use Astrotomic\Translatable\TranslatableServiceProvider;
|
43 | 44 | use Cartalyst\Tags\TagsServiceProvider;
|
44 | 45 | use Exception;
|
| 46 | +use Illuminate\Contracts\Debug\ExceptionHandler; |
| 47 | +use Illuminate\Contracts\Foundation\CachesConfiguration; |
45 | 48 | use Illuminate\Database\Eloquent\Relations\Relation;
|
46 | 49 | use Illuminate\Foundation\AliasLoader;
|
| 50 | +use Illuminate\Foundation\Exceptions\Handler; |
47 | 51 | use Illuminate\Support\Facades\Blade;
|
48 | 52 | use Illuminate\Support\Facades\View;
|
49 | 53 | use Illuminate\Support\ServiceProvider;
|
50 | 54 | use Illuminate\Support\Str;
|
| 55 | +use Illuminate\Support\ViewErrorBag; |
| 56 | +use Illuminate\Validation\ValidationException; |
51 | 57 | use PragmaRX\Google2FAQRCode\Google2FA as Google2FAQRCode;
|
52 | 58 | use Spatie\Activitylog\ActivitylogServiceProvider;
|
| 59 | +use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
53 | 60 |
|
54 | 61 | class TwillServiceProvider extends ServiceProvider
|
55 | 62 | {
|
@@ -79,8 +86,6 @@ class TwillServiceProvider extends ServiceProvider
|
79 | 86 | */
|
80 | 87 | public function boot(): void
|
81 | 88 | {
|
82 |
| - $this->requireHelpers(); |
83 |
| - |
84 | 89 | $this->publishConfigs();
|
85 | 90 | $this->publishMigrations();
|
86 | 91 | $this->publishAssets();
|
@@ -116,8 +121,13 @@ private function requireHelpers(): void
|
116 | 121 | */
|
117 | 122 | public function register(): void
|
118 | 123 | {
|
119 |
| - $this->mergeConfigs(); |
| 124 | + $this->requireHelpers(); |
120 | 125 |
|
| 126 | + if (! ($this->app instanceof CachesConfiguration && $this->app->configurationIsCached())) { |
| 127 | + $this->mergeConfigs(); |
| 128 | + } |
| 129 | + |
| 130 | + $this->registerErrorHandlers(); |
121 | 131 | $this->registerProviders();
|
122 | 132 | $this->registerAliases();
|
123 | 133 | $this->registerFacades();
|
@@ -150,8 +160,6 @@ public function register(): void
|
150 | 160 | 'blocks' => config('twill.models.block', Block::class),
|
151 | 161 | 'groups' => config('twill.models.group', Group::class),
|
152 | 162 | ]);
|
153 |
| - |
154 |
| - config(['twill.version' => $this->version()]); |
155 | 163 | }
|
156 | 164 |
|
157 | 165 | private function registerFacades(): void
|
@@ -214,47 +222,6 @@ private function registerAliases(): void
|
214 | 222 | */
|
215 | 223 | private function publishConfigs(): void
|
216 | 224 | {
|
217 |
| - if (config('twill.enabled.users-management')) { |
218 |
| - config([ |
219 |
| - 'auth.providers.twill_users' => [ |
220 |
| - 'driver' => 'eloquent', |
221 |
| - 'model' => twillModel('user'), |
222 |
| - ], |
223 |
| - ]); |
224 |
| - |
225 |
| - config([ |
226 |
| - 'auth.guards.twill_users' => [ |
227 |
| - 'driver' => 'session', |
228 |
| - 'provider' => 'twill_users', |
229 |
| - ], |
230 |
| - ]); |
231 |
| - |
232 |
| - if (blank(config('auth.passwords.twill_users'))) { |
233 |
| - config([ |
234 |
| - 'auth.passwords.twill_users' => [ |
235 |
| - 'provider' => 'twill_users', |
236 |
| - 'table' => config('twill.password_resets_table', 'twill_password_resets'), |
237 |
| - 'expire' => 60, |
238 |
| - 'throttle' => 60, |
239 |
| - ], |
240 |
| - ]); |
241 |
| - } |
242 |
| - } |
243 |
| - |
244 |
| - config( |
245 |
| - ['activitylog.enabled' => config('twill.enabled.dashboard') ? true : config('twill.enabled.activitylog')] |
246 |
| - ); |
247 |
| - config(['activitylog.subject_returns_soft_deleted_models' => true]); |
248 |
| - |
249 |
| - config( |
250 |
| - [ |
251 |
| - 'analytics.service_account_credentials_json' => config( |
252 |
| - 'twill.dashboard.analytics.service_account_credentials_json', |
253 |
| - storage_path('app/analytics/service-account-credentials.json') |
254 |
| - ), |
255 |
| - ] |
256 |
| - ); |
257 |
| - |
258 | 225 | $this->publishes([__DIR__ . '/../config/twill-publish.php' => config_path('twill.php')], 'config');
|
259 | 226 | $this->publishes([__DIR__ . '/../config/translatable.php' => config_path('translatable.php')], 'config');
|
260 | 227 | }
|
@@ -299,6 +266,49 @@ private function mergeConfigs(): void
|
299 | 266 | }
|
300 | 267 |
|
301 | 268 | $this->mergeConfigFrom(__DIR__ . '/../config/services.php', 'services');
|
| 269 | + |
| 270 | + if (config('twill.enabled.users-management')) { |
| 271 | + config([ |
| 272 | + 'auth.providers.twill_users' => [ |
| 273 | + 'driver' => 'eloquent', |
| 274 | + 'model' => twillModel('user'), |
| 275 | + ], |
| 276 | + ]); |
| 277 | + |
| 278 | + config([ |
| 279 | + 'auth.guards.twill_users' => [ |
| 280 | + 'driver' => 'session', |
| 281 | + 'provider' => 'twill_users', |
| 282 | + ], |
| 283 | + ]); |
| 284 | + |
| 285 | + if (blank(config('auth.passwords.twill_users'))) { |
| 286 | + config([ |
| 287 | + 'auth.passwords.twill_users' => [ |
| 288 | + 'provider' => 'twill_users', |
| 289 | + 'table' => config('twill.password_resets_table', 'twill_password_resets'), |
| 290 | + 'expire' => 60, |
| 291 | + 'throttle' => 60, |
| 292 | + ], |
| 293 | + ]); |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + config( |
| 298 | + ['activitylog.enabled' => config('twill.enabled.dashboard') ? true : config('twill.enabled.activitylog')] |
| 299 | + ); |
| 300 | + config(['activitylog.subject_returns_soft_deleted_models' => true]); |
| 301 | + |
| 302 | + config( |
| 303 | + [ |
| 304 | + 'analytics.service_account_credentials_json' => config( |
| 305 | + 'twill.dashboard.analytics.service_account_credentials_json', |
| 306 | + storage_path('app/analytics/service-account-credentials.json') |
| 307 | + ), |
| 308 | + ] |
| 309 | + ); |
| 310 | + |
| 311 | + config(['twill.version' => $this->version()]); |
302 | 312 | }
|
303 | 313 |
|
304 | 314 | private function setLocalDiskUrl($type): void
|
@@ -625,4 +635,34 @@ public function check2FA(): void
|
625 | 635 | );
|
626 | 636 | }
|
627 | 637 | }
|
| 638 | + |
| 639 | + private function registerErrorHandlers(): void |
| 640 | + { |
| 641 | + $handler = app(ExceptionHandler::class); |
| 642 | + if ($handler instanceof Handler) { |
| 643 | + $handler->renderable(function (HttpExceptionInterface $e) { |
| 644 | + $statusCode = $e->getStatusCode(); |
| 645 | + if (TwillRoutes::isTwillRequest()) { |
| 646 | + $view = "twill.errors.$statusCode"; |
| 647 | + |
| 648 | + $view = view()->exists($view) ? $view : "twill::errors.$statusCode"; |
| 649 | + } else { |
| 650 | + $view = config('twill.frontend.views_path') . ".errors.$statusCode"; |
| 651 | + |
| 652 | + $view = view()->exists($view) ? $view : null; |
| 653 | + } |
| 654 | + return $view ? response()->view($view, [ |
| 655 | + 'errors' => new ViewErrorBag(), |
| 656 | + 'exception' => $e, |
| 657 | + ], $e->getStatusCode(), $e->getHeaders()) : null; |
| 658 | + }); |
| 659 | + |
| 660 | + $handler->renderable(function (ValidationException $exception) { |
| 661 | + if (TwillRoutes::isTwillRequest() && request()->expectsJson()) { |
| 662 | + return response()->json($exception->errors(), $exception->status); |
| 663 | + } |
| 664 | + return null; |
| 665 | + }); |
| 666 | + } |
| 667 | + } |
628 | 668 | }
|
0 commit comments