[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / ProgressBar.php
blobd55cd3e728c9b4af74857a7485c7afdf24788fa7
1 <?php
2 /**
3 * LICENSE
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.
13 * @category Zend
14 * @package Zend_ProgressBar
15 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
16 * @license http://framework.zend.com/license/new-bsd New BSD License
17 * @version $Id$
20 /**
21 * Zend_ProgressBar offers an interface for multiple enviroments.
23 * @category Zend
24 * @package Zend_ProgressBar
25 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
26 * @license http://framework.zend.com/license/new-bsd New BSD License
28 class Zend_ProgressBar
30 /**
31 * Min value
33 * @var float
35 protected $_min;
37 /**
38 * Max value
40 * @var float
42 protected $_max;
44 /**
45 * Current value
47 * @var float
49 protected $_current;
51 /**
52 * Start time of the progressbar, required for ETA
54 * @var integer
56 protected $_startTime;
58 /**
59 * Current status text
61 * @var string
63 protected $_statusText = null;
65 /**
66 * Adapter for the output
68 * @var Zend_ProgressBar_Adapter
70 protected $_adapter;
72 /**
73 * Namespace for keeping the progressbar persistent
75 * @var string
77 protected $_persistenceNamespace = null;
79 /**
80 * Create a new progressbar backend.
82 * @param Zend_ProgressBar_Adapter $adapter
83 * @param float $min
84 * @param float $max
85 * @param string $persistenceNamespace
86 * @throws Zend_ProgressBar_Exception When $min is greater than $max
88 public function __construct(Zend_ProgressBar_Adapter $adapter, $min = 0, $max = 100, $persistenceNamespace = null)
90 // Check min/max values and set them
91 if ($min > $max) {
92 require_once 'Zend/ProgressBar/Exception.php';
93 throw new Zend_ProgressBar_Exception('$max must be greater than $min');
96 $this->_min = (float) $min;
97 $this->_max = (float) $max;
98 $this->_current = (float) $min;
100 // See if we have to open a session namespace
101 if ($persistenceNamespace !== null) {
102 require_once 'Zend/Session/Namespace.php';
104 $this->_persistenceNamespace = new Zend_Session_Namespace($persistenceNamespace);
107 // Set adapter
108 $this->_adapter = $adapter;
110 // Track the start time
111 $this->_startTime = time();
113 // See If a persistenceNamespace exists and handle accordingly
114 if ($this->_persistenceNamespace !== null) {
115 if (isset($this->_persistenceNamespace->isSet)) {
116 $this->_startTime = $this->_persistenceNamespace->startTime;
117 $this->_current = $this->_persistenceNamespace->current;
118 $this->_statusText = $this->_persistenceNamespace->statusText;
119 } else {
120 $this->_persistenceNamespace->isSet = true;
121 $this->_persistenceNamespace->startTime = $this->_startTime;
122 $this->_persistenceNamespace->current = $this->_current;
123 $this->_persistenceNamespace->statusText = $this->_statusText;
125 } else {
126 $this->update();
131 * Get the current adapter
133 * @return Zend_ProgressBar_Adapter
135 public function getAdapter()
137 return $this->_adapter;
141 * Update the progressbar
143 * @param float $value
144 * @param string $text
145 * @return void
147 public function update($value = null, $text = null)
149 // Update value if given
150 if ($value !== null) {
151 $this->_current = min($this->_max, max($this->_min, $value));
154 // Update text if given
155 if ($text !== null) {
156 $this->_statusText = $text;
159 // See if we have to update a namespace
160 if ($this->_persistenceNamespace !== null) {
161 $this->_persistenceNamespace->current = $this->_current;
162 $this->_persistenceNamespace->statusText = $this->_statusText;
165 // Calculate percent
166 if ($this->_min === $this->_max) {
167 $percent = false;
168 } else {
169 $percent = (float) ($this->_current - $this->_min) / ($this->_max - $this->_min);
172 // Calculate ETA
173 $timeTaken = time() - $this->_startTime;
175 if ($percent === .0 || $percent === false) {
176 $timeRemaining = null;
177 } else {
178 $timeRemaining = round(((1 / $percent) * $timeTaken) - $timeTaken);
181 // Poll the adapter
182 $this->_adapter->notify($this->_current, $this->_max, $percent, $timeTaken, $timeRemaining, $this->_statusText);
186 * Update the progressbar to the next value
188 * @param string $text
189 * @return void
191 public function next($diff = 1, $text = null)
193 $this->update(max($this->_min, min($this->_max, $this->_current + $diff)), $text);
197 * Call the adapters finish() behaviour
199 * @return void
201 public function finish()
203 if ($this->_persistenceNamespace !== null) {
204 unset($this->_persistenceNamespace->isSet);
207 $this->_adapter->finish();