This repository was archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathProgressBar.php
196 lines (170 loc) · 5.04 KB
/
ProgressBar.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\ProgressBar;
use Zend\Session;
/**
* Zend\ProgressBar offers an interface for multiple environments.
*/
class ProgressBar
{
/**
* Min value
*
* @var float
*/
protected $min;
/**
* Max value
*
* @var float
*/
protected $max;
/**
* Current value
*
* @var float
*/
protected $current;
/**
* Start time of the progressbar, required for ETA
*
* @var int
*/
protected $startTime;
/**
* Current status text
*
* @var string
*/
protected $statusText = null;
/**
* Adapter for the output
*
* @var \Zend\ProgressBar\Adapter\AbstractAdapter
*/
protected $adapter;
/**
* Namespace for keeping the progressbar persistent
*
* @var string
*/
protected $persistenceNamespace = null;
/**
* Create a new progressbar backend.
*
* @param Adapter\AbstractAdapter $adapter
* @param float|int $min
* @param float|int $max
* @param string|null $persistenceNamespace
* @throws Exception\OutOfRangeException When $min is greater than $max
*/
public function __construct(Adapter\AbstractAdapter $adapter, $min = 0, $max = 100, $persistenceNamespace = null)
{
// Check min/max values and set them
if ($min > $max) {
throw new Exception\OutOfRangeException('$max must be greater than $min');
}
$this->min = (float) $min;
$this->max = (float) $max;
$this->current = (float) $min;
// See if we have to open a session namespace
if ($persistenceNamespace !== null) {
$this->persistenceNamespace = new Session\Container($persistenceNamespace);
}
// Set adapter
$this->adapter = $adapter;
// Track the start time
$this->startTime = time();
// See If a persistenceNamespace exists and handle accordingly
if ($this->persistenceNamespace !== null) {
if (isset($this->persistenceNamespace->isSet)) {
$this->startTime = $this->persistenceNamespace->startTime;
$this->current = $this->persistenceNamespace->current;
$this->statusText = $this->persistenceNamespace->statusText;
} else {
$this->persistenceNamespace->isSet = true;
$this->persistenceNamespace->startTime = $this->startTime;
$this->persistenceNamespace->current = $this->current;
$this->persistenceNamespace->statusText = $this->statusText;
}
} else {
$this->update();
}
}
/**
* Get the current adapter
*
* @return Adapter\AbstractAdapter
*/
public function getAdapter()
{
return $this->adapter;
}
/**
* Update the progressbar
*
* @param float $value
* @param string $text
* @return void
*/
public function update($value = null, $text = null)
{
// Update value if given
if ($value !== null) {
$this->current = min($this->max, max($this->min, $value));
}
// Update text if given
if ($text !== null) {
$this->statusText = $text;
}
// See if we have to update a namespace
if ($this->persistenceNamespace !== null) {
$this->persistenceNamespace->current = $this->current;
$this->persistenceNamespace->statusText = $this->statusText;
}
// Calculate percent
if ($this->min === $this->max) {
$percent = false;
} else {
$percent = (float) ($this->current - $this->min) / ($this->max - $this->min);
}
// Calculate ETA
$timeTaken = time() - $this->startTime;
if ($percent === .0 || $percent === false) {
$timeRemaining = null;
} else {
$timeRemaining = round(((1 / $percent) * $timeTaken) - $timeTaken);
}
// Poll the adapter
$this->adapter->notify($this->current, $this->max, $percent, $timeTaken, $timeRemaining, $this->statusText);
}
/**
* Update the progressbar to the next value
*
* @param int $diff
* @param string $text
* @return void
*/
public function next($diff = 1, $text = null)
{
$this->update(max($this->min, min($this->max, $this->current + $diff)), $text);
}
/**
* Call the adapters finish() behaviour
*
* @return void
*/
public function finish()
{
if ($this->persistenceNamespace !== null) {
unset($this->persistenceNamespace->isSet);
}
$this->adapter->finish();
}
}