定制 cyberpunkcodes/laravel-extended-commands 二次开发

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

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

cyberpunkcodes/laravel-extended-commands

Composer 安装命令:

composer require cyberpunkcodes/laravel-extended-commands

包简介

Extends the Laravel commands for developers

README 文档

README

Extends the Laravel artisan commands for developers.

With all of the helpful commands that Laravel comes out of the box with, there are a few that are missing. Hopefully one day Laravel will add them officially. Until then, let's fill the gap.

Installation

Install as a dev dependency:

composer require cyberpunkcodes/laravel-extended-commands --dev

Usage

This package is only usable via the command line. Refer to the commands list below for supported commands and how to use them.

Commands

The make commands will generate files in the corresponding directories within the app directory and the App namespace.

For example: Actions will be generated in the app/Actions directory and will be within the App\Actions namespace. Enums will be generated in app/Enums with the App\Enums namespace. Contracts will be generated in app/Contracts with the App\Contracts namespace. And so on.

Using double backslashes in the name will generate sub-directories. ie, running php artisan make:action User\\AddBlogComment will create app/Actions/User/AddBlogComment.php with the namespace of App\Actions\User.

All of the "make" commands follow this convention.

This does not force any naming conventions on you. What you enter is what you get. If you want your contracts to end with Contract then you must enter it as the name, ie: CreatesNewBlogPostContract. If you want Enums to end with "Enum", name it like BlogPostStatusEnum. Personally, I don't think it's necessary to add "Contract" or "Enum" or anything to the end of most of these, as they are already included in the path and namespace and Laravel doesn't seem to do it themselves. I do like to add "Service" to the end of my services, but to each their own :)

The following list of actions/commands are supported:

Clear Logs

Delete all of the *.log files found in the storage/logs directory:

php artisan clear:logs

Make Action

php artisan make:action SomeAction

Example: php artisan make:action AddBlogPost

By default, this will generate "invokable actions", which allows you to typhint the action in your method and use it's parameter as a function/method. I find this the best use-case for actions and Laravel Fortify uses them mostly like this as well, which is why it's the default.

public function store(Request $request, AddBlogPost $addBlogPostAction)
{
    $blogPost = $addBlogPostAction($request, $user);

    // ...
}

If you don't want invokable actions, you can pass a --handle option which creates a handle() method instead of the __invoke() magic method. This is ideal for when your actions are being used within Pipelines, which is why it automatically has the $request and $next params.

Make Contract

php artisan make:contract SomeContract

Example: php artisan make:contract BlogSystem

Make Enum

php artisan make:enum SomeEnum

Example: php artisan make:enum BlogPostStatus

Enums require PHP version 8.1 or higher.

Note: The generated enum is just an example of a basic active/inactive status using an integer. It will cast the status as 1 or 0 in/out of the database and includes a label method for converting it to a string representation (ie: for a dropdown in your view). It can be any valid enum type, integer is just provided as a starting example.

Make Service

php artisan make:service SomeService

Example: php artisan make:service BlogPostService

What is a service? Services are basically a fancy name for a specialized helper class. This isn't to be confused with a Service Provider. It allows you to get a lot of your code out of the controller, improving readability, and allows you to write more fine-grained tests without repeating code. If an Action or Job isn't applicable, you may want to use a Service.

Make Trait

php artisan make:trait SomeTrait

Example: php artisan make:trait BlogPost

Make View

php artisan make:view some-name

The name can be nested in sub-directories relative to the views directory.

php artisan make:view user/settings will generate /resources/views/user/settings.blade.php

The name will be converted to kebab case. So a name of someName will be generate some-name.blade.php. It will not convert the path (if passed), just the name. ie: somePath/someName will generate somePath/some-name.blade.php.

View Layouts

The make:view command does, in a way, support layouts. Since there are a few ways to use layouts in Laravel (ie: the old @extend method and new components), I thought it would be better to rely on stubs for your various needs. This way it not only handles layouts, but any number of alternate view file variations.

Passing the --stub option allows you to define the stub to use:

php artisan make:view some-name --stub=some-stub

The stub option supports nested paths, relative to the app/Console/stubs directory. So passing --stub=views/admin-layout will use the app/Console/stubs/views/admin-layout.stub stub file. This allows you to keep the view stubs separate from the other make command stubs that you will read about in the "Custom Stubs" section below.

Note: If you don't pass the --stub option, the default make-stub.stub file is used. To override it, you must create your own make-view.stub file in app/Console/stubs. If you want all of your view stubs organized separately in app/Console/stubs/views but it drives you mad that the default stub is outside of that in the stubs directory, you could create default.stub in the stubs/views directory. However, you would always have to pass --stub=default. If you don't, it uses the default make-view.stub, whether it be the one included in this package or an overrode one.

Custom Stubs

You can override the stubs and use your own for file generation. Refer to the stubs included in this project's source code.

It will first look in /app/Console/stubs/ to see if the stub exists. If not, it will use the default ones provided by this package. So simply copy the stub from this package to /app/Console/stubs/ and modify it to your liking.

Requirements

This package requires Laravel 8 or higher. It should work with whatever PHP version you are running that satisfies your Laravel version, which is most likely PHP v7.3 or higher.

Enums, however, require PHP 8.1 or higher.

Contributing

Contributions are welcome. Keep in mind, the goal of this package is to add additional commands that Laravel should come out of the box with, but for some reason, doesn't. I don't want to over complicate this and add all kinds of extra features.

Laravel has many "make" commands already... but why does it still not make traits, actions, or contracts? They are all used in their official packages.

cyberpunkcodes/laravel-extended-commands 适用场景与选型建议

cyberpunkcodes/laravel-extended-commands 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 64 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-04