gigerit/laravel-morphmap-js-generator 问题修复 & 功能扩展

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

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

gigerit/laravel-morphmap-js-generator

最新稳定版本:v1.2.0

Composer 安装命令:

composer require gigerit/laravel-morphmap-js-generator

包简介

Generate frontend JavaScript/TypeScript constants from Laravel morph map configuration

README 文档

README

Generate frontend JavaScript/TypeScript constants from your Laravel application's morph map configuration. Keep your frontend morph types in sync with your Laravel models automatically.

Installation

You can install the package via composer:

composer require gigerit/laravel-morphmap-js-generator --dev

The package will automatically register itself with Laravel.

Usage

After installation, you can generate the JavaScript/TypeScript constants using the artisan command:

# Generate JavaScript file
php artisan morphmap:generate-js

# Generate TypeScript file
php artisan morphmap:generate-js --ts

# Specify custom output path
php artisan morphmap:generate-js --path=resources/js/constants

Example

If your Laravel application has this morph map configuration:

// App/Providers/AppServiceProvider.php

use Illuminate\Database\Eloquent\Relations\Relation;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Relation::morphMap([
            'tenant' => Tenant::class,
            'user' => User::class,
            1 => Client::class,
            2 => Contact::class,
        ]);
    }
}

The command will generate a JavaScript/TypeScript file with these constants:

// morphMap.ts or morphMap.js

export const MORPH_MAP = {
  TENANT: 'tenant',
  USER: 'user',
  CLIENT: 1,
  CONTACT: 2
} as const;

export const MORPH_MAP_MODELS = {
  [MORPH_MAP.TENANT]: 'Tenant',
  [MORPH_MAP.USER]: 'User',
  [MORPH_MAP.CLIENT]: 'Client',
  [MORPH_MAP.CONTACT]: 'Contact'
} as const;

export const getMorphMapModel = (morphMap: keyof typeof MORPH_MAP): string => {
  return MORPH_MAP_MODELS[morphMap] || 'Unknown';
};

export type MorphMapValue = typeof MORPH_MAP[keyof typeof MORPH_MAP];

Using in Your Frontend

import { MORPH_MAP, getMorphMapModel } from './morphMap';

// Use constants
if (type === MORPH_MAP.TENANT) {
  // Handle tenant case
}

// Get model name
const modelName = getMorphMapModel(MORPH_MAP.CLIENT); // Returns 'Client'

// TypeScript support
function handleMorphable(type: MorphMapValue) {
  // Type-safe handling of morph types
}

Features

  • 🔄 Automatically syncs with Laravel's morph map configuration
  • 📝 Generates JavaScript or TypeScript files
  • 🎯 Type-safe with TypeScript support
  • 🔍 Includes helper function for model name lookup
  • 🎨 Converts numeric keys to readable constant names
  • 📦 Zero configuration required
  • 💻 Command output shows current mappings

Command Options

Option Description Default
--ts Generate TypeScript instead of JavaScript false
--path Custom output path for generated file resources/js

Best Practices

  1. Version Control: Add the generated file to your version control system to track changes.
  2. Build Process: Include the generation command in your build process to ensure sync:
{
  "scripts": {
    "prepare": "php artisan morphmap:generate-js --ts"
  }
}
  1. Type Safety: Use TypeScript for better type checking and IDE support.

Security

If you discover any security related issues, please email security@example.com instead of using the issue tracker.

License

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固