File indexing completed on 2024-12-22 05:36:57

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  * @see Zend_Json
0022  */
0023 // require_once 'Zend/Json.php';
0024 
0025 /**
0026  * @see Zend_ProgressBar_Adapter
0027  */
0028 // require_once 'Zend/ProgressBar/Adapter.php';
0029 
0030 /**
0031  * Zend_ProgressBar_Adapter_JsPull offers a simple method for updating a
0032  * progressbar in a browser.
0033  *
0034  * @category  Zend
0035  * @package   Zend_ProgressBar
0036  * @uses      Zend_ProgressBar_Adapter_Interface
0037  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0038  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0039  */
0040 class Zend_ProgressBar_Adapter_JsPull extends Zend_ProgressBar_Adapter
0041 {
0042     /**
0043      * Wether to exit after json data send or not
0044      *
0045      * @var boolean
0046      */
0047     protected $_exitAfterSend = true;
0048 
0049     /**
0050      * Set wether to exit after json data send or not
0051      *
0052      * @param  boolean $exitAfterSend
0053      * @return Zend_ProgressBar_Adapter_JsPull
0054      */
0055     public function setExitAfterSend($exitAfterSend)
0056     {
0057         $this->_exitAfterSend = $exitAfterSend;
0058     }
0059 
0060     /**
0061      * Defined by Zend_ProgressBar_Adapter_Interface
0062      *
0063      * @param  float   $current       Current progress value
0064      * @param  float   $max           Max progress value
0065      * @param  float   $percent       Current percent value
0066      * @param  integer $timeTaken     Taken time in seconds
0067      * @param  integer $timeRemaining Remaining time in seconds
0068      * @param  string  $text          Status text
0069      * @return void
0070      */
0071     public function notify($current, $max, $percent, $timeTaken, $timeRemaining, $text)
0072     {
0073         $arguments = array(
0074             'current'       => $current,
0075             'max'           => $max,
0076             'percent'       => ($percent * 100),
0077             'timeTaken'     => $timeTaken,
0078             'timeRemaining' => $timeRemaining,
0079             'text'          => $text,
0080             'finished'      => false
0081         );
0082 
0083         $data = Zend_Json::encode($arguments);
0084 
0085         // Output the data
0086         $this->_outputData($data);
0087     }
0088 
0089     /**
0090      * Defined by Zend_ProgressBar_Adapter_Interface
0091      *
0092      * @return void
0093      */
0094     public function finish()
0095     {
0096         $data = Zend_Json::encode(array('finished' => true));
0097 
0098         $this->_outputData($data);
0099     }
0100 
0101     /**
0102      * Outputs given data the user agent.
0103      *
0104      * This split-off is required for unit-testing.
0105      *
0106      * @param  string $data
0107      * @return void
0108      */
0109     protected function _outputData($data)
0110     {
0111         echo $data;
0112 
0113         if ($this->_exitAfterSend) {
0114             exit;
0115         }
0116     }
0117 }