DatabaseServiceProvider.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Illuminate\Database;
  3. use Faker\Factory as FakerFactory;
  4. use Faker\Generator as FakerGenerator;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Support\ServiceProvider;
  7. use Illuminate\Contracts\Queue\EntityResolver;
  8. use Illuminate\Database\Connectors\ConnectionFactory;
  9. use Illuminate\Database\Eloquent\QueueEntityResolver;
  10. use Illuminate\Database\Eloquent\Factory as EloquentFactory;
  11. class DatabaseServiceProvider extends ServiceProvider
  12. {
  13. /**
  14. * Bootstrap the application events.
  15. *
  16. * @return void
  17. */
  18. public function boot()
  19. {
  20. Model::setConnectionResolver($this->app['db']);
  21. Model::setEventDispatcher($this->app['events']);
  22. }
  23. /**
  24. * Register the service provider.
  25. *
  26. * @return void
  27. */
  28. public function register()
  29. {
  30. Model::clearBootedModels();
  31. $this->registerConnectionServices();
  32. $this->registerEloquentFactory();
  33. $this->registerQueueableEntityResolver();
  34. }
  35. /**
  36. * Register the primary database bindings.
  37. *
  38. * @return void
  39. */
  40. protected function registerConnectionServices()
  41. {
  42. // The connection factory is used to create the actual connection instances on
  43. // the database. We will inject the factory into the manager so that it may
  44. // make the connections while they are actually needed and not of before.
  45. $this->app->singleton('db.factory', function ($app) {
  46. return new ConnectionFactory($app);
  47. });
  48. // The database manager is used to resolve various connections, since multiple
  49. // connections might be managed. It also implements the connection resolver
  50. // interface which may be used by other components requiring connections.
  51. $this->app->singleton('db', function ($app) {
  52. return new DatabaseManager($app, $app['db.factory']);
  53. });
  54. $this->app->bind('db.connection', function ($app) {
  55. return $app['db']->connection();
  56. });
  57. }
  58. /**
  59. * Register the Eloquent factory instance in the container.
  60. *
  61. * @return void
  62. */
  63. protected function registerEloquentFactory()
  64. {
  65. $this->app->singleton(FakerGenerator::class, function ($app) {
  66. return FakerFactory::create($app['config']->get('app.faker_locale', 'en_US'));
  67. });
  68. $this->app->singleton(EloquentFactory::class, function ($app) {
  69. return EloquentFactory::construct(
  70. $app->make(FakerGenerator::class), $this->app->databasePath('factories')
  71. );
  72. });
  73. }
  74. /**
  75. * Register the queueable entity resolver implementation.
  76. *
  77. * @return void
  78. */
  79. protected function registerQueueableEntityResolver()
  80. {
  81. $this->app->singleton(EntityResolver::class, function () {
  82. return new QueueEntityResolver;
  83. });
  84. }
  85. }