设为首页 加入收藏

TOP

laravel5.5源码笔记(二、服务提供者provider)(三)
2019-08-23 00:33:51 】 浏览:73
Tags:laravel5.5 源码 笔记 服务 提供者 provider
run.
10 //调用引导方法的钩子函数 11 $this->fireAppCallbacks($this->bootingCallbacks); 12 //使每个provider运行bootProvider,$p为provider 13 array_walk($this->serviceProviders, function ($p) { 14 $this->bootProvider($p); 15 }); 16 //改变引导状态 17 $this->booted = true; 18 //调用引导方法的钩子函数 19 $this->fireAppCallbacks($this->bootedCallbacks); 20 } 21 22 protected function bootProvider(ServiceProvider $provider) 23 { 24 //判断传入的provier,运行它们的boot方法完成引导 25 if (method_exists($provider, 'boot')) { 26 return $this->call([$provider, 'boot']); 27 } 28 }

到这里,provider通过register注册在了服务容器内,provider的初始化工作也由boot函数完成,这个provider所提供的对象便可以直接拿来使用了。

还记得学习laravel框架使用方式的时候,文档建议我们把所有在应用初始化时需要完成的事情,都写在AppServiceProvider的boot方法里吗?看到这里我们能明白作为系统核心prvider的app是最早被加载的,因此也充当了一个钩子函数的角色。

在了解了provider的注册流程之后,就可以自己来自定义一个provider了。我们上一篇博客里还有一个契约的概念没有说明,这里简单举一个小例子来说明。

1、新建一个接口。

 

1 namespace App\Contracts;
2 
3 interface Test
4 {
5     public function doing();
6 }

2、新建两个接口的实现

 1 namespace App\Services;
 2 
 3 use App\Contracts\Test;
 4 
 5 class TestService implements Test
 6 {
 7     public function doing()
 8     {
 9         echo 'this is TestService';
10     }
11 }
12 
13 
14 namespace App\Services;
15 
16 use App\Contracts\Test;
17 
18 class SecondTestService implements Test
19 {
20     public function doing()
21     {
22         echo 'this is SecondTestService';
23     }
24 }

3、新建一个provider,可使用artisan 命令行   php artisan make:provider TestServiceProvider 创建一个provider,契约上下文就在这个地方进行绑定。上一篇博文里讲到make方法的时候,容器在解析类的时候,有一个获取上下文的步骤,所要获取的concrete就是在provider中通过when方法绑定的类了,不过可惜这个绑定只能具体到类,不能具体到方法。

 1 namespace App\Providers;
 2 
 3 use Illuminate\Support\ServiceProvider;
 4 
 5 class TestServiceProvider extends ServiceProvider
 6 {
 7     /**
 8      * Bootstrap any application services.
 9      *
10      * @return void
11      */
12     public function boot()
13     {
14         //
15     }
16 
17     public function register()
18     {
19         $this->app->bind('App\Contracts\Test', 'App\services\TestService');
20         //重点在于when方法确定运行环境,也就是执行上下文,needs为make所需的abstract类名或别名,give所传入的参数则是实际调用的实现类了
21         $this->app->when('App\Http\Controllers\IndexController')
22                 ->needs('App\Contracts\Test')
23                 ->give('App\Services\SecondTestService');
24     }
25 }

4、在config/app.php文件的providers数组中添加刚刚生成的provider

 1     'providers' => [
 2 
 3         /*
 4          * Laravel Framework Service Providers...
 5          */
 6         Illuminate\Auth\AuthServiceProvider::class,
 7         Illuminate\Broadcasting\BroadcastServiceProvider::class,
 8         Illuminate\Bus\BusServiceProvider::class,
 9         Illuminate\Cache\CacheServiceProvider::class,
10         Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
11         Illuminate\Cookie\CookieServiceProvider::class,
12         Illuminate\Database\DatabaseServiceProvider::class,
13         Illuminate\Encryption\EncryptionServiceProvider::class,
14         Illuminate\Filesystem\FilesystemServiceProvider::class,
15         Illuminate\Foundation\Providers\FoundationServiceProvider::class,
16         Illuminate\Hashing\HashServiceProvider::class,
17         Illuminate\Mail\MailServiceProvider::class,
18         Illuminate\Notifications\NotificationServiceProvider::class,
19         Illuminate\Pagination\PaginationServiceProvider::class,
20         Illuminate\Pip
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇PHP标记 下一篇Linux系统下搭建MantisBT环境以及..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目