(未完成,待续) 把调用方法放在前面,方便理解
主要的思路
Api类继承Facade,重写了getFacadeAccessor()该方法,目的是为了,
代码使用在Facade类中使用了 __callStatic, Api::apiTypeOne() 相当于实例化了 TypeOne类
api:apiTypeTwon() 相当于实例化了 TypeTwo 类 回调本实例,在署名函数中完成操作
namespace AppAdminControllers;
use AppLibrariesApiapiTypeOne;
$content = Api::apiTypeOne($data,function (apiTypeOne $apiTypeOne) {
$header = 'Accept:text/xml,application/xml,application/xhtml+xml,text/html;';
$apiTypeOne->setHeaders($header);
});
Api.php 是外层的入口点,重写了getFacadeAccessor() 方法,根据自己的业务需求重写该方法,既可以通用的使用该设计模式
// file Api.php
namespace AppLibraries;
use AppLibrariesFacade;
class Api extends Facade {
static public function getFacadeAccessor() {
return AppLibrariesApiApiImplement::class;
}
}
Facade.php 通过__callStatic 方法来实现通用
// file Facade.php
namespace AppLibraries;
use RuntimeException;
abstract class Facade {
static public $resolvedInstance;
static public $app;
/**
- [getFacadeAccessor 获取访问器]
- @return [type] [description]
*/
static public function getFacadeAccessor() {
throw new RuntimeException('Facade does not implement getFacadeAccessor method.');
}
/**
* [getInstance 获取实例]
* @return [object] [instance]
*/
static public function getFacadeRoot() {
return static::resolveFacadeInstance(static::getFacadeAccessor());
}
static function resolveFacadeInstance($name) {
if( is_object($name) ) {
return $name;
}
if (isset(static::$resolvedInstance[$name])) {
return static::$resolvedInstance[$name];
}
return static::$resolvedInstance[$name] = new $name();
}
/**
* [__callStatic 魔术方法]
* @param [string] $method [调用的方法]
* @param [array] $args [参数]
* @return [mixed] [mixed]
*/
static public function __callStatic($method,$args) {
$instance = self::getFacadeRoot();
if (! $instance) {
throw new RuntimeException('A facade root has not been set.');
}
return $instance->$method(...$args);
}
}
api 子系统实现
//file ApiImplement.php
namespace AppLibrariesApi;
use Closure;
class ApiImplement {
/**
* [apiTypeOne demo方法]
* @param [type] $data [数据]
* @param Closure $callable [匿名函数 的类.]
* @return [type] [object]
*/
public function apiTypeOne($data,Closure $callable) {
return new TypeOne($data,$callable);
}
/**
* [apiTypTwo demo方法2]
* @param [type] $data [数据]
* @param Closure $callable [匿名函数的类.]
* @return [type] [object]
*/
public function apiTypTwo($data,Closure $callable) {
return new TypeTwo($data,$callable);
}
}
// api 子系统 apiTypeOne.php
namespace AppLibrariesApi;
use Closure;
class TypeOne{
protected $data;
protected $callable;
/**
* @param array $data
* @param callable $callback
*/
public function __construct($data,Closure $callback)
{
$this->data = $data;
$this->callable = $callback;
$callback($this);
}
public function setHeaders($header) {
$this->header = $header;
}
/**
* Get the string contents of the view.
* @return string
*/
public function __toString()
{
return $this->callable;
}
}
// api 子系统2 apiTypeTwo.php
namespace AppLibrariesApi;
use Closure;
class TypeTwo{
protected $data;
protected $callable;
/**
* @param array $data
* @param callable $callback
*/
public function __construct($data,Closure $callback)
{
$this->data = $data;
$this->callable = $callback;
$callback($this);
}
public function setHeaders($header) {
$this->header = $header;
}
/**
* Get the string contents of the view.
* @return string
*/
public function __toString()
{
return $this->callable;
}
} (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|