主要的思路
Api类继承Facade,重写了getFacadeAccessor()该方法,目的是为了,
代码使用在Facade类中使用了 __callStatic,
Api::apiTypeOne() 相当于实例化了 TypeOne类
api:apiTypeTwon() 相当于实例化了 TypeTwo 类
回调本实例,在署名函数中完成操作
namespace App\Admin\Controllers;
use App\Libraries\Api\apiTypeOne;
$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 App\Libraries;
use App\Libraries\Facade;
class Api extends Facade {
static public function getFacadeAccessor() {
return \App\Libraries\Api\ApiImplement::class;
}
}
Facade.PHP 通过__callStatic 方法来实现通用
// file Facade.PHP
namespace App\Libraries;
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 <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>实例]
* @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 魔术<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>]
* @param [string] $method [<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>的<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>]
* @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 App\Libraries\Api;
use Closure;
class ApiImplement {
/**
* [apiTypeOne demo<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>]
* @param [type] $data [数据]
* @param Closure $callable [匿名<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a> 的类.]
* @return [type] [object]
*/
public function apiTypeOne($data,Closure $callable) {
return new TypeOne($data,$callable);
}
/**
* [apiTypTwo demo<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>2]
* @param [type] $data [数据]
* @param Closure $callable [匿名<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>的类.]
* @return [type] [object]
*/
public function apiTypTwo($data,Closure $callable) {
return new TypeTwo($data,$callable);
}
}
// api 子系统 apiTypeOne.PHP
namespace App\Libraries\Api;
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 App\Libraries\Api;
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;
}
}
原文链接:https://www.f2er.com/note/421013.html