Laravel的IOC容器和依赖注入

编程

1,  入口文件

<?php

/**

* Laravel - A PHP Framework For Web Artisans

*

* @package Laravel

* @author Taylor Otwell <taylor@laravel.com>

*/

define("LARAVEL_START", microtime(true));

1.1 注册自动加载器,也就是 Laravel的自动加载机制,可参看 Laravel Composer自动加载机制

/*

|--------------------------------------------------------------------------

| Register The Auto Loader

|--------------------------------------------------------------------------

|

| Composer provides a convenient, automatically generated class loader for

| our application. We just need to utilize it! We"ll simply require it

| into the script here so that we don"t have to worry about manual

| loading any of our classes later on. It feels great to relax.

|

*/

require __DIR__."/../vendor/autoload.php";

1.2 把灯打开。将框架运行起来,并让它就绪,这样它就可以加载应用,跑起来,然后返回响应。

/*

|--------------------------------------------------------------------------

| Turn On The Lights

|--------------------------------------------------------------------------

|

| We need to illuminate PHP development, so let us turn on the lights.

| This bootstraps the framework and gets it ready for use, then it

| will load up this application so that we can run it and send

| the responses back to the browser and delight our users.

|

*/

$app = require_once __DIR__."/../bootstrap/app.php";

在这一步,服务容器$app 就被实例化出来了。

1.3 运行应用。一旦我们获取到了应用程序,我们可以通过 kernel内核 来处理进来的请求,然后返回相关的响应结果。

/*

|--------------------------------------------------------------------------

| Run The Application

|--------------------------------------------------------------------------

|

| Once we have the application, we can handle the incoming request

| through the kernel, and send the associated response back to

| the client"s browser allowing them to enjoy the creative

| and wonderful application we have prepared for them.

|

*/

$kernel = $app->make(IlluminateContractsHttpKernel::class);

$response = $kernel->handle(

$request = IlluminateHttpRequest::capture()

);

$response->send();

$kernel->terminate($request, $response);

在这一步,通过服务容器$app示例化出了内核对象,内核又去处理被Request捕获到的请求,然后将响应结果发送回去,最后本次请求的内核对象停止工作。

 

2,如何运行

2.1 app/bootstrap/app.php 文件

2.1.1 创建应用:我们第一件要做的事情就是创建一个新的Laravel应用实例,这个实例会像 胶水 一样为Laravel的所有部件服务,同时它也是 绑定了所有各种各样的部分的系统 的 IOC容器。

/*

|--------------------------------------------------------------------------

| Create The Application

|--------------------------------------------------------------------------

|

| The first thing we will do is create a new Laravel application instance

| which serves as the "glue" for all the components of Laravel, and is

| the IoC container for the system binding all of the various parts.

|

*/

$app = new IlluminateFoundationApplication(

$_ENV["APP_BASE_PATH"] ?? dirname(__DIR__)

);

2.1.1 我们来看看这个容器是如何被创建出来的

这个构造方法在vendor/laravel/framework/src/Illuminate/Foundation/Application.php目录下,作用是创造一个新的容器

    /**

* Create a new Illuminate application instance.

*

* @param string|null $basePath

* @return void

*/

public function __construct($basePath = null)

{

if ($basePath) {

$this->setBasePath($basePath);

}

$this->registerBaseBindings();

$this->registerBaseServiceProviders();

$this->registerCoreContainerAliases();

}

2.1.1.1 设置基本的路径

2.2.1.2 注册 基本的绑定 到 容器中

   /**

* Register the basic bindings into the container.

*

* @return void

*/

protected function registerBaseBindings()

{

static::setInstance($this);

$this->instance("app", $this);

$this->instance(Container::class, $this);

$this->instance(PackageManifest::class, new PackageManifest(

new Filesystem, $this->basePath(), $this->getCachedPackagesPath()

));

}

第一步 把当前对象设置成共享的容器示例

第二步 将 "app" 抽象类  绑定到当前对象上

第三步 把 容器抽象类 绑定到当前对象上

第四步 把 货物清单 绑定到 PackageManifest() 中

2.2.1.3 注册 基础服务提供者

   /**

* Register all of the base service providers.

*

* @return void

*/

protected function registerBaseServiceProviders()

{

$this->register(new EventServiceProvider($this));

$this->register(new LogServiceProvider($this));

$this->register(new RoutingServiceProvider($this));

}

将 事件,日志,路由这三个 基础服务提供者 注册到当前对象中

2.2.1.4 注册核心类别名到容器中

   /**

* Register the core class aliases in the container.

*

* @return void

*/

public function registerCoreContainerAliases()

