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