Skip to content

Commit

Permalink
modified more options settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulDey12 committed Feb 15, 2022
1 parent fc0a4e3 commit 5f2c217
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 43 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
}
],
"require": {
"php": "^7.3|^7.4|^8.0",
"illuminate/console": "^6.0|^7.0|^8.0",
"illuminate/support": "^6.0|^7.0|^8.0"
"php": "^7.3|^8.0",
"illuminate/console": "^6.0|^7.0|^8.0|^9.0",
"illuminate/support": "^6.0|^7.0|^8.0|"
},
"autoload": {
"psr-4": {
Expand Down
41 changes: 1 addition & 40 deletions src/LaravelConsoleSpinnerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,7 @@ public function boot()
__DIR__.'/../config/console-spinner.php' => config_path('console-spinner.php'),
], 'console-spinner-config');

Command::macro(
'spinner',
function (int $max = 0) {
return new Spinner($this->output, $max);
}
);

Command::macro(
'withSpinner',
function ($totalSteps, \Closure $callback, string $message = '', array $options = []) {
$spinner = $this->spinner(
is_iterable($totalSteps) ? count($totalSteps) : $totalSteps
);
$spinner->setMessage($message);

// Set more options
foreach ($options as $option => $value) {
$method = 'set'.ucfirst($option);
$spinner->{$method}($value);
}

$spinner->start();

if (is_iterable($totalSteps)) {
foreach ($totalSteps as $item) {
$callback($item, $spinner);

$spinner->advance();
}
} else {
$callback($spinner);
}

$spinner->finish();

if (is_iterable($spinner)) {
return $totalSteps;
}
}
);
Command::mixin(new SpinnerMixin());
}

public function register()
Expand Down
50 changes: 50 additions & 0 deletions src/SpinnerMixin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Rahul900Day\LaravelConsoleSpinner;

use Illuminate\Support\Arr;
use Illuminate\Support\Str;

class SpinnerMixin
{
protected function spinner()
{
return function (int $max = 0) {
return new Spinner($this->output, $max);
};
}

protected function withSpinner()
{
return function ($totalSteps, \Closure $callback, string $message = '', array $options = []) {
$spinner = $this->spinner(
is_iterable($totalSteps) ? count($totalSteps) : $totalSteps
);
$spinner->setMessage($message);

// Set more options
foreach ($options as $option => $args) {
$method = Str::startsWith($option, 'set') ? Str::camel($option) : 'set'.Str::studly($option);
call_user_func_array([$spinner, $method], Arr::wrap($args));
}

$spinner->start();

if (is_iterable($totalSteps)) {
foreach ($totalSteps as $item) {
$callback($item, $spinner);

$spinner->advance();
}
} else {
$callback($spinner);
}

$spinner->finish();

if (is_iterable($spinner)) {
return $totalSteps;
}
};
}
}

0 comments on commit 5f2c217

Please sign in to comment.