-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExamplesProgressBar.php
97 lines (82 loc) · 3.3 KB
/
ExamplesProgressBar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* JBZoo Toolbox - Cli
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Cli
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/Cli
*/
declare(strict_types=1);
namespace DemoApp\Commands;
use JBZoo\Cli\CliCommand;
use JBZoo\Cli\Codes;
use JBZoo\Cli\ProgressBars\ProgressBar;
use Symfony\Component\Console\Input\InputOption;
/**
* Class ExamplesProgressBar
*/
class ExamplesProgressBar extends CliCommand
{
protected function configure(): void
{
$this
->setName('examples:progress-bar')
->setDescription('Examples of progress bar')
->addOption('exception', null, InputOption::VALUE_NONE, 'Throw exception')
->addOption('exception-list', null, InputOption::VALUE_NONE, 'Throw list of exceptions');
parent::configure();
}
/**
* @inheritDoc
*/
protected function executeAction(): int
{
//////////////////////////////////////////////////////////////////////// Just 3 steps
ProgressBar::run(2, function ($stepValue, $stepIndex, $currentStep) {
sleep(1);
return "Step info: \$stepValue={$stepValue}, \$stepIndex={$stepIndex}, \$currentStep={$currentStep}";
}, 'Number of steps');
//////////////////////////////////////////////////////////////////////// Assoc array. Step-by-step
$list = [
'key_1' => 'value_1',
'key_2' => 'value_2',
'key_3' => 'value_3'
];
ProgressBar::run($list, function ($stepValue, $stepIndex, $currentStep) {
return "Step info: \$stepValue={$stepValue}, \$stepIndex={$stepIndex}, \$currentStep={$currentStep}";
}, 'Assoc array');
//////////////////////////////////////////////////////////////////////// Exit from the cycle
ProgressBar::run(3, function ($stepValue, $stepIndex, $currentStep) {
if ($stepValue === 1) {
return ProgressBar::BREAK;
}
return "Step info: \$stepValue={$stepValue}, \$stepIndex={$stepIndex}, \$currentStep={$currentStep}";
}, 'Exit from the cycle');
//////////////////////////////////////////////////////////////////////// Exception
if ($this->getOptBool('exception')) {
ProgressBar::run(3, function ($stepValue) {
if ($stepValue === 1) {
throw new Exception("Exception #{$stepValue}");
}
return "\$stepValue={$stepValue}";
}, 'Exception handling', false);
}
//////////////////////////////////////////////////////////////////////// List of Exceptions
if ($this->getOptBool('exception-list')) {
ProgressBar::run(10, function ($stepValue) {
if ($stepValue % 3 === 0) {
throw new Exception("Exception #{$stepValue}");
}
return "\$stepValue={$stepValue}";
}, 'Handling list of exceptions at once', true);
}
// Default success exist code is "0". Max value is 255.
// See JBZoo\Cli\Codes class for more info
return Codes::OK;
}
}