<?php
namespace Illuminate\Database;
use Faker\Factory as FakerFactory;
use Faker\Generator as FakerGenerator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\QueueEntityResolver;
use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Database\Eloquent\Factory as EloquentFactory;
class DatabaseServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application events.
* 启动应用程序事件。
* @return void
*/
public function boot()
{
Model::setConnectionResolver($this->app['db']);
Model::setEventDispatcher($this->app['events']);
}
/**
* Register the service provider.
* 注册服务提供者
* @return void
*/
public function register()
{
Model::clearBootedModels();
$this->registerEloquentFactory();
$this->registerQueueableEntityResolver();
// 连接工厂用于在数据库上创建实际的连接实例。 我们会将工厂注入管理器,以便它可以在实际需要而不是之前建立连接。
$this->app->singleton('db.factory', function ($app) {
return new ConnectionFactory($app);
});
// 数据库管理器用于解析各种连接,因为可能管理多个连接。 它还实现了连接解析器接口,其他需要连接的组件可以使用该接口。
$this->app->singleton('db', function ($app) {
return new DatabaseManager($app, $app['db.factory']);
});
$this->app->bind('db.connection', function ($app) {
return $app['db']->connection();
});
}
/**
* Register the Eloquent factory instance in the container.
* 在容器中注册 Eloquent 工厂实例。
* @return void
*/
protected function registerEloquentFactory()
{
$this->app->singleton(FakerGenerator::class, function () {
return FakerFactory::create();
});
$this->app->singleton(EloquentFactory::class, function ($app) {
$faker = $app->make(FakerGenerator::class);
return EloquentFactory::construct($faker, database_path('factories'));
});
}
/**
* Register the queueable entity resolver implementation.
* 注册可排队实体解析器实现。
* @return void
*/
protected function registerQueueableEntityResolver()
{
$this->app->singleton('Illuminate\Contracts\Queue\EntityResolver', function () {
return new QueueEntityResolver;
});
}
}
【Laravel5源码阅读】数据库服务提供者DatabaseServiceProvider
原创
©著作权归作者所有:来自51CTO博客作者深漂小码哥的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章