开发文档 v2.x

核心工厂(Be)

更新时间:2024年3月5日 10:07 浏览:969

BE 双驱框架最核心的基础功能 Be 工厂,用来控制对象的创建,管理,及回收。

Be 工厂主要提供了几类方法:

  • getXxx - 以 get 开头的 的方法使用了单像创建,多次调用将使用同一实例。
  • newXxx - 以 new 开头的 的方法会新建一个实例,
  • 其它

代码示例:

// 将重复使用一个数据库操作类,共用同一个数据库连接
$db =  Be::getDb();

// 将新建一个数据库操作类,单独占用一个数据库连接
$db = Be::newDb() 

 

getCid - 获取 swoole 模式下的协程ID

public static function getCid(): int

每个访问请求对应一个协程ID, 普通 php 模式下返回0

代码示例:

$cid =  Be::getCid();

 

getRuntime - 获取运行时对象

public static function getRuntime(): \Be\Runtime\Driver

代码示例:

$runtime =  Be::getRuntime();

 

 getRequest - 获取请求对象

public static function getRequest() : \Be\Request\Driver

请求对象封装了两种模式下的差异。

代码示例:

$request = Be::getRequest();

// 获取 GET 请求数据
$id = $request->get('id', '');
// 获取 POST 请求数据
$id = $request->post('id', '');
// 获取 JSON 数据
$id = $request->json('id', '');
// 获取 Cookie 数据
$id = $request->cookie('id', '');

 

getResponse - 获取输出对象

public static function getResponse() : \Be\Response\Driver

为兼容 Swoole,开发人员需要使用 BE 框架提供的输出对象输出内容。

代码示例:

$response = Be::getResponse();

// 输出内容
$response->write('hello world!');
// 输出Cookie
$response->cookie('key', 'hello world!');
// 演染模板
$response->display('App.Cms.Article.Detail');

 

getConfig - 获取指定的配置文件

public static function getConfig(string $name): object

代码示例:

// 获取系统应用下的 ES 配置
$configEs = Be::getConfig('App.System.Es');

 

newConfig - 获取指定的配置文件,创新新实例

public static function newConfig(string $name): object

代码示例:

// 获取系统应用下的 ES 配置
$configEs = Be::newConfig('App.System.Es');


getLog - 获取 日志记录器 单例

public static function getLog(): \Be\Log\Driver

代码示例:

$log = Be::getLog();
$log->error('出错啦');


 newLog - 创建 日志记录器 新的实例

public static function newLog(): \Be\Log\Driver

 

getProperty - 获取一个属性 单例

public static function getProperty(string $name): \Be\Property\Driver

代码示例:

$property = Be::getProperty('App.Cms');

$appName = $property->getName();; // APP 名称
$appIcon = $property->getIcon();; // APP 图标
$appPath = $property->getPath(); // APP 所在目录


getSession - 获取SESSION会话对象

public static function getSession(): \Be\Session\Driver

SESSION会话提供了 文件 和 Redis 两种驱动

代码示例:

$session = Be::getSession();

$session->set('userId', 123);; // 存储会话数据
$userId = $session->get('userId');; // 获取会话数据


getCache - 获取 Cache 单例

public static function getCache(): \Be\Cache\Driver

缓存提供了 文件 和 Redis 两种驱动

代码示例:

$cache = Be::getCache();

$cache->set('userId', 123);; // 缓存数据
$cache->set('userId', 123, 600);; // 缓存数据 600秒后失效
$userId = $cache->get('userId');; // 获取缓存数据


newCache - 创建 缓存对象 实例

public static function newCache(): \Be\Cache\Driver

 

getStorage - 获取 存储 单例

public static function getStorage(): \Be\Storage\Driver

存储提供了本地磁盘,阿里云OSS,腾讯云COS 等驱动

代码示例:

$storage = Be::getStorage();
// 上传 src.txt
// 如果存储驱动为本地磁盘,则上传到 /www 目录下:/www/abc.txt
// 如果存储驱动为对象存储,则上传到远端对象存储服务器中
$url = $storage->uploadFile('/dst.txt',  'src.txt');


newStorage - 创建 存储 新实例

public static function newStorage(): \Be\Storage\Driver

 

getRedis - 获取Redis对象 单例

public static function getRedis(string $name = 'master')

代码示例:

$redis = Be::getRedis();

$redis->set('userId', 123);; // 保存数据
$redis->setEx('userId', 600);; // 600秒后失效
$userId = $redis->get('userId');; // 获取数据


newRedis - 创建一个 Redis对象 实例

public static function newRedis(string $name = 'master')

 

getEs - 获取ES对象 单例

public static function getEs() : \Elasticsearch\Client

代码示例:

$es = Be::getEs();

$result = $es->search([
    // ...
]);


newEs - 创建一个 ES对象 实例

public static function newEs() : \Elasticsearch\Client

 

getDb - 获取 数据库对象 单例

public static function getDb(string $name = 'master') : \Be\Db\Driver

代码示例:

$db = Be::getDb();
$articles = $db->getObjects('SELECT * FROM cms_article');

 

newDb - 创建一个 数据库对象 实例

public static function newDb(string $name = 'master') : \Be\Db\Driver

 

getTable - 获取指定的一个数据库表对象

public static function getTable(string $name, string $db = 'master') : \Be\Db\Table

注意,此方法特殊,因表对象不会有共享需求,固未做单例处理。

代码示例:

