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 = [];

/**
* FuncName:make
* @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;
}

/**
* FuncName:checkMake
* @param $abstract
* @throws ApiException
*/
private function checkMake($abstract): void
{
if (!isset($this->registry[$abstract])) {
throw new ApiException($abstract . ‘类没有被绑定注册’);
}
}

/**
* FuncName:check
* @param $abstract
* @throws ApiException
*/
public function check($abstract): void
{
$this->checkMake($abstract);
if (!$this->registry[$abstract][‘data’]) {
throw new ApiException(‘该’ . $this->registry[$abstract][‘name’] . ‘不存在’);
}
}

/**
* FuncName:checkAttribute
* @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);
}
}

/**
* FuncName:acquire
* @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;
}

/**
* FuncName:remove
* @param $abstract
* @throws ApiException
*/
public function remove($abstract): void
{
$this->checkMake($abstract);
unset($this->registry[$abstract]);
}

/**
* FuncName:bind
* @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];
}

/**
* FuncName:change
* @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());
        }
    }

转载请注明出处:https://www.onexin.net/yii-container/

相关文章:

1、用 PHP 编写更好的正则表达式regex
https://www.onexin.net/php-regex/

2、PHP 版本 8.2,8.1,8.0,7.4
https://www.onexin.net/php-8_2-8_1-8_0-7_4/

3、PHP中的Liskov替换原则
https://www.onexin.net/php-liskov/

4、php实现自运行的do实例详解
https://www.onexin.net/php-do/

5、PHP 后端实现JWT认证方法示例
https://www.onexin.net/php-jwt/

Leave a Reply