hydreflab/laravel-make-me

可扩展的Laravel交互式Make命令

v1.0.0 2018-02-22 14:31 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:41:52 UTC


README

使用交互式make命令,让你的生活更轻松!

安装

composer require hydreflab/laravel-make-me

Laravel >= 5.5

该包使用Laravel自动发现功能,因此不需要注册服务提供者。

Laravel <= 5.4

config/app.php 中需要手动注册服务提供者。

'providers' => [
    // ...
    
    HydrefLab\Laravel\Make\MakeServiceProvider::class,
]

注意:运行此包需要PHP >= 7.0

使用方法

要使用交互式make,只需运行

php artisan make

之后,您将被询问想要生成哪种类型的类。然后,您将按照一系列问题来准备满足您需求的类。

交互式make集成了所有默认的Laravel生成器命令。

如果您想查看可用命令,只需运行

php artisan make --list

原因

Laravel的Artisan是一个伟大的工具。Laravel的生成器(make)命令也是伟大的工具。它们经常有大量的额外选项。然而,如果没有检查命令的代码或运行带有 --help 选项的命令,那么特定的命令可以做什么还是个谜。这就是为什么我创建了交互式make命令。祝您享受!

[编辑]: 我最近注意到有一个类似的 也可以添加交互式make。查看一下,也许它会更适合您。

预览

Preview

可扩展性

随着项目的进展,您经常会发现自己创建自己的生成器命令,自己的 make:something。如果这个新命令可以包含在交互式make中那就太棒了。嘿,这是可能的。

要将自定义(非默认)生成器命令添加到交互式make中

  • 命令必须实现 public collectInputForInteractiveMake() 方法,
  • collectInputForInteractiveMake 方法必须返回一个数组,
  • 命令必须扩展 Illuminate\Console\Command 类(由于它是类似make的命令,我建议扩展 Illuminate\Console\GeneratorCommand),
  • 命令名称必须包含 make: 前缀,例如 make:awesome

示例

<?php

class MyAwesomeMakeCommand extends \Illuminate\Console\Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'make:awesome';
    

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new awesome class';
    

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        // Generate something awesome
    }
    
    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [
            ['name', InputArgument::REQUIRED, 'Your awesome name'],
        ];
    }
    
    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [
            ['scale', 's', InputOption::VALUE_NONE, 'Awesomeness scale.'],
        ];
    }
    
    /**
     * Collect options for the interactive make command.
     * 
     * @return array
     */
    public function collectInputForInteractiveMake()
    {
        return [
            'name' => $this->ask('What is your name, awesome?'),
            '-s' => $this->ask('How awesome are you?', 10),
        ];
    }
}

就这样。 MyAwesomeMakeCommand 命令现在将包含在交互式make中,作为 awesome