saas-laravel / laravel-enums-to-json
This package generates a json file off of values from enum
Installs: 11 161
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 0
Forks: 1
Open Issues: 1
Requires
- php: ^8.1
- illuminate/contracts: ^11.0
- spatie/laravel-package-tools: ^1.14.0
- spatie/php-structure-discoverer: ^2.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^v8.1.1
- orchestra/testbench: ^v9.0.4
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
This package is not auto-updated.
Last update: 2024-11-12 10:34:32 UTC
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.