File indexing completed on 2024-12-22 05:37:17
0001 <?php 0002 /** 0003 * LICENSE 0004 * 0005 * This source file is subject to the new BSD license that is bundled 0006 * with this package in the file LICENSE.txt. 0007 * It is also available through the world-wide-web at this URL: 0008 * http://framework.zend.com/license/new-bsd 0009 * If you did not receive a copy of the license and are unable to 0010 * obtain it through the world-wide-web, please send an email 0011 * to license@zend.com so we can send you a copy immediately. 0012 * 0013 * @category Zend 0014 * @package Zend_ProgressBar 0015 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0016 * @license http://framework.zend.com/license/new-bsd New BSD License 0017 * @version $Id$ 0018 */ 0019 0020 /** 0021 * Zend_ProgressBar offers an interface for multiple enviroments. 0022 * 0023 * @category Zend 0024 * @package Zend_ProgressBar 0025 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0026 * @license http://framework.zend.com/license/new-bsd New BSD License 0027 */ 0028 class Zend_ProgressBar 0029 { 0030 /** 0031 * Min value 0032 * 0033 * @var float 0034 */ 0035 protected $_min; 0036 0037 /** 0038 * Max value 0039 * 0040 * @var float 0041 */ 0042 protected $_max; 0043 0044 /** 0045 * Current value 0046 * 0047 * @var float 0048 */ 0049 protected $_current; 0050 0051 /** 0052 * Start time of the progressbar, required for ETA 0053 * 0054 * @var integer 0055 */ 0056 protected $_startTime; 0057 0058 /** 0059 * Current status text 0060 * 0061 * @var string 0062 */ 0063 protected $_statusText = null; 0064 0065 /** 0066 * Adapter for the output 0067 * 0068 * @var Zend_ProgressBar_Adapter 0069 */ 0070 protected $_adapter; 0071 0072 /** 0073 * Namespace for keeping the progressbar persistent 0074 * 0075 * @var string 0076 */ 0077 protected $_persistenceNamespace = null; 0078 0079 /** 0080 * Create a new progressbar backend. 0081 * 0082 * @param Zend_ProgressBar_Adapter $adapter 0083 * @param float $min 0084 * @param float $max 0085 * @param string $persistenceNamespace 0086 * @throws Zend_ProgressBar_Exception When $min is greater than $max 0087 */ 0088 public function __construct(Zend_ProgressBar_Adapter $adapter, $min = 0, $max = 100, $persistenceNamespace = null) 0089 { 0090 // Check min/max values and set them 0091 if ($min > $max) { 0092 // require_once 'Zend/ProgressBar/Exception.php'; 0093 throw new Zend_ProgressBar_Exception('$max must be greater than $min'); 0094 } 0095 0096 $this->_min = (float) $min; 0097 $this->_max = (float) $max; 0098 $this->_current = (float) $min; 0099 0100 // See if we have to open a session namespace 0101 if ($persistenceNamespace !== null) { 0102 // require_once 'Zend/Session/Namespace.php'; 0103 0104 $this->_persistenceNamespace = new Zend_Session_Namespace($persistenceNamespace); 0105 } 0106 0107 // Set adapter 0108 $this->_adapter = $adapter; 0109 0110 // Track the start time 0111 $this->_startTime = time(); 0112 0113 // See If a persistenceNamespace exists and handle accordingly 0114 if ($this->_persistenceNamespace !== null) { 0115 if (isset($this->_persistenceNamespace->isSet)) { 0116 $this->_startTime = $this->_persistenceNamespace->startTime; 0117 $this->_current = $this->_persistenceNamespace->current; 0118 $this->_statusText = $this->_persistenceNamespace->statusText; 0119 } else { 0120 $this->_persistenceNamespace->isSet = true; 0121 $this->_persistenceNamespace->startTime = $this->_startTime; 0122 $this->_persistenceNamespace->current = $this->_current; 0123 $this->_persistenceNamespace->statusText = $this->_statusText; 0124 } 0125 } else { 0126 $this->update(); 0127 } 0128 } 0129 0130 /** 0131 * Get the current adapter 0132 * 0133 * @return Zend_ProgressBar_Adapter 0134 */ 0135 public function getAdapter() 0136 { 0137 return $this->_adapter; 0138 } 0139 0140 /** 0141 * Update the progressbar 0142 * 0143 * @param float $value 0144 * @param string $text 0145 * @return void 0146 */ 0147 public function update($value = null, $text = null) 0148 { 0149 // Update value if given 0150 if ($value !== null) { 0151 $this->_current = min($this->_max, max($this->_min, $value)); 0152 } 0153 0154 // Update text if given 0155 if ($text !== null) { 0156 $this->_statusText = $text; 0157 } 0158 0159 // See if we have to update a namespace 0160 if ($this->_persistenceNamespace !== null) { 0161 $this->_persistenceNamespace->current = $this->_current; 0162 $this->_persistenceNamespace->statusText = $this->_statusText; 0163 } 0164 0165 // Calculate percent 0166 if ($this->_min === $this->_max) { 0167 $percent = false; 0168 } else { 0169 $percent = (float) ($this->_current - $this->_min) / ($this->_max - $this->_min); 0170 } 0171 0172 // Calculate ETA 0173 $timeTaken = time() - $this->_startTime; 0174 0175 if ($percent === .0 || $percent === false) { 0176 $timeRemaining = null; 0177 } else { 0178 $timeRemaining = round(((1 / $percent) * $timeTaken) - $timeTaken); 0179 } 0180 0181 // Poll the adapter 0182 $this->_adapter->notify($this->_current, $this->_max, $percent, $timeTaken, $timeRemaining, $this->_statusText); 0183 } 0184 0185 /** 0186 * Update the progressbar to the next value 0187 * 0188 * @param string $text 0189 * @return void 0190 */ 0191 public function next($diff = 1, $text = null) 0192 { 0193 $this->update(max($this->_min, min($this->_max, $this->_current + $diff)), $text); 0194 } 0195 0196 /** 0197 * Call the adapters finish() behaviour 0198 * 0199 * @return void 0200 */ 0201 public function finish() 0202 { 0203 if ($this->_persistenceNamespace !== null) { 0204 unset($this->_persistenceNamespace->isSet); 0205 } 0206 0207 $this->_adapter->finish(); 0208 } 0209 }