Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.ConsoleProgressOMeter.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 namespace Dcp;
8 
10 {
11  protected $starttime = 0;
12  protected $progress = 1;
13  protected $max = 1;
14  protected $interval = 1;
15  protected $timeInterval = 0;
16  protected $prevLineLen = 0;
17  protected $prevLineTime = 0;
18  protected $isInteractive = true;
19  protected $updateProcessTitle = false;
20  protected $prefix = '';
21 
22  public function __construct()
23  {
24  if (function_exists('posix_isatty')) {
25  $this->setInteractive(posix_isatty(STDOUT));
26  }
27 
28  return $this;
29  }
30  public function setPrefix($prefix)
31  {
32  $this->prefix = $prefix;
33  return $this;
34  }
35  public function setMax($max)
36  {
37  $max = (int)$max;
38  if ($max >= 0) {
39  $this->max = $max;
40  }
41  return $this;
42  }
43  public function setInterval($interval)
44  {
45  $interval = (int)$interval;
46  if ($interval > 0) {
47  $this->timeInterval = 0;
48  $this->interval = $interval;
49  }
50 
51  return $this;
52  }
53  public function setTimeInterval($interval)
54  {
55  $interval = (int)$interval;
56  if ($interval > 0) {
57  $this->timeInterval = $interval;
58  $this->interval = 0;
59  }
60 
61  return $this;
62  }
63  public function setInteractive($bool)
64  {
65  $this->isInteractive = ($bool === true);
66 
67  return $this;
68  }
69  public function setUpdateProcessTitle($prefix)
70  {
71  $this->updateProcessTitle = $prefix;
72 
73  return $this;
74  }
75  public function start($at = 0)
76  {
77  $at = (int)$at;
78  if ($at >= 0) {
79  $this->progress = $at;
80  } else {
81  $this->progress = 0;
82  }
83  $this->starttime = microtime(true);
84  return $this->progress(0);
85  }
86  public function finish()
87  {
88  if ($this->progress < $this->max) {
89  $this->progress($this->max);
90  }
91  if ($this->isInteractive) {
92  print "\n";
93  }
94  }
95  public function reset()
96  {
97  $this->start(0);
98  return $this;
99  }
100  protected function isTimeToUpdateProgress($p)
101  {
102  if ($p == $this->max) {
103  return true;
104  }
105  if ($this->interval > 0 && ($p % $this->interval == 0)) {
106  return true;
107  }
108  if ($this->timeInterval > 0 && ((microtime(true) - $this->prevLineTime) > $this->timeInterval)) {
109  return true;
110  }
111  return false;
112  }
113  public function progress($p)
114  {
115  $p = (int)$p;
116  if ($p <= 0) {
117  return $this;
118  }
119  if ($this->isTimeToUpdateProgress($p)) {
120  $ratio = (($this->max == 0) ? 0 : $p / $this->max);
121  $line = sprintf("%s%3d%% (%d/%d) [elapsed: %d sec. | remaining: %d sec. | ETA: %s]", ($this->prefix != '' ? $this->prefix . ' ' : '') , intval(100 * $ratio) , $p, $this->max, (microtime(true) - $this->starttime) , $this->eta($p, false) , $this->eta($p));
122  if ($this->isInteractive) {
123  print "\r" . $line;
124  if (strlen($line) < $this->prevLineLen) {
125  print str_repeat(" ", $this->prevLineLen - strlen($line));
126  }
127  } else {
128  print $line . "\n";
129  }
130  $this->updateProcessTitle($line);
131  $this->prevLineLen = strlen($line);
132  $this->prevLineTime = microtime(true);
133  }
134  $this->progress = $p;
135 
136  return $this;
137  }
138  protected function eta($done, $eta = true)
139  {
140  $now = microtime(true);
141  $rate = ($now - $this->starttime) / $done;
142  $remainingtime = ($this->max - $done) * $rate;
143  if ($eta) {
144  return date(DATE_RFC2822, $now + $remainingtime);
145  }
146  return $remainingtime;
147  }
148  protected function isProcessTitleEnabled()
149  {
150  if (!function_exists('cli_set_process_title')) {
151  return false;
152  }
153  return ($this->updateProcessTitle !== false && $this->updateProcessTitle != '');
154  }
155  private function updateProcessTitle($suffix = '')
156  {
157  if ($this->isProcessTitleEnabled()) {
158  $title = $this->updateProcessTitle;
159  if ($suffix != '') {
160  $title.= " - " . $suffix;
161  }
162  cli_set_process_title($title);
163  }
164  return $this;
165  }
166 }
$ratio
if(!is_string($famid)) $progress
print
Definition: checklist.php:49
$s start
← centre documentaire © anakeen