此处使用为yii框架
1.创建接口文件Container.php
<?php
namespace common\container;
interface Container
{
//生产
public function make($abstract, $params = []): void;
//得到
public function acquire($abstract, $attribute = null);
//移除
public function remove($abstract): void;
//绑定
public function bind($abstract, $callBack, $name = null): void;
}
2.创建实现类
<?php
namespace common\container;
use common\exceptions\ApiException;
use common\tools\Tool;
/**
* 容器
* Class FuncContainer
* @package common\container
*/
class FuncContainer implements Container
{
//注册服务
private $registry = [];
/**
* 生成
* Created by Mr.亮先生.
* program: new-saas-api
* FuncName:make
* status:
* User: Mr.liang
* Date: 2020/9/1
* Time: 9:50
* Email:1695699447@qq.com
* @param $abstract
* @param array $params
* @throws ApiException
*/
public function make($abstract, $params = []): void
{
$this->checkMake($abstract);
$data = call_user_func_array($this->registry[$abstract]['callBack'], $params);
if (is_array($data)) {
$this->registry[$abstract]['isArray'] = true;
}
$this->registry[$abstract]['data'] = $data;
}
/**
* 内部检查是否绑定
* Created by Mr.亮先生.
* program: new-saas-api
* FuncName:checkMake
* status:
* User: Mr.liang
* Date: 2020/9/2
* Time: 9:58
* Email:1695699447@qq.com
* @param $abstract
* @throws ApiException
*/
private function checkMake($abstract): void
{
if (!isset($this->registry[$abstract])) {
throw new ApiException($abstract . '类没有被绑定注册');
}
}
/**
* 检查这个数据
* Created by Mr.亮先生.
* program: new-saas-api
* FuncName:check
* status:
* User: Mr.liang
* Date: 2020/9/1
* Time: 9:50
* Email:1695699447@qq.com
* @param $abstract
* @throws ApiException
*/
public function check($abstract): void
{
$this->checkMake($abstract);
if (!$this->registry[$abstract]['data']) {
throw new ApiException('该' . $this->registry[$abstract]['name'] . '不存在');
}
}
/**
* 检查具体某个属性
* Created by Mr.亮先生.
* program: new-saas-api
* FuncName:checkAttribute
* status:
* User: Mr.liang
* Date: 2020/9/1
* Time: 9:50
* Email:1695699447@qq.com
* @param $abstract
* @param $attribute
* @param $value
* @param null $msg
* @throws ApiException
*/
public function checkAttribute($abstract, $attribute, $value, $msg = null): void
{
$this->checkMake($abstract);
if ($this->registry[$abstract]['isArray']) {
$attribute = $this->registry[$abstract]['data'][$attribute];
} else {
$attribute = $this->registry[$abstract]['data']->$attribute;
}
if ((int)$attribute !== $value) {
$msg = $msg ?? '状态异常';
throw new ApiException($msg);
}
}
/**
* 得到
* Created by Mr.亮先生.
* program: new-saas-api
* FuncName:acquire
* status:
* User: Mr.liang
* Date: 2020/9/2
* Time: 9:58
* Email:1695699447@qq.com
* @param $abstract
* @param null $attribute
* @return mixed
* @throws ApiException
*/
public function acquire($abstract, $attribute = null)
{
$this->checkMake($abstract);
if ($attribute === null) {
return $this->registry[$abstract]['data'];
}
if ($this->registry[$abstract]['isArray']) {
return $this->registry[$abstract]['data']['$attribute'];
}
return $this->registry[$abstract]['data']->$attribute;
}
/**
* 移除
* Created by Mr.亮先生.
* program: new-saas-api
* FuncName:remove
* status:
* User: Mr.liang
* Date: 2020/9/2
* Time: 9:59
* Email:1695699447@qq.com
* @param $abstract
* @throws ApiException
*/
public function remove($abstract): void
{
$this->checkMake($abstract);
unset($this->registry[$abstract]);
}
/**
* 绑定
* Created by Mr.亮先生.
* program: new-saas-api
* FuncName:bind
* status:
* User: Mr.liang
* Date: 2020/9/2
* Time: 10:00
* Email:1695699447@qq.com
* @param $abstract
* @param $callBack
* @param null $name
* @throws ApiException
*/
public function bind($abstract, $callBack, $name = null): void
{
if (!is_callable($callBack)) {
throw new ApiException('对象生成器不是可以调用的类型');
}
$this->registry[$abstract] = ['callBack' => $callBack, 'name' => $name, 'isArray' => false, 'data' => null];
}
/**
* 更改
* Created by Mr.亮先生.
* program: new-saas-api
* FuncName:change
* status:
* User: Mr.liang
* Date: 2020/9/2
* Time: 10:00
* Email:1695699447@qq.com
* @param $abstract
* @param $attributes
* @return bool
* @throws ApiException
*/
public function change($abstract, $attributes): bool
{
$this->checkMake($abstract);
if ($this->registry[$abstract]['isArray'] === true) {
throw new ApiException('该方法只能模型对象调用');
}
$model = $this->registry[$abstract]['data'];
$model->attributes = $attributes;
if (!$model->save()) {
$msg = Tool::getFirstErrorMessage($model->firstErrors);
throw new ApiException($msg);
}
return true;
}
}
3.使用
public static function actionOperate(): ?array
{
$id = Tool::getRequestData('id', null);
$type = Tool::getRequestData('type', null);//到店 离店
$note = Tool::getRequestData('note', null);
$reception_membership = Tool::getRequestData('reception_membership', null);
$abstract = EntryRecord::tableName();
$container = new FuncContainer();
try {
$type = (int)$type;
if (!$id) {
throw new ApiException('请选择要查看的记录');
}
if (!$type) {
throw new ApiException('请选择操作类型');
}
if ($type === 1) {
if (!$reception_membership) {
throw new ApiException('请选择接待会籍');
}
$data['reception_membership'] = $reception_membership;
$data['entry_time'] = time();
} else {
$data['leaving_time'] = time();
}
$container->bind(
$abstract,
static function ($id) {
return EntryRecord::findOne(['id' => $id, 'entry_way' => 4]);
},
'到访'
);
$container->make($abstract, [$id]);
$container->check($abstract);
$info = $container->acquire($abstract);
if ($type === 1 && $info->entry_time) {
throw new ApiException('请勿重复到店');
}
if ($type === 2 && !$info->entry_time) {
throw new ApiException('请先到店再离店');
}
if ($type === 2 && $info->leaving_time) {
throw new ApiException('请勿重复离店');
}
$data['note'] = $info->note . ',' . $note;
$container->change($abstract, $data);
return Tool::success('操作成功');
} catch (ApiException $e) {
return Tool::error($e->getMessage());
}
}