{

foreach ([

"app" => [IlluminateFoundationApplication::class, IlluminateContractsContainerContainer::class, IlluminateContractsFoundationApplication::class, PsrContainerContainerInterface::class],

"auth" => [IlluminateAuthAuthManager::class, IlluminateContractsAuthFactory::class],

"auth.driver" => [IlluminateContractsAuthGuard::class],

"blade.compiler" => [IlluminateViewCompilersBladeCompiler::class],

"cache" => [IlluminateCacheCacheManager::class, IlluminateContractsCacheFactory::class],

"cache.store" => [IlluminateCacheRepository::class, IlluminateContractsCacheRepository::class],

"config" => [IlluminateConfigRepository::class, IlluminateContractsConfigRepository::class],

"cookie" => [IlluminateCookieCookieJar::class, IlluminateContractsCookieFactory::class, IlluminateContractsCookieQueueingFactory::class],

"encrypter" => [IlluminateEncryptionEncrypter::class, IlluminateContractsEncryptionEncrypter::class],

"db" => [IlluminateDatabaseDatabaseManager::class],

"db.connection" => [IlluminateDatabaseConnection::class, IlluminateDatabaseConnectionInterface::class],

"events" => [IlluminateEventsDispatcher::class, IlluminateContractsEventsDispatcher::class],

"files" => [IlluminateFilesystemFilesystem::class],

"filesystem" => [IlluminateFilesystemFilesystemManager::class, IlluminateContractsFilesystemFactory::class],

"filesystem.disk" => [IlluminateContractsFilesystemFilesystem::class],

"filesystem.cloud" => [IlluminateContractsFilesystemCloud::class],

"hash" => [IlluminateHashingHashManager::class],

"hash.driver" => [IlluminateContractsHashingHasher::class],

"translator" => [IlluminateTranslationTranslator::class, IlluminateContractsTranslationTranslator::class],

"log" => [IlluminateLogLogManager::class, PsrLogLoggerInterface::class],

"mailer" => [IlluminateMailMailer::class, IlluminateContractsMailMailer::class, IlluminateContractsMailMailQueue::class],

"auth.password" => [IlluminateAuthPasswordsPasswordBrokerManager::class, IlluminateContractsAuthPasswordBrokerFactory::class],

"auth.password.broker" => [IlluminateAuthPasswordsPasswordBroker::class, IlluminateContractsAuthPasswordBroker::class],

"queue" => [IlluminateQueueQueueManager::class, IlluminateContractsQueueFactory::class, IlluminateContractsQueueMonitor::class],

"queue.connection" => [IlluminateContractsQueueQueue::class],

"queue.failer" => [IlluminateQueueFailedFailedJobProviderInterface::class],

"redirect" => [IlluminateRoutingRedirector::class],

"redis" => [IlluminateRedisRedisManager::class, IlluminateContractsRedisFactory::class],

"request" => [IlluminateHttpRequest::class, SymfonyComponentHttpFoundationRequest::class],

"router" => [IlluminateRoutingRouter::class, IlluminateContractsRoutingRegistrar::class, IlluminateContractsRoutingBindingRegistrar::class],

"session" => [IlluminateSessionSessionManager::class],

"session.store" => [IlluminateSessionStore::class, IlluminateContractsSessionSession::class],

"url" => [IlluminateRoutingUrlGenerator::class, IlluminateContractsRoutingUrlGenerator::class],

"validator" => [IlluminateValidationFactory::class, IlluminateContractsValidationFactory::class],

"view" => [IlluminateViewFactory::class, IlluminateContractsViewFactory::class],

] as $key => $aliases) {

foreach ($aliases as $alias) {

$this->alias($key, $alias);

}

}

}

至此,就把各种基础的路径,基础的绑定,基础服务提供者,核心类别名 都 注册或者绑定到了当前的应用对象中,返回了 $app 这个 应用实例,也就是容器示例。

 

2.1.2 然后将重要的接口绑定到这 应用实例/容器实例 中 ,

/*

|--------------------------------------------------------------------------

| Bind Important Interfaces

|--------------------------------------------------------------------------

|

| Next, we need to bind some important interfaces into the container so

| we will be able to resolve them when needed. The kernels serve the

| incoming requests to this application from both the web and CLI.

|

*/

$app->singleton(

IlluminateContractsHttpKernel::class,

AppHttpKernel::class

);

$app->singleton(

IlluminateContractsConsoleKernel::class,

AppConsoleKernel::class

);

$app->singleton(

IlluminateContractsDebugExceptionHandler::class,

AppExceptionsHandler::class

);

 

2.1.3  然后返回 应用实例。这个应用实例将会给到 被调用的脚本,所以这里我们得以将 应用实例的构建 和 真正的应用运行 及 发送响应 这两个步骤分开来。

/*

|--------------------------------------------------------------------------

| Return The Application

|--------------------------------------------------------------------------

|

| This script returns the application instance. The instance is given to

| the calling script so we can separate the building of the instances

| from the actual running of the application and sending responses.

|

*/

return $app;

至此,应用实例构造完成。

 

回到 index.php 入口文件,我们获取到了应用实例 / 容器示例 ,接下来,就是去运行应用实例了。

/*

|--------------------------------------------------------------------------

| Run The Application

|--------------------------------------------------------------------------

|

| Once we have the application, we can handle the incoming request

| through the kernel, and send the associated response back to

| the client"s browser allowing them to enjoy the creative

| and wonderful application we have prepared for them.

|

*/

$kernel = $app->make(IlluminateContractsHttpKernel::class);

$response = $kernel->handle(

$request = IlluminateHttpRequest::capture()

);

$response->send();

$kernel->terminate($request, $response);

 

以上是 Laravel的IOC容器和依赖注入 的全部内容, 来源链接: utcz.com/z/512856.html

回到顶部