philiprehberger/laravel-make-service 问题修复 & 功能扩展

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

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

philiprehberger/laravel-make-service

Composer 安装命令:

composer require philiprehberger/laravel-make-service

包简介

Artisan generator commands for services, DTOs, enums, actions, value objects, and interfaces

README 文档

README

Tests Latest Version on Packagist Last updated

Artisan generator commands for services, DTOs, enums, actions, value objects, and interfaces.

Requirements

  • PHP 8.2+
  • Laravel 11 or 12

Installation

composer require philiprehberger/laravel-make-service

The package is auto-discovered by Laravel. No manual provider registration is needed.

Publishing Stubs

Publish the stubs to customize the generated output:

php artisan vendor:publish --tag=make-service-stubs

Stubs are published to stubs/make-service/ in your application root. Once published, the commands use your local stubs instead of the package defaults.

Usage

All commands support the following shared flags:

Flag Description
--test / -t Also generate a PHPUnit test file in tests/Unit/
--force / -f Overwrite the file if it already exists

make:service

Generates a service class in app/Services/.

php artisan make:service UserService

Generated file: app/Services/UserService.php

<?php

declare(strict_types=1);

namespace App\Services;

class UserService
{
    public function __construct()
    {
        //
    }
}

With model injection (--model)

php artisan make:service OrderService --model=Order

Generated file: app/Services/OrderService.php

<?php

declare(strict_types=1);

namespace App\Services;

use App\Models\Order;

class OrderService
{
    public function __construct(
        protected Order $order,
    ) {
        //
    }
}

With test (--test)

php artisan make:service PaymentService --test

Generates both app/Services/PaymentService.php and tests/Unit/Services/PaymentServiceTest.php.

Subdirectories

php artisan make:service Auth/LoginService

Generates app/Services/Auth/LoginService.php with namespace App\Services\Auth.

make:dto

Generates a readonly Data Transfer Object in app/DTOs/.

php artisan make:dto CreateUserDto

Generated file: app/DTOs/CreateUserDto.php

<?php

declare(strict_types=1);

namespace App\DTOs;

readonly class CreateUserDto
{
    public function __construct(
        //
    ) {}
}

Add constructor-promoted properties to define the shape of your DTO:

readonly class CreateUserDto
{
    public function __construct(
        public string $name,
        public string $email,
        public string $password,
    ) {}
}

With test (--test)

php artisan make:dto CreateUserDto --test

Generates both app/DTOs/CreateUserDto.php and tests/Unit/DTOs/CreateUserDtoTest.php. The test asserts that the class is readonly and can be instantiated.

make:enum

Generates a string-backed enum in app/Enums/ with values(), labels(), and fromName() helper methods.

php artisan make:enum OrderStatus

Generated file: app/Enums/OrderStatus.php

<?php

declare(strict_types=1);

namespace App\Enums;

enum OrderStatus: string
{
    case Example = 'example';

    public static function values(): array
    {
        return array_column(self::cases(), 'value');
    }

    public static function labels(): array
    {
        return array_reduce(self::cases(), function (array $carry, self $case): array {
            $carry[$case->value] = ucwords(str_replace(['_', '-'], ' ', $case->value));
            return $carry;
        }, []);
    }

    public static function fromName(string $name): self
    {
        foreach (self::cases() as $case) {
            if (strtolower($case->name) === strtolower($name)) {
                return $case;
            }
        }
        throw new \ValueError('"' . $name . '" is not a valid name for enum "' . self::class . '"');
    }
}

Integer-backed enum (--int)

php artisan make:enum Priority --int

Generates an int-backed enum with the same helper methods.

With test (--test)

php artisan make:enum OrderStatus --test

Generates both app/Enums/OrderStatus.php and tests/Unit/Enums/OrderStatusTest.php. The test covers values(), labels(), fromName(), and the invalid-name exception.

make:action

Generates a single-action invokable class in app/Actions/. The __invoke() method delegates to execute(), allowing both direct invocation and explicit calling.

php artisan make:action CreateInvoiceAction

