PailServiceProvider.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace Laravel\Pail;
  3. use Illuminate\Console\Events\CommandStarting;
  4. use Illuminate\Contracts\Foundation\Application;
  5. use Illuminate\Log\Events\MessageLogged;
  6. use Illuminate\Queue\Events\JobExceptionOccurred;
  7. use Illuminate\Queue\Events\JobProcessed;
  8. use Illuminate\Queue\Events\JobProcessing;
  9. use Illuminate\Support\ServiceProvider;
  10. use Laravel\Pail\Console\Commands\PailCommand;
  11. class PailServiceProvider extends ServiceProvider
  12. {
  13. /**
  14. * Registers the application services.
  15. */
  16. public function register(): void
  17. {
  18. $this->app->singleton(
  19. Files::class,
  20. fn (Application $app) => new Files($app->storagePath('pail'))
  21. );
  22. $this->app->singleton(Handler::class, fn (Application $app) => new Handler(
  23. $app,
  24. $app->make(Files::class),
  25. $app->runningInConsole(),
  26. ));
  27. }
  28. /**
  29. * Bootstraps the application services.
  30. */
  31. public function boot(): void
  32. {
  33. if (! $this->runningPailTests() && ($this->app->runningUnitTests() || ($_ENV['VAPOR_SSM_PATH'] ?? false))) {
  34. return;
  35. }
  36. /** @var \Illuminate\Contracts\Events\Dispatcher $events */
  37. $events = $this->app->make('events');
  38. $events->listen(MessageLogged::class, function (MessageLogged $messageLogged) {
  39. /** @var Handler $handler */
  40. $handler = $this->app->make(Handler::class);
  41. $handler->log($messageLogged);
  42. });
  43. $events->listen([CommandStarting::class, JobProcessing::class, JobExceptionOccurred::class], function (CommandStarting|JobProcessing|JobExceptionOccurred $lifecycleEvent) {
  44. /** @var Handler $handler */
  45. $handler = $this->app->make(Handler::class);
  46. $handler->setLastLifecycleEvent($lifecycleEvent);
  47. });
  48. $events->listen([JobProcessed::class], function () {
  49. /** @var Handler $handler */
  50. $handler = $this->app->make(Handler::class);
  51. $handler->setLastLifecycleEvent(null);
  52. });
  53. if ($this->app->runningInConsole()) {
  54. $this->commands([
  55. PailCommand::class,
  56. ]);
  57. }
  58. }
  59. /**
  60. * Determines if the Pail's test suite is running.
  61. */
  62. protected function runningPailTests(): bool
  63. {
  64. return $_ENV['PAIL_TESTS'] ?? false;
  65. }
  66. }