$articles = Be::getTable('cms_article')
    ->where('is_enable', 1)
    ->getObjects();

 

getTuple - 获取指定的一个数据库行记灵对象

public static function getTuple(string $name, string $db = 'master') : \Be\Db\Tuple

注意,此方法特殊,因行记灵对象不会有共享需求,固未做单例处理。

代码示例:

$tupleArticle = Be::getTuple('cms_article');
$tupleArticle->title = '新建文章';
$tupleArticle->insert(); // 插入

$tupleArticle = Be::getTuple('cms_article');
$tupleArticle->load('xxx'); // 加载主键为指定值的行
$tupleArticle->title = '编辑文章';
$tupleArticle->update(); // 更新


getTableProperty - 获取指定的一个数据库表属性

public static function getTableProperty(string $name, string $db = 'master') : \Be\Db\TableProperty

代码示例:

$tableProperty = Be::getTableProperty('cms_article');

// 获取表的主键,联合主键时返回数组
$primaryKey = $tableProperty->getPrimaryKey();
// 获取表的字段明细列表
$fields = $tableProperty->getFields();

 

getMongoDb - 获取 MongoDb 对象 单例

public static function getMongoDb(string $name) : \Be\MongoDb\Driver

 

newMongoDb - 创建一个 MongoDb 对象 新实例

public static function newMongoDb(string $name) : \Be\MongoDb\Driver

 

getService - 获取指定的一个服务 单例

public static function getService(string $name)

代码示例:

$service = Be::getService('App.Cms.Article');

// 调用服务的方法
$articles = $service->getLatestArticles(10);

 

newService - 创建一个服务 实例

public static function newService(string $name)

 

getLib - 获取指定的库 单例

public static function getLib(string $name)

 

newLib - 创建 指定的库 新实例

public static function newLib(string $name)

 

getPlugin - 获取指定的一个扩展 单例

public static function getPlugin(string $name)

 

newPlugin - 创建一个指定的扩展  新实例

public static function newPlugin(string $name)

 

getAdminPlugin - 获取指定的一个后台扩展 单例

public static function getAdminPlugin(string $name)

代码示例:

$adminPlugin = Be::getAdminPlugin('Curd');
$adminPlugin->setting([
    // ...
])->execute();

 

newAdminPlugin - 创建一个新的指定的后台扩展 实例

public static function newAdminPlugin(string $name)

 

getTemplate - 获取指定的一个模板 单例

public static function getTemplate(string $template, string $theme = null)

 

getAdminTemplate - 获取指定的一个后台模板 单例

public static function getAdminTemplate(string $template, string $theme = null)

 

getMenu - 获取指定的一个菜单 单例

public static function getMenu($name): \Be\Menu\Driver

代码示例:

// 获取顶部菜单
$northMenu = Be::getMenu('North');
$menuTree = $menu->getTree();
foreach ($menuTree as $item) {
    // ...
}


getAdminMenu - 获取后台菜单

public static function getAdminMenu()

代码示例:

$adminMenu = \Be\Be::getAdminMenu();
$adminMenuTree = $adminMenu->getTree();


getAdminRole - 获取指定的一个后台角色信息 

public static function getAdminRole(string $roleId) :\Be\AdminUser\AdminRole

代码示例:

$roleId = 'xxxx';
$role = Be::getAdminRole($roleId);

// 判断角色是否有权限访问指定资源
if ($role->hasPermission($app, $controller, $action)) {
    return true;
}

 

getAdminPermission - 获取后台权限信息

public static function getAdminPermission() : \Be\AdminUser\AdminPermission

 

setAdminUser - 后台用户登录后,设置当前后台用户

public static function setAdminUser($adminUser = null)

 

getAdminUser - 获取当前后台用户

public static function setAdminUser($adminUser = null) : \Be\AdminUser\AdminUser

代码示例:

$my = Be::getAdminUser();

// 判断用户是否登录
if ($my->isGuest()) {
    // ...
}

$name = $my->name;
$avatar = $my->avatar;

 

setUser - 用户登录后,设置当前用户

public static function setUser($user = null)

 

getUser - 获取当前用户

public static function getUser(): \Be\User\User

代码示例:

$my = Be::getUser();

// 判断用户是否登录
if ($my->isGuest()) {
    // ...
}

 

 setGlobal - 设置全局数据

public static function setGlobal(string $name, $value)

Swoole 模式下,服务器重启前,该数据有效,普通模式下仅在当前访问结束前有效

代码示例:

Be::setBlobal('loaded', 1);

 

getGlobal - 获取全局数据

public static function getGlobal(string $name)

代码示例:

$loaded = Be::getBlobal('loaded');

 

hasGlobal - 全局数据是否存在

public static function hasGlobal(string $name): bool

代码示例:

if (Be::hasBlobal('loaded')) {
    // ...
}


setContext - 设置上下文

public static function setContext(string $name, $value)

仅在当前会话下有效,会话结束后,数据销毁

代码示例:

Be::setContext('loaded', 1);

 

getContext - 获取上下文

public static function getContext(string $name)

仅在当前会话下有效,会话结束后,数据销毁

代码示例:

$loaded = Be::getContext('loaded');

 

hasContext - 上下文是否存在

public static function hasContext(string $name): bool

代码示例:

if (Be::hasContext('loaded')) {
    // ...
}

 

gc - 回收资源

public static function gc()

开发人员一般不需要主动调用,框架会在合适的节点调用此方法回收资源

代码示例:

Be::gc();

 

导航