A package for adding loading spinners to your Laravel Artisan commands

Overview

Laravel Command Spinner

Latest Version on Packagist Total Downloads PHP from Packagist GitHub license

Table of Contents

Overview

A Laravel package that allows you to add 47 different styles of loading spinners to your Artisan commands.

Installation

Requirements

The package has been developed and tested to work with the following minimum requirements:

  • PHP 8
  • Laravel 8

Install the Package

You can install the package via Composer:

composer require ashallendesign/laravel-command-spinner

Usage

Adding Loading Spinners to Commands

Using a loading spinner can be useful for when executing long-running commands, such as fetching data from an API.

To add a loading spinner to your command, first start by adding HasSpinner trait to your command. After that, you can then use the withSpinner() method like shown in the example below:

use AshAllenDesign\CommandSpinner\Traits\HasSpinner;

class YourCommand extends Command
{
    use HasSpinner;

    public function handle(): int
    {
        $this->withSpinner(function () {
            // Run your code here.
        });
    }
}

Adding Text to the Spinner

When you're displaying a loading spinner in your console command, you may wish to also display a message.

The example below shows how you can add some text to be displayed alongisde the loading spinner:

use AshAllenDesign\CommandSpinner\Traits\HasSpinner;

class YourCommand extends Command
{
    use HasSpinner;

    public function handle(): int
    {
        $this->withSpinner(function () {
            // Run your code here.
        }, 'Fetching data from API...');
    }
}

Customising the Spinner Type

The package comes with many types of loading spinners out of the box that you can use. To view all of the different possible loading spinners, check out the SpinnerType class.

The example below shows how you can choose a different spinner type:

use AshAllenDesign\CommandSpinner\Classes\SpinnerType;
use AshAllenDesign\CommandSpinner\Traits\HasSpinner;

class YourCommand extends Command
{
    use HasSpinner;

    public function handle(): int
    {
        $this->withSpinner(function () {
            // Run your code here.
        }, 'Fetching data from API...', SpinnerType::BLOCK_VARIANT_1);
    }
}

Examples

The below example shows you can use the PHP 8 named parameters to add a spinner to your command:

use AshAllenDesign\CommandSpinner\Classes\SpinnerType;
use AshAllenDesign\CommandSpinner\Traits\HasSpinner;

class YourCommand extends Command
{
    use HasSpinner;

    public function handle(): int
    {
        $this->withSpinner(
            closure: function () {
                $this->fetchDataFromAPI();
            },
            outputText: 'Fetching data from API...',
            spinnerType: SpinnerType::BLOCK_VARIANT_1
        );
    }
}

In the below example, let's imagine that we have a class that calculates something for us and returns it. So, we'll display a loading spinner while it's calculating and then output the result when it's returned.

use AshAllenDesign\CommandSpinner\Traits\HasSpinner;

class YourCommand extends Command
{
    use HasSpinner;

    public function handle(): int
    {
        $result = $this->withSpinner(function () {
            return (new Calculator)->calculate();
        });
        
        $this->line($result);
    }
}

Gotchas

Here's a list of a few gotchas that I'm currently aware of at the time of writing this:

  • This package's functionality is based on the spatie/fork package which is currently under active development. Therefore, it's possible that the functionality of this package may change if the fork package is updated before it's initial release.

  • Due to the fact that the package is based on the fork package, 2 child PHP processes are created for each spinner that is created. One of them handles the spinning animation, while the other executes the closure that is passed in. This means that because the closure is executed in a child process, it won't be able to directly change any data in the command class itself.

  • The withSpinner() method currently only allows for primitive types to be returned. Therefore, you can't currently return objects.

Security

If you find any security related issues, please contact me directly at [email protected] to report it.

Contribution

If you wish to make any changes or improvements to the package, feel free to make a pull request.

To contribute to this library, please use the following guidelines before submitting your pull request:

  • Write tests for any new functions that are added. If you are updating existing code, make sure that the existing tests pass and write more if needed.
  • Follow PSR-2 coding standards.
  • Make all pull requests to the master branch.

License

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

You might also like...
Laravel Package for crud generation

Crud and API Generator Package Package which let's you automate tedious CRUD Operations. Requirements Laravel Version: = 8.0 PHP Version: = 7.3 Comp

