[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的方式实现快速切换超人拥有的超能力