maize-tech/laravel-badges 问题修复 & 功能扩展

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

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

maize-tech/laravel-badges

Composer 安装命令:

composer require maize-tech/laravel-badges

包简介

Laravel Badges

README 文档

README

Social Card of Laravel Badges

Laravel Badges

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package lets you easily add badge mechanics to your application.

Installation

You can install the package via composer:

composer require maize-tech/laravel-badges

You can publish the config and migration files and run the migrations with:

php artisan badges:install

This is the contents of the published config file:

return [

    /*
    |--------------------------------------------------------------------------
    | Badge model
    |--------------------------------------------------------------------------
    |
    | Here you may specify the fully qualified class name of the badge model.
    |
    */

    'model' => Maize\Badges\Models\BadgeModel::class,

    /*
    |--------------------------------------------------------------------------
    | Badges
    |--------------------------------------------------------------------------
    |
    | Here you may specify the list of fully qualified class name of badges.
    |
    */

    'badges' => [
        // App\Badges\FirstLogin::class,
    ],

];

Usage

Basic

To use the package, firstly you should implement the Maize\Badges\HasBadges interface and apply the Maize\Badges\InteractsWithBadges trait to all models who can have badges:

<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Maize\Badges\HasBadges;
use Maize\Badges\InteractsWithBadges;

class User extends Authenticatable implements HasBadges
{
    use InteractsWithBadges;
}

Once done, all you have to do is define a class for each badge, extend the Maize\Badges\Badge class and implement the isAwarded abstract method:

<?php

namespace App\Badges;

use Illuminate\Database\Eloquent\Model;
use Maize\Badges\Badge;

class FirstLogin extends Badge
{
    public static function isAwarded(Model $model): bool
    {
        return $model->logins()->exists();
    }
}

Once done, don't forget to list the newly created badge within the badges list under config/badges.php:

'badges' => [
    App\Badges\FirstLogin::class,
],

Progressable badges

If your badge can have progress, you should extend the Maize\Badges\ProgressableBadge class and implement both the getTotal and getCurrent abstract methods:

<?php

namespace App\Badges;

use Illuminate\Database\Eloquent\Model;
use Maize\Badges\ProgressableBadge;

class FiveLogins extends ProgressableBadge
{
    public static function getTotal(): int
    {
        return 5;
    }

    public static function getCurrent(Model $model): bool
    {
        return $model->logins()->count();
    }
}

Under the hoods, the isAwarded method checks if getCurrent is equals or greater than getTotal.

Badge metadata

What is a badge without a name or description?

To accomplish this, you can override the metadata method within all badge classes.

Here is an example implementation using Laravel built-in translation method and the badge slug:

<?php

namespace App\Badges;

use Illuminate\Database\Eloquent\Model;
use Maize\Badges\Badge;

class FirstLogin extends Badge
{
    public static function isAwarded(Model $model): bool
    {
        return $model->logins()->exists();
    }
    
    public static function metadata(): array
    {
        $slug = static::slug();

        return [
            'name' => __("badges.{$slug}.name"),
            'description' => __("badges.{$slug}.name"),
        ];    
    }
}

Once done, you can retrieve the metadata using both the badge class and the BadgeModel entities:

use App\Badges\FirstLogin;
use Maize\Badges\Models\BadgeModel;

$user->giveBadge(FirstLogin::class);

$user->badges()->first()->metadata; // returns the list of metadata attributes

$user->badges()->first()->getMetadata('description'); // returns the attribute with key 'description'

FirstLogin::metadata(); // returns the list of metadata attributes

FirstLogin::getMetadata('description'); // returns the metadata attribute with key 'description'

Custom badge slug

All badges have a default slug used when storing a badge awarded event into the database.

The default slug is the badge's fully qualified class name. For example, FirstLogin badge's slug would be App\Badges\FirstLogin.

You can however customize the default behaviour overriding the slug method.

Here is an example using the badge's class basename in kebab case:

<?php

namespace App\Badges;

use Illuminate\Database\Eloquent\Model;
use Maize\Badges\Badge;

class FirstLogin extends Badge
{
    public static function slug(): string
    {
        return str(static::class)
            ->classBasename()
            ->kebab()
            ->toString(); // returns 'first-login'
    }

    public static function isAwarded(Model $model): bool
    {
        return $model->logins()->exists();
    }
}

Beware that all badge classes should have a unique slug to prevent inconsistencies.

Giving a badge

You can give a badge to any entity implementing the HasBadges interface using one of the following methods:

use App\Badges\FirstLogin;

$user->giveBadge(FirstLogin::class);

FirstLogin::giveTo($user);

When giving a badge, the isAwarded method will be evaluated to make sure the entity meets the conditions.

Every time a badge is given, a BadgeAwarded event will also be fired.

Check if a badge is awarded

To check if an entity has a badge, you can use the hasBadge method:

use App\Badges\FirstLogin;

$user->hasBadge(FirstLogin::class)

Retrieve awarded badges

To retrieve all badges awarded by an entity, you can use the badges relationship which returns a list of BadgeModel entities.

use Maize\Badges\Models\BadgeModel;

$user->badges->map->badge; // returns the list of awarded badges slug

Sync badges

To sync all badges for a given entity, you can use the syncBadges method, which retrieves all badges within the badges list under config/badges.php and checks whether it can be awarded or not.

$user->syncBadges();

Scheduling badges cleanup

The package also comes with the badges:clear command, which automatically deletes all stored badges which are not anymore listed within the badges list under config/badges.php:

You may schedule the command using the schedule method of the console kernel (usually located under the App\Console directory):

use Maize\Badges\Commands\ClearBadgesCommand;

$schedule->command(ClearBadgesCommand::class)->daily();

Testing

composer test

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

maize-tech/laravel-badges 适用场景与选型建议

maize-tech/laravel-badges 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.96k 次下载、GitHub Stars 达 37, 最近一次更新时间为 2023 年 10 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 maize-tech/laravel-badges 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 37
  • Watchers: 4
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-04