a laravel package to create dynamically dashboard views in several templates ( in development)

Laravel Dashboarder A laravel package for generate admin dashboard dynamically based on Tabler template use livewire - alpinejs Installation Run the c

Package for using ReactJS with Laravel

react-laravel With react-laravel you'll be able to use ReactJS components right from your Blade views, with optional server-side rendering, and use th

18Laravel ReactJS Package to simplify sending data from Laravel back-end to front-end built to Facebook ReactJS.

Laravel ReactJS This is a package that we wrote to use on our Laravel applications that use React from Facebook. Our goal is deal with the SEO problem

A Laravel package which helps you automate creation of files.
A Laravel package which helps you automate creation of files.

Laravel file generator This is a Laravel package which helps you automate creation of files. High Res Link Benefits If you create a type of file frequ

A starter package for Laravel 5 mirroring L5 structure

Starter package for Laravel 5 Installation Create packages/yourname folder and cd into it. Clone this repository: git clone git://github.com/andyjesso

My first Laravel package (not)
My first Laravel package (not)

This an example package This repo can be used to scaffold a Laravel package. Follow these steps to get started: Press the "Use template" button at the

A super simple crud package for laravel
A super simple crud package for laravel

A super simple crud package for laravel

:computer: :octocat: A hackathon/MVP boilerplate for laravel web applications. Start your hackathons without hassle.
:computer: :octocat: A hackathon/MVP boilerplate for laravel web applications. Start your hackathons without hassle.

Laravel Hackathon Starter - SUSUMU 進 If you have attended any hackathons in the past, then you know how much time it takes to get a project started: d

Owner
Ash Allen
Laravel Web Developer Preston, United Kingdom
Ash Allen
This package provides an artisan command to generate a basic crud with Restful API support

NHRROB Crud Generator Package This package provides an artisan command to generate a basic crud composer install command: composer require nhrrob/crud

Nazmul Hasan Robin 22 Jun 24, 2022
Prepare your Laravel apps incredibly fast, with various commands, services, facades and boilerplates.

Grafite Builder Grafite has archived this project and no longer supports or develops the code. We recommend using only as a source of ideas for your o

Grafite Inc 997 Dec 22, 2022
A Web Artisan list of categorized OPEN SOURCE PROJECTS built with Laravel PHP Framework.

Laravel-Open-Source-Projects A Web Artisan list of categorized OPEN SOURCE PROJECTS built with Laravel PHP Framework. This repository includes a compr

Goodness Toluwanimi Kayode 833 Dec 26, 2022
Laravel CRUD Generator This Generator package provides various generators like CRUD, API, Controller, Model, Migration, View for your painless development of your applications.

Laravel CRUD Generator This Generator package provides various generators like CRUD, API, Controller, Model, Migration, View for your painless develop

AppzCoder 1.3k Jan 2, 2023
⚡️ This package provides a wonderful PHP skeleton to start building your next package idea.

This package provides a wonderful PHP Skeleton to start building your next package idea. Requires PHP 8.0+ ⚡️ Create your package using Composer: comp

Nuno Maduro 383 Dec 20, 2022
Until 2018, Backpack v3 used this Base package to offer admin authentication and a blank admin panel using AdminLTE. Backpack v4 no longer uses this package, they're now built-in - use Backpack/CRUD instead.

Note: This package is only used by Backpack v3. Starting with Backpack v4, everything this package does is included in Backpack/CRUD - one package to

Backpack for Laravel 845 Nov 29, 2022
A package for building Admin-Interfaces that help maintaining the data of your applications

A package for building Admin-Interfaces that help maintaining the data of your applications. It provides an intuitive interface and the tools needed to manage your project's Users, Models and free Forms for Pages, Settings etc.

null 808 Dec 31, 2022
A Laravel 5 package that switchs default Laravel scaffolding/boilerplate to AdminLTE template and Pratt Landing Page with Bootstrap 3.0

AdminLTE template Laravel package A Laravel package that switch default Laravel scaffolding / boilerplate to AdminLTE template with Bootstrap 3.0 and

Sergi Tur Badenas 1.8k Jan 3, 2023
Kick-start laravel prepared package for you!

Laravel Platform Install For development composer install npm install npm run dev ?? npm run prod php artisan migrate --seed For testing composer ins

Nejc 6 Sep 16, 2022