|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Classes\Update; |
|
4
|
|
|
|
|
5
|
|
|
class ProgressBar |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
private static $finished = 0; |
|
9
|
|
|
private static $progressBar; |
|
10
|
|
|
|
|
11
|
|
|
private $max = 0; |
|
12
|
|
|
|
|
13
|
|
|
private $progress = 0; |
|
14
|
|
|
|
|
15
|
|
|
private $progresslength = 0; |
|
16
|
|
|
|
|
17
|
|
|
private function __construct (){ } |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @return \Classes\Update\ProgressBar |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function getInstance () |
|
23
|
|
|
{ |
|
24
|
|
|
if ( is_null ( self::$progressBar ) ) |
|
25
|
|
|
{ |
|
26
|
|
|
self::$progressBar = new ProgressBar(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return self::$progressBar; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function setMaxByte ( $bytesMax ) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->max = $bytesMax; |
|
35
|
|
|
|
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function setProgress ( $progress ) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->progress = $progress; |
|
42
|
|
|
$this->calcule (); |
|
43
|
|
|
|
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function getProgress () |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->progresslength; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function calcule () |
|
53
|
|
|
{ |
|
54
|
|
|
if ( $this->progress > 0 ) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->progresslength = round( $this->progress * 100 / $this->max ); |
|
|
|
|
|
|
57
|
|
|
//$this->progresslength = (int) ( ( $this->progress / $this->max ) * 100 ); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function finish () |
|
62
|
|
|
{ |
|
63
|
|
|
if ( $this->getProgress () >= 99 && !self::$finished) |
|
64
|
|
|
{ |
|
65
|
|
|
echo "\n\033[1;32mDone!\033[0m\n"; |
|
66
|
|
|
self::$finished = 1; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function render () |
|
72
|
|
|
{ |
|
73
|
|
|
if ( $this->progress > 0 && !self::$finished) |
|
74
|
|
|
{ |
|
75
|
|
|
if ( ! isset( $this->max ) ) |
|
76
|
|
|
{ |
|
77
|
|
|
printf ( "\rUnknown filesize.. %2d kb done.." , $this->progress / 1024 ); |
|
78
|
|
|
} else |
|
79
|
|
|
{ |
|
80
|
|
|
$length = $this->getProgress (); |
|
81
|
|
|
printf ( "\r[%-100s] %d%%" , str_repeat ( "=" , $length ) |
|
82
|
|
|
. ">" , $length ); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function clear () |
|
90
|
|
|
{ |
|
91
|
|
|
$this->max = 0; |
|
92
|
|
|
$this->progress = 0; |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
} |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.