ProgressBar.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * LICENSE
  4. *
  5. * This source file is subject to the new BSD license that is bundled
  6. * with this package in the file LICENSE.txt.
  7. * It is also available through the world-wide-web at this URL:
  8. * http://framework.zend.com/license/new-bsd
  9. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to license@zend.com so we can send you a copy immediately.
  12. *
  13. * @category Zend
  14. * @package Zend_ProgressBar
  15. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  16. * @license http://framework.zend.com/license/new-bsd New BSD License
  17. * @version $Id$
  18. */
  19. /**
  20. * Zend_ProgressBar offers an interface for multiple enviroments.
  21. *
  22. * @category Zend
  23. * @package Zend_ProgressBar
  24. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. class Zend_ProgressBar
  28. {
  29. /**
  30. * Min value
  31. *
  32. * @var float
  33. */
  34. protected $_min;
  35. /**
  36. * Max value
  37. *
  38. * @var float
  39. */
  40. protected $_max;
  41. /**
  42. * Current value
  43. *
  44. * @var float
  45. */
  46. protected $_current;
  47. /**
  48. * Start time of the progressbar, required for ETA
  49. *
  50. * @var integer
  51. */
  52. protected $_startTime;
  53. /**
  54. * Current status text
  55. *
  56. * @var string
  57. */
  58. protected $_statusText = null;
  59. /**
  60. * Adapter for the output
  61. *
  62. * @var Zend_ProgressBar_Adapter
  63. */
  64. protected $_adapter;
  65. /**
  66. * Namespace for keeping the progressbar persistent
  67. *
  68. * @var string
  69. */
  70. protected $_persistenceNamespace = null;
  71. /**
  72. * Create a new progressbar backend.
  73. *
  74. * @param Zend_ProgressBar_Adapter $adapter
  75. * @param float $min
  76. * @param float $max
  77. * @param string $persistenceNamespace
  78. * @throws Zend_ProgressBar_Exception When $min is greater than $max
  79. */
  80. public function __construct(Zend_ProgressBar_Adapter $adapter, $min = 0, $max = 100, $persistenceNamespace = null)
  81. {
  82. // Check min/max values and set them
  83. if ($min > $max) {
  84. #require_once 'Zend/ProgressBar/Exception.php';
  85. throw new Zend_ProgressBar_Exception('$max must be greater than $min');
  86. }
  87. $this->_min = (float) $min;
  88. $this->_max = (float) $max;
  89. $this->_current = (float) $min;
  90. // See if we have to open a session namespace
  91. if ($persistenceNamespace !== null) {
  92. #require_once 'Zend/Session/Namespace.php';
  93. $this->_persistenceNamespace = new Zend_Session_Namespace($persistenceNamespace);
  94. }
  95. // Set adapter
  96. $this->_adapter = $adapter;
  97. // Track the start time
  98. $this->_startTime = time();
  99. // See If a persistenceNamespace exists and handle accordingly
  100. if ($this->_persistenceNamespace !== null) {
  101. if (isset($this->_persistenceNamespace->isSet)) {
  102. $this->_startTime = $this->_persistenceNamespace->startTime;
  103. $this->_current = $this->_persistenceNamespace->current;
  104. $this->_statusText = $this->_persistenceNamespace->statusText;
  105. } else {
  106. $this->_persistenceNamespace->isSet = true;
  107. $this->_persistenceNamespace->startTime = $this->_startTime;
  108. $this->_persistenceNamespace->current = $this->_current;
  109. $this->_persistenceNamespace->statusText = $this->_statusText;
  110. }
  111. } else {
  112. $this->update();
  113. }
  114. }
  115. /**
  116. * Get the current adapter
  117. *
  118. * @return Zend_ProgressBar_Adapter
  119. */
  120. public function getAdapter()
  121. {
  122. return $this->_adapter;
  123. }
  124. /**
  125. * Update the progressbar
  126. *
  127. * @param float $value
  128. * @param string $text
  129. * @return void
  130. */
  131. public function update($value = null, $text = null)
  132. {
  133. // Update value if given
  134. if ($value !== null) {
  135. $this->_current = min($this->_max, max($this->_min, $value));
  136. }
  137. // Update text if given
  138. if ($text !== null) {
  139. $this->_statusText = $text;
  140. }
  141. // See if we have to update a namespace
  142. if ($this->_persistenceNamespace !== null) {
  143. $this->_persistenceNamespace->current = $this->_current;
  144. $this->_persistenceNamespace->statusText = $this->_statusText;
  145. }
  146. // Calculate percent
  147. if ($this->_min === $this->_max) {
  148. $percent = false;
  149. } else {
  150. $percent = (float) ($this->_current - $this->_min) / ($this->_max - $this->_min);
  151. }
  152. // Calculate ETA
  153. $timeTaken = time() - $this->_startTime;
  154. if ($percent === .0 || $percent === false) {
  155. $timeRemaining = null;
  156. } else {
  157. $timeRemaining = round(((1 / $percent) * $timeTaken) - $timeTaken);
  158. }
  159. // Poll the adapter
  160. $this->_adapter->notify($this->_current, $this->_max, $percent, $timeTaken, $timeRemaining, $this->_statusText);
  161. }
  162. /**
  163. * Update the progressbar to the next value
  164. *
  165. * @param string $text
  166. * @return void
  167. */
  168. public function next($diff = 1, $text = null)
  169. {
  170. $this->update(max($this->_min, min($this->_max, $this->_current + $diff)), $text);
  171. }
  172. /**
  173. * Call the adapters finish() behaviour
  174. *
  175. * @return void
  176. */
  177. public function finish()
  178. {
  179. if ($this->_persistenceNamespace !== null) {
  180. unset($this->_persistenceNamespace->isSet);
  181. }
  182. $this->_adapter->finish();
  183. }
  184. }