laravel5.5探究容器的秘密
阿新 • • 發佈:2018-03-27
AC ted xtend log 指定 stat ext target pre
[toc]
1. 定義一個契約(接口)
app\Contracts\SuperModuleContract.php
<?php
namespace App\Contracts;
interface SuperModuleContract
{
//激活超能力,這裏參數必須是array,這就是一個規範
public function activate(array $target);
}
2. 一個實現這個接口的類
app\Services\FlyPower.php
<?php namespace App\Services; use App\Contracts\SuperModuleContract; class FlyPower implements SuperModuleContract { public function activate(array $target) { //..可以根據$target 參數進行一些操作 $ability = 'fly ability'; return $ability; } }
3. 創建服務提供者
app\Providers\SuperPowerProvider.php
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use App\Services\FlyPower; class SuperPowerProvider extends ServiceProvider { public function boot() { // } public function register() { //singleton綁定單例 //如何使用:App::make('fly_power')方法時調用 $this->app->singleton('fly_power',function(){ return new FlyPower(); }); //bind綁定實例到接口以便依賴註入 // 如何使用:在類構造方法中實例化,並指定接口類型 $this->app->bind('App\Contracts\SuperModuleContract',function(){ return new FlyPower(); }); } }
4. 註冊服務提供者
配置文件config/app.php的providers數組中:
'providers' => [
//其他服務提供者
App\Providers\SuperPowerProvider::class,
],
5. 創建facades
app\Facades\FlyPower.php
<?php namespace App\Facades; use Illuminate\Support\Facades\Facade; class FlyPower extends Facade { protected static function getFacadeAccessor() { return 'fly_power'; } }
6. 再然後需要到配置文件config/app.php中註冊門面類別名:
'aliases' => [
...//其他門面類別名映射
'FlyPower' => App\Facades\FlyPower::class,
],
7. 寫一個控制器進行測試
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Contracts\SuperModuleContract;
use App;
use FlyPower;
class TestController extends Controller
{
protected $superower;
public function __construct(SuperModuleContract $super_power)
{
$this->super_power = $super_power;
}
public function getPower(Request $request){
//實現超人的幾種方式
# 使用make方式
$superman1 = App::make('fly_power')->activate(array());
# 使用facades
$superman2 = FlyPower::activate(array());
# 構造函數,類型提示,依賴註入
$superman3 = $this->super_power->activate(array());
dd($superman1, $superman2, $superman3);
}
}
結果:
"fly ability"
"fly ability"
"fly ability"
證明三個超人類都實現了。
8. 進一步分析
當有超人有多種超能力的時候怎麽辦
8.1 定義一個獲取超能力的接口
app\Contracts\GetPowerContract.php
<?php
namespace App\Contracts;
interface GetPowerContract
{
public function init();
}
8.2 一個實現獲取超能力接口的類
app\Services\GetPower.php
<?php
namespace App\Services;
use App\Contracts\GetPowerContract;
use App;
class GetPower implements GetPowerContract
{
public function init()
{
$config = config('power');
$ability = $config['ability'];
$ability = App::make($ability);
return $ability;
}
}
8.3 創建配置文件
app\config\power.php
<?php
return [
'ability' => 'fly_power',
];
8.4 增加一個超能力類,需要實現超能力接口
app\Services\ShotPower.php
<?php
namespace App\Services;
use App\Contracts\SuperModuleContract;
class ShotPower implements SuperModuleContract
{
public function activate(array $target)
{
//..可以根據$target 參數進行一些操作
$ability = 'shot ability';
return $ability;
}
}
8.5 修改服務提供器
app\Providers\SuperPowerProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\FlyPower;
use App\Services\ShotPower;
use App\Services\GetPower;
class SuperPowerProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//singleton綁定單例
//如何使用:App::make('fly_power')方法時調用
$this->app->singleton('fly_power',function(){
return new FlyPower();
});
$this->app->singleton('shot_power',function(){
return new ShotPower();
});
//bind綁定實例到接口以便依賴註入
// 如何使用:在類構造方法中實例化,並指定接口類型
$this->app->bind('App\Contracts\SuperModuleContract',function(){
return new FlyPower();
});
$this->app->bind('App\Contracts\GetPowerContract',function(){
//return new FlightPower();
return new GetPower();
});
}
}
8.6 修改控制器測試代碼
app\Services\ShotPower.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
//use App\Services\XPower;
use App\Contracts\SuperModuleContract;
use App\Contracts\GetPowerContract;
use App;
use FlyPower;
class TestController extends Controller
{
protected $superower;
//public function __construct(XPower $XPower)
//{
// $this->XPower = $XPower;
//}
//public function __construct(SuperModuleContract $super_power)
//{
// $this->super_power = $super_power;
//}
public function __construct(GetPowerContract $super_power)
{
$this->super_power = $super_power;
}
public function getPower(Request $request){
//實現超人的幾種方式
$superman1 = App::make('fly_power')->activate(array());
$superman2 = FlyPower::activate(array());
$superman3 = $this->super_power->init()->activate(array());
$superman4 = App::make('shot_power')->activate(array());
d($superman1, $superman2, $superman3, $superman4);
}
}
結果
"fly ability"
"fly ability"
"fly ability"
"shot ability"
說明我們賦予的shot超能力可以正常激活
修改config/power.php內容
<?php
return [
'ability' => 'shot_power',
];
結果
"fly ability"
"fly ability"
"shot ability"
"shot ability"
第三行的更改,說明我們可以通過更改config的方式實現快速切換超人擁有的超能力
laravel5.5探究容器的秘密