AppServiceProvider.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Providers;
  3. use Carbon\CarbonImmutable;
  4. use Illuminate\Support\Facades\Date;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\ServiceProvider;
  7. use Illuminate\Validation\Rules\Password;
  8. use Godruoyi\Snowflake\Snowflake;
  9. use Godruoyi\Snowflake\LaravelSequenceResolver;
  10. use App\Tools\QueryBuilderMacro;
  11. use Illuminate\Database\Query\Builder as QueryBuilder;
  12. use App\Services\RomanizeService;
  13. use Illuminate\Support\Facades\View;
  14. use App\View\Composers\BlogViewComposer;
  15. class AppServiceProvider extends ServiceProvider
  16. {
  17. /**
  18. * Register any application services.
  19. */
  20. public function register(): void
  21. {
  22. /*
  23. |--------------------------------------------------------------------------
  24. | Snowflake ID Generator
  25. |--------------------------------------------------------------------------
  26. */
  27. $this->app->singleton('snowflake', function () {
  28. return (new Snowflake(
  29. config('mint.snowflake.data_center_id'),
  30. config('mint.snowflake.worker_id')
  31. ))
  32. ->setStartTimeStamp(
  33. strtotime(config('mint.snowflake.start')) * 1000
  34. )
  35. ->setSequenceResolver(
  36. new LaravelSequenceResolver(
  37. $this->app->get('cache')->store()
  38. )
  39. );
  40. });
  41. /*
  42. |--------------------------------------------------------------------------
  43. | Romanize Service
  44. |--------------------------------------------------------------------------
  45. */
  46. $this->app->singleton(RomanizeService::class);
  47. }
  48. /**
  49. * Bootstrap any application services.
  50. */
  51. public function boot(): void
  52. {
  53. /*
  54. |--------------------------------------------------------------------------
  55. | Laravel 13 Default Production Behaviors
  56. |--------------------------------------------------------------------------
  57. */
  58. $this->configureDefaults();
  59. /*
  60. |--------------------------------------------------------------------------
  61. | Custom Query Builder Macros
  62. |--------------------------------------------------------------------------
  63. */
  64. QueryBuilder::mixin(
  65. $this->app->make(QueryBuilderMacro::class)
  66. );
  67. /*
  68. |--------------------------------------------------------------------------
  69. | View Composers
  70. |--------------------------------------------------------------------------
  71. */
  72. View::composer('blog.*', BlogViewComposer::class);
  73. View::composer('layouts.blog', BlogViewComposer::class);
  74. }
  75. /**
  76. * Configure default behaviors for production-ready applications.
  77. */
  78. protected function configureDefaults(): void
  79. {
  80. /*
  81. |--------------------------------------------------------------------------
  82. | Use Immutable Dates
  83. |--------------------------------------------------------------------------
  84. */
  85. Date::use(CarbonImmutable::class);
  86. /*
  87. |--------------------------------------------------------------------------
  88. | Prevent destructive DB commands in production
  89. |--------------------------------------------------------------------------
  90. */
  91. DB::prohibitDestructiveCommands(
  92. app()->isProduction(),
  93. );
  94. /*
  95. |--------------------------------------------------------------------------
  96. | Strong password defaults in production
  97. |--------------------------------------------------------------------------
  98. */
  99. Password::defaults(
  100. fn(): ?Password => app()->isProduction()
  101. ? Password::min(12)
  102. ->mixedCase()
  103. ->letters()
  104. ->numbers()
  105. ->symbols()
  106. ->uncompromised()
  107. : null,
  108. );
  109. }
  110. }