定制 macmotp/codegen-laravel 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

macmotp/codegen-laravel

Composer 安装命令:

composer require macmotp/codegen-laravel

包简介

Generate human friendly codes - Laravel Extension

README 文档

README

Latest Version on Packagist Total Downloads

Generate human friendly codes

Useful for generation of referral codes based on names, receipt numbers, unique references.

This is the Laravel extension for the package Codegen - Generate Human Friendly Codes

Requirements

  • PHP >=8.1
  • Laravel >= 5

if you use PHP 7.4, please make sure to use v0.1.2

if you use PHP 8.0, please make sure to use v0.2.0

Installation

You can install the package via composer:

composer require macmotp/codegen-laravel

Usage

Create semantic and sanitized reference codes from any model by applying the trait

use Illuminate\Database\Eloquent\Model;
use Macmotp\HasCodegen;

class User extends Model
{
    use HasCodegen;
    
    protected $fillable = [
        'name',
        'code',
    ];

    /**
     * Attribute of the model used to generate the code
     *
     * @return string
     */
    protected function buildCodeFrom(): string
    {
        return $this->name;
    }
}

On the creating event, it will generate a human readable code to the specified column

$user = User::create([
    'name' => 'Bob McLovin',
]);

dump($user->code);
// (string) 'BBMCLV';

Configuration

Publish default configuration

Create config/codegen.php file, where you can adjust a few settings:

<?php
// config for Macmotp/HasCodegen
return [
    /*
    |--------------------------------------------------------------------------
    | The attribute of the model to build the code from.
    | For example, if your model has a column 'name' you can build the code from this attribute.
    | If empty, will generate random codes.
    |--------------------------------------------------------------------------
    */
    'build-from' => '',

    /*
    |--------------------------------------------------------------------------
    | The column use to save the code into the model.
    |--------------------------------------------------------------------------
    */
    'code-column' => 'code',

    /*
    |--------------------------------------------------------------------------
    | The length of the code to generate.
    |--------------------------------------------------------------------------
    */
    'code-length' => 6,

    /*
    |--------------------------------------------------------------------------
    | Sanitize level.
    | 1. Low/Default: will filter out anything is not a letter or a digit;
    | 2. Medium: will filter out (O - 0 - Q - I - 1) characters;
    | 3. High: will filter out (2 - Z - 4 - A - 5 - S - 8 - B - U - V - Y) characters;
    | Levels are inclusive, e.g. the highest level will apply also regex of level low and medium.
    |--------------------------------------------------------------------------
    */
    'sanitize-level' => 1,

    /*
    |--------------------------------------------------------------------------
    | Prepend a string.
    |--------------------------------------------------------------------------
    */
    'prepend' => '',
    
    /*
    |--------------------------------------------------------------------------
    | Append a string.
    |--------------------------------------------------------------------------
    */
    'append' => '',

    /*
    |--------------------------------------------------------------------------
    | Maximum accepted number of attempts for the generation.
    |--------------------------------------------------------------------------
    */
    'max-attempts' => 10000,
];

Custom configuration per model

Override custom configuration for a single model.

use Illuminate\Database\Eloquent\Model;
use Macmotp\HasCodegen;

class Foo extends Model
{
    use HasCodegen;
    
    protected $fillable = [
        'title',
        'reference',
    ];

    /**
     * Attribute of the model used to generate the code
     *
     * @return string
     */
    protected function buildCodeFrom(): string
    {
        return $this->title;
    }
    
    /**
     * Column used to save the unique code
     *
     * @return string
     */
    protected function getCodeColumn(): string
    {
        return 'reference';
    }
    
    /**
     * Get char length of the  code
     *
     * @return int
     */
    protected function getCodeLength(): int
    {
        return 12;
    }
    
    /**
     * Force to prepend this portion of string in the code
     *
     * @return string
     */
    protected function prependToCode(): string
    {
        return 'PR';
    }
    
    /**
     * Force to append this portion of string in the code
     *
     * @return string
     */
    protected function appendToCode(): string
    {
        return 'AP';
    }
    
    /**
     * Get the sanitize level to apply
     *
     * @return int
     */
    protected function getCodeSanitizeLevel(): int
    {
        return 3; // Level High
    }
}

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.

macmotp/codegen-laravel 适用场景与选型建议

macmotp/codegen-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 44 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 05 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-17