phpfacile / chat-db
使用数据库作为存储实现 phpfacile/chat 接口(旨在提供聊天/消息服务)
Requires
- php: >=7.0.0
- phpfacile/chat: ^1.0.0
- zendframework/zend-db: ^2.5.1
Requires (Dev)
- phpunit/dbunit: ^3.0
- phpunit/phpunit: ^6.2
- squizlabs/php_codesniffer: 3.*
This package is auto-updated.
Last update: 2024-09-23 06:01:13 UTC
README
这是一个使用数据库作为存储实现的 phpfacile/chat 接口的实现。
安装
在项目的根目录下,输入以下命令
composer require phpfacile/chat-db
或者将 "phpfacile/chat-db": "^1.0" 添加到 composer.json 文件中的 "require" 部分
"require": {
"phpfacile/chat-db": "^1.0"
}
您的数据库必须包含一个名为 "chat_messages" 的表,并且至少包含以下字段
- id: 整数自增
- msg: 字符串
- user_id: 整数或字符串
- channel_id: 整数或字符串
- insertion_datetime_utc: 日期时间或字符串
注意:当前版本中,表和字段名称不可配置
使用 SQLite 创建表的示例查询(仅用于测试)
CREATE TABLE chat_messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
channel_id INTEGER UNSIGNED NOT NULL,
user_id INTEGER UNSIGNED NOT NULL,
msg TEXT NOT NULL,
insertion_datetime_utc DATETIME NOT NULL
);
使用 MySQL 创建表的示例查询
CREATE TABLE `chat_messages` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `channel_id` BIGINT UNSIGNED NOT NULL, `user_id` BIGINT UNSIGNED NOT NULL, `msg` TEXT NOT NULL, `insertion_datetime_utc` DATETIME NOT NULL, UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
使用方法
步骤 1 : 适配器实例化
实例化一个 Zend 适配器以允许连接到数据库。
示例:使用 SQLite(仅用于测试)
$config = [ 'driver' => 'Pdo_Sqlite', 'database' => 'my_chat_database.sqlite', ]; $adapter = new Zend\Db\Adapter\Adapter($config);
示例:使用 MySQL
$config = [ 'driver' => 'Pdo_Mysql', 'host' => 'localhost' 'dbname' => 'my_database', 'user' => 'my_user', 'password' => 'my_pass', ]; $adapter = new Zend\Db\Adapter\Adapter($config);
步骤 2 : ChatChannelService 实例化
use PHPFacile\Chat\Service\ChatChannelService; $chatChannelService = new ChatChannelService();
注意:您可能需要覆盖默认的 ChatChannelService 以控制用户访问。(见下文)
步骤 3 : ChatService 实例化
use PHPFacile\Chat\Service\ChatService; $chatService = new ChatService($adapter, $chatChannelService);
步骤 4 : 发布或获取消息或消息信息
addMessage
您可以通过向 addMessage() 方法提供文本、频道 ID 和用户 ID 来将新消息添加到聊天频道。
$chatService->addMessage($text, $channelId, $userId);
注意:此方法不会检查 $channelId 或 $userId(是否已存在)
getMessages
您可以通过向 getMessages() 方法提供频道 ID 和用户 ID 来检索聊天频道中的所有消息(及其元数据)。
$msgs = $chatService->getMessages($channelId, $userId);
这将返回一个包含消息文本和相关数据的 StdClass 对象数组。
foreach ($msgs as $msg) { echo 'Id of the msg = '.$msg->id."\n"; echo 'Text = '.$msg->text."\n"; echo 'User id = '.$msg->user->id."\n"; echo 'Posted at = '.$msg->insertionDateTimeUTC."\n"; }
getLastUserMessageDateTimeUTC
您还可以通过向 getLastUserMessageDateTimeUTC() 方法提供频道 ID 和用户 ID 来检索用户在聊天频道中发布的最后一条消息的日期和时间(UTC)。
$dateTime = $chatService->getLastUserMessageDateTimeUTC($channelId, $userId);
这将返回一个字符串格式的日期(Y-m-d H:i:s),如 '2018-12-25 22:30:10',如果没有发布消息则返回 null。
高级功能
邀请您覆盖默认的 ChatChannelService(或提供您自己的 ChatChannelServiceInterface 实现)以编写自己的用户访问权限管理。
use PHPFacile\Chat\Service\ChatChannelServiceInterface; class CustomChatChannelService implements ChatChannelServiceInterface { public function isUserAllowedToAccessChannelMessages($userId, $channelId, $right) { // Return true if the user must be allowed to access // to the content of the chat channel either for // reading ($right = self::RIGHT_CHANNEL_MSG_READ) or for // writing ($right = self::RIGHT_CHANNEL_MSG_WRITE) return true; } } $chatChannelService = new CustomChatChannelService();
如果您想存储附加数据,可以在 addMessage() 方法的第四个参数中提供一个关联数组。数组的键必须与(现有)表字段名称匹配。
$extraData = [ 'software' => 'MyChatApp', ]; $chatService->addMessage($text, $channelId, $userId, extraData);