cjmellor/blockade 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cjmellor/blockade

Composer 安装命令:

composer require cjmellor/blockade

包简介

Allow a User Model to block another User Model

README 文档

README

Packagist Version GitHub Tests Action Status Packagist Downloads Packagist PHP Version Laravel Version

Allow a User to block another User. Blocking a User will prevent the blocker from seeing the blockees data.

Installation

You can install the package via composer:

composer require cjmellor/blockade

You can publish and run the migrations with:

php artisan vendor:publish --tag="blockade-migrations"

php artisan migrate

and publish the config file with:

php artisan vendor:publish --tag="blockade-config"

This is the contents of the published config file:

return [
    /**
     * If your User model different to the default, you can specify it here.
     */
    'user_model' => 'App\Models\User',
    
    /**
     * Specify the user model's foreign key.
     */
    'user_foreign_key' => 'user_id',

    /**
     * Specify the table name for the blocks table.
     */
    'blocks_table' => 'blocks',

    /**
     * Specify the foreign key for the blocker.
     */
    'blocker_foreign_key' => 'blocker_id',

    /**
     * Specify the foreign key for the blocked.
     */
    'blocked_foreign_key' => 'blocked_id',

    /**
     * Schedule the cleanup of expired blocks.
     */
    'schedule_cleanup' => false,
];

Usage

Using the Traits

CanBlock

Add the CanBlock trait to your User model.

This trait should only ever be added to your User model.

use Cjmellor\Blockade\Concerns\CanBlock;

class User
{
    use CanBlock;
    
    // ...
}

HasBlocked

Use this trait in your Models where you would not want a User to see the Model of a User they have blocked.

Example: you have a Comment Model and User 7 has blocked User 8 so User 7 should not be able to see any comments made by User 8.

use Cjmellor\Blockade\Concerns\HasBlocked;

class Comment
{
    use HasBlocked;
    
    // ...
}

Block a User

A User can block another User by supplying a User model, or just the ID of the User to be blocked.

$user->block(User::find(2)));

// or

$user->block(2);

Note: You cannot block yourself.

Unblock a User

A User can unblock another User by supplying a User model, or just the ID of the User to be unblocked.

$user->unblock(User::find(2)));

// or

$user->unblock(2);

Checking if a User is blocked

You can run a check to see if you're already blocking a User or not.

$user->isBlocking(User::find(2)));

// or

$user->isBlocking(2);

Set a block expiry

You can set an expiry date when blocking a User. This will automatically unblock the User after the expiry date has passed.

$user->block(User::find(2), expiresAt: now()->addDays(7)));

This example will block the User for 7 days. After 7 days, the User will be unblocked.

⏱️ Scheduling the cleanup of expired blocks

By default, a blocked User with an expiry will not be removed after the expiry time as past.

If you want to automatically remove expired blocks, you can set the schedule_cleanup config value in the config/blockade config file to true.

If you want more control over when the cleanup is run, there is an artisan command that can be run to remove expired blocks.

php artisan unblock:expired

You can also schedule this command to run automatically.

$schedule->command('unblock:expired')->everyMinute();

Note

Don't forget to add the command to the App\Console\Kernel schedule() method.

Events

When a User is blocked, a UserBlocked event is fired.

When a User is unblocked, a UserUnblocked event is fired.

The User Model of the blocked or unblocked User is passed to the event so you can listen for these events and perform further actions.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

cjmellor/blockade 适用场景与选型建议

cjmellor/blockade 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 20, 最近一次更新时间为 2023 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「cjmellor」 「blockade」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cjmellor/blockade 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cjmellor/blockade 我们能提供哪些服务?
定制开发 / 二次开发

基于 cjmellor/blockade 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 20
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 20
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-17