定制 dannyvilla/artisan-commands 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

dannyvilla/artisan-commands

Composer 安装命令:

composer require dannyvilla/artisan-commands

包简介

This package provides a set of artisan commands for Laravel

README 文档

README

This package provides a set of new artisan commands for Laravel

Installation

Use the package manager composer to install dannyvilla/artisan-commands

composer require dannyvilla/artisan-commands

Configuration

Publish the package config when you want to change where models are discovered or where TypeScript definitions are written:

php artisan vendor:publish --provider="Davinet\ArtisanCommand\ArtisanCommandServiceProvider"

This creates config/artisan-commands.php in your application. The relevant options are:

return [
    'models_path' => 'app/Models/',

    'transform' => [
        'type' => 'interface',
        'output' => 'resouces/js/types',
    ],
];
  • models_path tells the package where your Eloquent models live. Change it if your application keeps models outside app/Models.
  • transform.type controls the generated TypeScript declaration style. Use interface to generate export interface IUser { ... }, or type to generate export type IUser = { ... }.
  • transform.output controls the output folder, relative to the application base path. Change it to match your frontend structure, such as resources/js/types or resources/ts/generated.

Usage

Model annotate command

Add generated PHPDoc annotations to Eloquent models from database columns and typed relation methods.

php artisan model:annotate

Annotate one model by basename, fully qualified class, or configured model path:

php artisan model:annotate User
php artisan model:annotate App\\Models\\User

Preview changes without writing files:

php artisan model:annotate --dry-run

The command scans artisan-commands.models_path, inspects each model table, and writes a generated section inside the model class PHPDoc:

/**
 * @generated model annotations
 * @property int $id
 * @property string $email
 * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Post> $posts
 * @property-read \App\Models\Profile|null $profile
 * @end-generated model annotations
 */
class User extends Model
{
}

On later runs, only the generated section between the markers is replaced, so your handwritten class documentation is kept. Relation annotations are generated from public, parameterless model methods with an Eloquent relation return type, such as posts(): HasMany or profile(): HasOne.

TypeScript transform command

Transform PHP classes, models, resources, and enums marked with the TypeScriptTransform attribute into TypeScript definitions.

php artisan typescript:transform

Definitions are written to resouces/js/types/index.d.ts by default. Publish the config to change transform.type to interface or type, and transform.output to another output folder.

Only PHP types marked with #[TypeScriptTransform] are transformed. The command scans your application classes, reflects each marked type, and writes one TypeScript definition file. Related transformed types can also be included when they are referenced by model relations, resource responses, or typed properties.

The command supports four kinds of PHP types:

  • Plain PHP classes: public non-static properties are mapped to TypeScript properties.
  • Eloquent models: database columns are mapped first, including their database type and nullability. Relation methods such as profile(): HasOne are added as optional nested properties. If database inspection is not possible, the command falls back to @property annotations, then $fillable, then public properties.
  • JSON resources: the TypeScript shape is inferred from the array returned by toArray(), not from the resource class properties.
  • Enums: enum cases become a TypeScript const object and a value-union type.

Classes and models

use Davinet\ArtisanCommand\Attributes\TypeScriptTransform;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;

#[TypeScriptTransform]
class User extends Model
{
    public function profile(): HasOne
    {
        return $this->hasOne(Profile::class);
    }
}

For a model, the command inspects the model table and maps each column:

  • numeric columns become number
  • boolean columns become boolean
  • date, time, text, uuid, enum, and string columns become string
  • JSON and array columns become Record<string, any>
  • nullable database columns become optional TypeScript properties
  • non-null database columns, like most IDs, become required TypeScript properties

Typed Eloquent relation methods are included too. For example, profile(): HasOne adds profile?: IProfile, while posts(): HasMany adds posts?: IPost[].

If the database cannot be inspected, model properties are resolved in this fallback order:

  1. @property, @property-read, and @property-write annotations
  2. $fillable fields
  3. public non-static class properties

Resources

Resources are transformed from their toArray() response instead of their class properties.

use Davinet\ArtisanCommand\Attributes\TypeScriptTransform;
use Illuminate\Http\Resources\Json\JsonResource;

#[TypeScriptTransform]
class UserResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'role' => $this->role,
        ];
    }
}

For a resource, the command creates a sample backing model when it can infer one from the resource name, such as UserResource to User. It then calls toArray() and maps the returned keys and values to TypeScript. Nested arrays become inline object types, nested resources become their own transformed types, and resource collections become arrays of the collected resource type.

Enums

use Davinet\ArtisanCommand\Attributes\TypeScriptTransform;

#[TypeScriptTransform]
enum UserRole: string
{
    case ADMIN = 'admin';
    case USER = 'user';
}

This generates:

export const UserRoles = { ADMIN: 'admin', USER: 'user' } as const;

export type UserRole = (typeof UserRoles)[keyof typeof UserRoles];

For an enum, the exported const preserves the original PHP case names and backed values. The exported type is the union of the const values, so UserRole is equivalent to 'admin' | 'user'.

Repository command

Generate an empty repository file:

php artisan make:repository UserRepository

Generate a repository for a model:

php artisan make:repository UserRepository --model=User

You may also pass a fully qualified model class:

php artisan make:repository UserRepository --model=App\Models\User

Service command

Generate a service class:

php artisan make:service PayPalPaymentService

Generate a service class with a repository dependency:

php artisan make:service UserService UserRepository

View command

Generate an empty view:

php artisan make:view folder.subfolder.view

Generate a view with a layout:

php artisan make:view folder.subfolder.view --layout=app

Lang command

Generate a new locale file:

php artisan make:lang myFilename --locale=es

Generate a new JSON locale file:

php artisan make:lang --locale=es --json

Class command

Generate a class:

php artisan make:class App\Handlers\UserHandlers

You can use a dot (.) as separator:

php artisan make:class App.Handlers.UserHandlers --separator=.

Generate a trait:

php artisan make:class App\Traits\MyTrait --kind=trait

Generate an interface:

php artisan make:class App\Contracts\IClassable --kind=interface

File command

Generate a generic file:

php artisan make:file folder.subfolder1.subfolder2.filename --ext=php

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

dannyvilla/artisan-commands 适用场景与选型建议

dannyvilla/artisan-commands 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.07k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 09 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 dannyvilla/artisan-commands 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dannyvilla/artisan-commands 我们能提供哪些服务?
定制开发 / 二次开发

基于 dannyvilla/artisan-commands 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-09-13