2
0

MockupStream.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Text
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_ProgressBar
  25. * @subpackage UnitTests
  26. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_ProgressBar_Adapter_Console_MockupStream {
  30. private $position;
  31. private $test;
  32. public static $tests = array();
  33. function stream_open($path, $mode, $options, &$opened_path)
  34. {
  35. $url = parse_url($path);
  36. $this->test = $url["host"];
  37. $this->position = 0;
  38. self::$tests[$url["host"]] = '';
  39. return true;
  40. }
  41. function stream_read($count)
  42. {
  43. $ret = substr(self::$tests[$this->test], $this->position, $count);
  44. $this->position += strlen($ret);
  45. return $ret;
  46. }
  47. function stream_write($data)
  48. {
  49. $left = substr(self::$tests[$this->test], 0, $this->position);
  50. $right = substr(self::$tests[$this->test], $this->position + strlen($data));
  51. self::$tests[$this->test] = $left . $data . $right;
  52. $this->position += strlen($data);
  53. return strlen($data);
  54. }
  55. function stream_tell()
  56. {
  57. return $this->position;
  58. }
  59. function stream_eof()
  60. {
  61. return $this->position >= strlen(self::$tests[$this->test]);
  62. }
  63. function stream_seek($offset, $whence)
  64. {
  65. switch ($whence) {
  66. case SEEK_SET:
  67. if ($offset < strlen(self::$tests[$this->test]) && $offset >= 0) {
  68. $this->position = $offset;
  69. return true;
  70. } else {
  71. return false;
  72. }
  73. break;
  74. case SEEK_CUR:
  75. if ($offset >= 0) {
  76. $this->position += $offset;
  77. return true;
  78. } else {
  79. return false;
  80. }
  81. break;
  82. case SEEK_END:
  83. if (strlen(self::$tests[$this->test]) + $offset >= 0) {
  84. $this->position = strlen(self::$tests[$this->test]) + $offset;
  85. return true;
  86. } else {
  87. return false;
  88. }
  89. break;
  90. default:
  91. return false;
  92. }
  93. }
  94. public function __destruct() {
  95. unset(self::$tests[$this->test]);
  96. }
  97. }