Skip to content

Scan the specified path to obtain enumeration information through reflection

Notifications You must be signed in to change notification settings

carlin-rj/laravel-dict

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

laravel-dict

Scan the specified path to obtain enumeration information through reflection

安装:

composer require carlin/laravel-dict

发布配置文件:

php artisan vendor:publish --provider "Carlin\LaravelDict\DictServiceProvider"

配置文件:

<?php
return [
	'store'=>env('DICT_ENUM_STORE', env('CACHE_DRIVER', 'file')), //缓存驱动
	'cache-key'=>env('DICT_ENUM_CACHE_KEY', 'dict-cache-key'), //缓存key
	'cache-ttl'=> (int)env('DICT_ENUM_CACHE_TTL', 60 * 60 * 24 * 30), //缓存时间, 默认30天
	'enum-scan-paths'=>[
		//base_path('app/Enums/*.php'), //扫描路径
	],
];

例子:

<?php
namespace App\Enums;

use Carlin\LaravelDict\Dict;
use BenSampo\Enum\Enum;

class BaseEnum extends Enum {
    public static function getDescription(mixed $value): string
    {
        return Dict::getDescription(static::class, $value) ?? parent::getDescription($value);
    }

    public static function descriptions(): array
    {
        return Dict::getEnums(static::class);
    }
}
<?php

namespace App\Enums;

use Carlin\LaravelDict\Attributes\EnumClass;
use Carlin\LaravelDict\Attributes\EnumProperty;

#[EnumClass(
__CLASS__,  //枚举类名
'布尔整型枚举', //枚举描述
'webApi', //枚举分组
[
    'test'=>1 //业务拓展字段(选填)
]
)] //枚举类注解
class BoolIntEnums extends BaseEnum
{
    //#[EnumProperty('是', ['test'=>2])] //业务拓展字段(选填)
    #[EnumProperty('', ['test'=>2])]
    public const TRUE = 1;

    //#[EnumProperty('否', ['test'=>1])]
    #[EnumProperty('')]
    public const FALSE = 0;
}
<?php
use Carlin\LaravelDict\Dict;
//清除缓存
Dict::clearDictCache();
//获取枚举信息
Dict::getEnums(BoolIntEnums::class); //获取枚举信息
//getDescription
Dict::getDescription(BoolIntEnums::class, 1); //获取枚举描述
//获取枚举字典列表
Dict::getDict();
//获取指定group字典
Dict::getByGroup('webApi');

Dict::getDict输出示例:

//获取枚举字典列表
Array
(
    [App\Enums\BoolIntEnum] => Array
        (
            [name] => bool
            [description] => 布尔值字典
            [group] => webapi
            [test] => 1  //业务拓展字段(选填)
            ...//其他业务拓展字段
            [data] => Array
                (
                    [0] => Array
                        (
                            [name] => 是
                            [code] => 1
                            [test] => 2 //业务拓展字段(选填)
                            ...//其他业务拓展字段
                        )

                    [1] => Array
                        (
                            [name] => 否
                            [code] => 0
                            [test] => 1 //业务拓展字段(选填)
                            ...//其他业务拓展字段
                        )

                )

            [class] => App\Enums\BoolIntEnum
        )
)

About

Scan the specified path to obtain enumeration information through reflection

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages