-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc0a4e3
commit 5f2c217
Showing
3 changed files
with
54 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
} | ||
} |