Coder Social home page Coder Social logo

sync-enum-types's Introduction

This is my package sync-enum-types

Latest Version on Packagist GitHub Tests Action Status Total Downloads

This package ships a command that creates typescript declaration files for your php Enums. This command can be set so it automatically runs on-save, for example.

Installation

You can install the package via composer:

composer require mudandstars/sync-enum-types --dev

Usage (VS Code)

  1. Check if the published config needs changes, based on your project structure
  2. Install the 'Run on Save' Extension
  3. Add the 'Run on Save' command to your settings.json (I like the workspace settings for this):
{
	...other settings

	"emeraldwalk.runonsave": {
		"commands": [
			{
				"match": ".*/Enum/.*\\.php$",
				"cmd": "php artisan sync-enum-types"
			}
		]
	}
}

Configuration

You can publish the config file with:

php artisan vendor:publish --tag="sync-enum-types-config"

The recommended config has SYNC_CASES set to true to provide the enum cases as typed array for use in the frontend. Its latest version looks like this:

// config/sync-enum-types.php
return [
    'PHP_ENUM_FOLDER_DESTINATION' => app_path('Enum'),
    'TYPESCRIPT_ENUM_FOLDER_DESTINATION' => app_path('../resources/ts/types/Enum'),

    'SYNC_CASES' => true,
    'CASES_FOLDER_DESTINATION' => app_path('../resources/ts/EnumCases'),

    'EXCEPTIONS' => [],
]

Possible Enum Features and Flags

Using Custom Method's Description

To avoid conflicts with existing data when changing Enum values in your database, some engineers prefer to use simple integers as their values and make use of functions to describe what the values represent, like the following:

enum MyEnum: int
{
    case FIRST_CASE = 1;
    case SECOND_CASE = 2;

    public function description(): string
    {
        return match($this) {
            self::FIRST_CASE => 'first case description',
            self::SECOND_CASE => 'second case description',
        };
    }

    public function someOtherFunction()...
}

If you want to sync the descriptions from an Enum's method instead of its cases' values, put the sync-using-method flag above the corresponding function like so:

enum MyEnum: int
{
    case FIRST_CASE = 1;
    case SECOND_CASE = 2;

    // @sync-enum-types: sync-using-method
    public function description(): string
    {
        return match($this) {
            self::FIRST_CASE => 'first case description',
            self::SECOND_CASE => 'second case description',
        };
    }

    public function someOtherFunction()...
}

Linking Other Enums

To maintain a single source of truth when using many Enums, sometimes it is necessary to define a case like this:

    case MY_CASE = MyOtherEnum::ITS_CASE->value;

This is automatically handled correctly and resolves the value of the other Enum, as long as its source file also resides in the same directory.

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.

sync-enum-types's People

Contributors

mudandstars avatar

Stargazers

 avatar Simon Meißner avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.