From Apprentice To Artisan 翻译 07

Service Providers 服务提供者

As Bootstrapper 他是引导程序

A Laravel service provider is a class that registers IoC container bindings. In fact, Laravel ships with over a dozen service providers that manage the container bindings for the core framework components. Almost every component of the framework is registered with the container via a provider. A list of the providers currently being used by your application can be found in the providers array within the app/config/app.php configuration file.

一个Laravel服务提供者就是一个用来进行IoC绑定的类。事实上,Laravel有好几十个服务提供者,用于管理框架核心组件的容器绑定。几乎框架里每一个组件的IoC绑定都是靠服务提供者来做的。你可以在app/config/app.php这个文件里查看目前有哪些服务提供者。

A service provider must have at least one method: register. The register method is where the provide binds classes to the container. When a request enters your application and the framework is booting up, the register method is called on the providers listed in your configuration file. This happens very early in the application life-cycle so that all of the Laravel services will be available to you when your own bootstrap files, such as those in the start directory, are loaded.

一个服务提供者必须有一个register方法。你可以在这个方法里写IoC绑定。当一个请求发过来,程序框架刚启动时,所有在你配置文件里的服务提供者的register方法就会被调用。这在程序周期的很早的地方就会执行,所以在你自己的引导代码(比如那些在start目录里的文件)里所有的服务已经准备好了。

Register Vs. Boot注册Vs引导代码

Never attemp to use services in the register method. This method is only for binding objects into the IoC container. All resolutions and interaction with the bound classes must be done inside the boot method.

永远不要在register方法里面使用任何服务。该方法只是用来进行IoC绑定的地方。所有关于绑定类后续的判断、交互都要在boot方法里进行。

Some third-party packages you install via Composer will ship with a service provider. The package's installation instructions will typically tell you to register their provider with your application by adding it to the providers array in your configuration file. Once you've registered the package's provider, it is ready for use.

你用Composer安装的一些第三方包也会有服务提供者。在第三方包的安装说明里一般都会告诉你要在providers数组里加上一行。一旦你加上了,那这个服务就算安装好了。

Package Providers包提供者

Not all third-party packages require a service provider. In fact, no package requires a service provider, as service providers generally just bootstrap components so they are ready for use immediately. They are simply a convenient place to organize bootstrap code and container bindings.

不是所有的第三方包都需要服务提供者。事实上一个包并不需要服务提供者。因为服务提供者只是一个用来自动初始化服务组件的地方,一个方便管理引导代码和容器绑定的地方。

Deferred Providers 延迟加载的服务提供者

Not every provider that is listed in your providers configuration array will be instantiated on every request. This would be detrimental to performance, especially if the services the provider registers are not needed for the request. For example, the QueueServiceProvider services are not needed on every request, but only on requests where the queue is actually utilized.

并非在你配置文件中的providers数组里的所有提供者在每次请求都会被实例化。否则会对性能不利,尤其是这个服务的功能用不到的情况下。比如,QueueServiceProvider服务就不是每次都用得到。

In order to only instantiate the providers that are needed for a given request, Laravel generates a “service manifest” that is stored in the app/storage/meta directory. This manifest lists all of the service providers for the application, as well as the names of the container bindings they register. So, when the application asks the container for the queue container binding, Laravel knows that it needs to instantiate and run the QueueServiceProvider because it is listed as providing the queue service in the manifest. This allows the framework to lazy-load service providers for each request, greatly increasing performance.

为了达到只实例化需要的服务的提供者,Laravel生成了“服务清单”并且储存在了app/storage/meta目录下。这份清单列出了应用里所有的服务提供者,包括容器绑定的名字也记录了。这样,当应用想让容器取出一个名为queue的绑定时,Laravel知道需要先实例化并运行QueueServiceProvider因为在服务清单里记录着该服务提供者能提供queue的绑定。如此这般框架就能够延迟加载每个请求需要的服务了,性能大大提高。

Manifest Generation如何生成服务清单

When you add a service provider to the providers array, Laravel will automatically regenerate the service manifest file during the next request to the application.

当你在providers数组里新增一条,Laravel在下一次请求时就会自动重新生成服务清单。

As you have time, take a look through the service manifest so you are aware of its contents. Understanding how this file is structured will be helpful if you ever need a debug a deferred service provider.

如果你有时间,去看看服务清单文件里面的内容。理解这个文件的结构有助于你对服务进行排错。

你可能感兴趣的:(From Apprentice To Artisan 翻译 07)