Generated file: app/Actions/CreateInvoiceAction.php

<?php

declare(strict_types=1);

namespace App\Actions;

class CreateInvoiceAction
{
    public function __invoke(mixed ...$args): mixed
    {
        return $this->execute(...$args);
    }

    public function execute(mixed ...$args): mixed
    {
        //
    }
}

Typical usage after implementing the action:

// Via dependency injection and invocation
$action = app(CreateInvoiceAction::class);
$invoice = $action($client, $lineItems);

// Or call execute directly
$invoice = $action->execute($client, $lineItems);

With test (--test)

php artisan make:action CreateInvoiceAction --test

Generates both app/Actions/CreateInvoiceAction.php and tests/Unit/Actions/CreateInvoiceActionTest.php. The test asserts invocability and that __invoke delegates to execute.

make:value

Generates an immutable readonly value object in app/ValueObjects/ with an equals() method.

php artisan make:value Money

Generated file: app/ValueObjects/Money.php

<?php

declare(strict_types=1);

namespace App\ValueObjects;

readonly class Money
{
    public function __construct(
        //
    ) {}

    public function equals(self $other): bool
    {
        return $this == $other;
    }
}

Add constructor-promoted properties to define the value:

readonly class Money
{
    public function __construct(
        public int $amount,
        public string $currency,
    ) {}

    public function equals(self $other): bool
    {
        return $this == $other;
    }
}

With test (--test)

php artisan make:value Money --test

Generates both app/ValueObjects/Money.php and tests/Unit/ValueObjects/MoneyTest.php. The test asserts the class is readonly and that equals() returns true for identical instances.

make:contract

Generates a PHP interface in app/Contracts/.

php artisan make:contract PaymentGateway

Generated file: app/Contracts/PaymentGateway.php

<?php

declare(strict_types=1);

namespace App\Contracts;

interface PaymentGateway
{
    //
}

Define your contract's public API and then bind it in a service provider:

$this->app->bind(PaymentGateway::class, StripeGateway::class);

With test (--test)

php artisan make:contract PaymentGateway --test

Generates both app/Contracts/PaymentGateway.php and tests/Unit/Contracts/PaymentGatewayTest.php. The test asserts the generated file is a proper PHP interface.

Customizing Stubs

After publishing stubs with php artisan vendor:publish --tag=make-service-stubs, edit any file in stubs/make-service/. The commands always prefer your published stubs over the package defaults.

Available stubs:

File Used by
stubs/make-service/service.stub make:service (no model)
stubs/make-service/service.model.stub make:service --model=
stubs/make-service/dto.stub make:dto
stubs/make-service/enum.stub make:enum (string-backed)
stubs/make-service/enum.int.stub make:enum --int
stubs/make-service/action.stub make:action
stubs/make-service/value.stub make:value
stubs/make-service/contract.stub make:contract
stubs/make-service/service.test.stub --test on make:service
stubs/make-service/dto.test.stub --test on make:dto
stubs/make-service/enum.test.stub --test on make:enum
stubs/make-service/action.test.stub --test on make:action
stubs/make-service/value.test.stub --test on make:value
stubs/make-service/contract.test.stub --test on make:contract

API

Command Output location Description
php artisan make:service {Name} app/Services/ Service class
php artisan make:dto {Name} app/DTOs/ Readonly DTO
php artisan make:enum {Name} app/Enums/ String-backed enum with helpers
php artisan make:action {Name} app/Actions/ Invokable action class
php artisan make:value {Name} app/ValueObjects/ Readonly value object
php artisan make:contract {Name} app/Contracts/ PHP interface

All commands accept --test / -t (generate a test file) and --force / -f (overwrite existing).

Development

composer install
vendor/bin/phpunit
vendor/bin/pint --test
vendor/bin/phpstan analyse

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

philiprehberger/laravel-make-service 适用场景与选型建议

philiprehberger/laravel-make-service 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 74 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「generator」 「service」 「laravel」 「make」 「dto」 「artisan」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 philiprehberger/laravel-make-service 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-09