JsPull.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * @see Zend_Json
  21. */
  22. #require_once 'Zend/Json.php';
  23. /**
  24. * @see Zend_ProgressBar_Adapter
  25. */
  26. #require_once 'Zend/ProgressBar/Adapter.php';
  27. /**
  28. * Zend_ProgressBar_Adapter_JsPull offers a simple method for updating a
  29. * progressbar in a browser.
  30. *
  31. * @category Zend
  32. * @package Zend_ProgressBar
  33. * @uses Zend_ProgressBar_Adapter_Interface
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_ProgressBar_Adapter_JsPull extends Zend_ProgressBar_Adapter
  38. {
  39. /**
  40. * Wether to exit after json data send or not
  41. *
  42. * @var boolean
  43. */
  44. protected $_exitAfterSend = true;
  45. /**
  46. * Set wether to exit after json data send or not
  47. *
  48. * @param boolean $exitAfterSend
  49. * @return Zend_ProgressBar_Adapter_JsPull
  50. */
  51. public function setExitAfterSend($exitAfterSend)
  52. {
  53. $this->_exitAfterSend = $exitAfterSend;
  54. }
  55. /**
  56. * Defined by Zend_ProgressBar_Adapter_Interface
  57. *
  58. * @param float $current Current progress value
  59. * @param float $max Max progress value
  60. * @param float $percent Current percent value
  61. * @param integer $timeTaken Taken time in seconds
  62. * @param integer $timeRemaining Remaining time in seconds
  63. * @param string $text Status text
  64. * @return void
  65. */
  66. public function notify($current, $max, $percent, $timeTaken, $timeRemaining, $text)
  67. {
  68. $arguments = array(
  69. 'current' => $current,
  70. 'max' => $max,
  71. 'percent' => ($percent * 100),
  72. 'timeTaken' => $timeTaken,
  73. 'timeRemaining' => $timeRemaining,
  74. 'text' => $text,
  75. 'finished' => false
  76. );
  77. $data = Zend_Json::encode($arguments);
  78. // Output the data
  79. $this->_outputData($data);
  80. }
  81. /**
  82. * Defined by Zend_ProgressBar_Adapter_Interface
  83. *
  84. * @return void
  85. */
  86. public function finish()
  87. {
  88. $data = Zend_Json::encode(array('finished' => true));
  89. $this->_outputData($data);
  90. }
  91. /**
  92. * Outputs given data the user agent.
  93. *
  94. * This split-off is required for unit-testing.
  95. *
  96. * @param string $data
  97. * @return void
  98. */
  99. protected function _outputData($data)
  100. {
  101. echo $data;
  102. if ($this->_exitAfterSend) {
  103. exit;
  104. }
  105. }
  106. }