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