christophrumpel/laravel-command-file-picker

This package lets a user pick a file or class from a list during a Laravel command.

v1.2.0 2020-12-09 15:35 UTC

README

Latest Version on Packagist Build Status Quality Score Total Downloads

This package lets you show a list of files or classes to choose from during a Laravel command.

Screenshot of the command Screenshot of the command Screenshot of the command

Reasons You Might Need This Package

While using Laravel commands, you may need to run them on specific files or classes. Let's say you want to run some actions on one of your Laravel models. In the past, you may have passed a class or file as an argument to the command. This works, but it is quite cumbersome and leads to typing mistakes.

This package will make it easy for the user by prompting a list of files or classes. (like in the screenshot above) The user can then easily select one, and you can use it inside your command.

Installation

You can install the package via composer:

composer require christophrumpel/laravel-command-file-picker

Usage

Load Classes

To show the user a list of classes, you need to use the package's trait called PicksClasses. Just add it to one of your Laravel commands. (Be aware that you have to create your Laravel commands yourself.)

<?php

use Illuminate\Console\Command;


class MyLaravelCommand extends Command
{
  use PicksClasses;
  
  //...

}

After that you are able to show the user a list of files to choose from by calling the new askToPickClasses method:

$modelClass = $this->askToPickClasses(base_path('app/Models'));

This will show the user all the classes and give you back a collection. This collection can contain the one chosen model or all of them if you have use the All option. If you just need Laravel models, you can also load them like:

$models = $this->askToPickModels();

It will automatically look for models under your app/Models path.

Load Files

To show the user a list of files, you need to use the package's trait called PicksFiles. Just add it to one of your Laravel commands.

<?php

use Illuminate\Console\Command;


class MyLaravelCommand extends Command
{
  use PicksFiles;
  
  //...

}

After that you are able to show the user a list of files to choose from by calling the new askToPickFiles method. As a parameter you provide a directory path.

$files = $this->askToPickFiles(base_path('app/Models'));

This will show the user all the files and give you back a collection. This collection can contain the chosen file or all of them if you haven chosen the All option.

Filter Loaded Files

Sometimes you do not want to show all found files or classes to the user. This is when you can use a filter callable to define what results to leave out. Here is an example.

$files = $this->askToPickFiles(base_path('app/Models'), function(array $fileInfo) {
    return rand(0,1);
});

The second param is the filter callable which gets passed to Laravel's collection filter method. It lets you loop over all files and define what to keep in the files list and what to leave out. In our example, the decision will be random. Take note that the $fileInfo variable will be an array with some details about the current file like path. In case of a model or class it will contain the class name and file path.

The filtered result will then be shown to user during the Laravel command.

Remove "All" option

If you want to remove the All option when showing a list, you can do so with an extra parameter:

$files = $this->askToPickFiles(base_path('app/Models'), $filterCallable, $showAllOption);

Testing

composer test

Changelog

Please see CHANGELOG for more information about what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

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

Credits

License

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

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.