目录
1. 定义一个契约(接口)
appContractsSuperModuleContract.php
<?php
namespace AppContracts;
interface SuperModuleContract
{
//激活超能力,这里参数必须是array,这就是一个规范
public function activate(array $target);
}
2. 一个实现这个接口的类
appServicesFlyPower.php
<?php
namespace AppServices;
use AppContractsSuperModuleContract;
class FlyPower implements SuperModuleContract
{
public function activate(array $target)
{
//..可以根据$target 参数进行一些操作
$ability = 'fly ability';
return $ability;
}
}
3. 创建服务提供者
appProvidersSuperPowerProvider.php
<?php
namespace AppProviders;
use IlluminateSupportServiceProvider;
use AppServicesFlyPower;
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('AppContractsSuperModuleContract',function(){
return new FlyPower();
});
}
}
4. 注册服务提供者
配置文件config/app.php的providers数组中:
'providers' => [
//其他服务提供者
AppProvidersSuperPowerProvider::class,
],
5. 创建facades
appFacadesFlyPower.php
<?php
namespace AppFacades;
use IlluminateSupportFacadesFacade;
class FlyPower extends Facade
{
protected static function getFacadeAccessor()
{
return 'fly_power';
}
}
6. 再然后需要到配置文件config/app.php中注册门面类别名:
'aliases' => [
...//其他门面类别名映射
'FlyPower' => AppFacadesFlyPower::class,
],
7. 写一个控制器进行测试
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppContractsSuperModuleContract;
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 定义一个获取超能力的接口
appContractsGetPowerContract.php
<?php
namespace AppContracts;
interface GetPowerContract
{
public function init();
}
8.2 一个实现获取超能力接口的类
appServicesGetPower.php
<?php
namespace AppServices;
use AppContractsGetPowerContract;
use App;
class GetPower implements GetPowerContract
{
public function init()
{
$config = config('power');
$ability = $config['ability'];
$ability = App::make($ability);
return $ability;
}
}
8.3 创建配置文件
appconfigpower.php
<?php
return [
'ability' => 'fly_power',
];
8.4 增加一个超能力类,需要实现超能力接口
appServicesShotPower.php
<?php
namespace AppServices;
use AppContractsSuperModuleContract;
class ShotPower implements SuperModuleContract
{
public function activate(array $target)
{
//..可以根据$target 参数进行一些操作
$ability = 'shot ability';
return $ability;
}
}
8.5 修改服务提供器
appProvidersSuperPowerProvider.php
<?php
namespace AppProviders;
use IlluminateSupportServiceProvider;
use AppServicesFlyPower;
use AppServicesShotPower;
use AppServicesGetPower;
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('AppContractsSuperModuleContract',function(){
return new FlyPower();
});
$this->app->bind('AppContractsGetPowerContract',function(){
//return new FlightPower();
return new GetPower();
});
}
}
8.6 修改控制器测试代码
appServicesShotPower.php
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
//use AppServicesXPower;
use AppContractsSuperModuleContract;
use AppContractsGetPowerContract;
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的方式实现快速切换超人拥有的超能力