saas-laravel/laravel-enums-to-json 问题修复 & 功能扩展

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

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

saas-laravel/laravel-enums-to-json

最新稳定版本:3.0.0

Composer 安装命令:

composer require saas-laravel/laravel-enums-to-json

包简介

This package generates a json file off of values from enum

README 文档

README

The idea for this package is to streamline the process of sharing enums values between backend and frontend.

If you use Laravel as a backend for a frontend framework like Nuxt or Next, and they both live in the same project, you can utilize it to generate a json file from your enums.

Installation

You can install the package via composer:

composer require saas-laravel/laravel-enums-to-json

You can publish the config file with:

php artisan vendor:publish --tag="laravel-enums-to-json"

This is the contents of the published config file:

return [
    // The disk, defined in filesystem.php, where json files will be stored
    'disk' => 'public',

    // The folder on that disk where json will be generated
    'path' => '/shared',

    'enum_locations' => [
        app_path(),
    ],
];

Usage

In order to use the package, you need to add SaasLaravel\LaravelEnumsToJson\Attributes\EnumToJson attribute to your enum

Example usage:

<?php

namespace App\Enums;

use SaasLaravel\LaravelEnumsToJson\Attributes\EnumToJson; 

#[EnumToJson] //add attribute
enum CastType: int
{
    case Boolean = 0;
    case Integer = 1;
    case String = 2;

    // (optional) A way to customize the generated file name
    public static function jsonFileName(): string
    {
        return 'cast';
    }
}

Then you'd run a command to generate the json files.

php artisan enum-to-json:generate

Customizing the name

By default, it generates a file named as a normalized version of the enum name - that'd be cast_type.json if the example above didn't contain the jsonFileName method. However, because it is present, it'd generate cast.json instead.

Testing

composer test

Credits

License

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

统计信息

  • 总下载量: 29.06k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-28