Stream.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_View
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Stream wrapper to convert markup of mostly-PHP templates into PHP prior to
  22. * include().
  23. *
  24. * Based in large part on the example at
  25. * http://www.php.net/manual/en/function.stream-wrapper-register.php
  26. *
  27. * As well as the example provided at:
  28. * http://mikenaberezny.com/2006/02/19/symphony-templates-ruby-erb/
  29. * written by
  30. * Mike Naberezny (@link http://mikenaberezny.com)
  31. * Paul M. Jones (@link http://paul-m-jones.com)
  32. *
  33. * @category Zend
  34. * @package Zend_View
  35. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Zend_View_Stream
  39. {
  40. /**
  41. * Current stream position.
  42. *
  43. * @var int
  44. */
  45. protected $_pos = 0;
  46. /**
  47. * Data for streaming.
  48. *
  49. * @var string
  50. */
  51. protected $_data;
  52. /**
  53. * Stream stats.
  54. *
  55. * @var array
  56. */
  57. protected $_stat;
  58. /**
  59. * Opens the script file and converts markup.
  60. */
  61. public function stream_open($path, $mode, $options, &$opened_path)
  62. {
  63. // get the view script source
  64. $path = str_replace('zend.view://', '', $path);
  65. $this->_data = file_get_contents($path);
  66. /**
  67. * If reading the file failed, update our local stat store
  68. * to reflect the real stat of the file, then return on failure
  69. */
  70. if ($this->_data === false) {
  71. $this->_stat = stat($path);
  72. return false;
  73. }
  74. /**
  75. * Convert <?= ?> to long-form <?php echo ?> and <? ?> to <?php ?>
  76. *
  77. */
  78. $this->_data = preg_replace('/\<\?\=/', "<?php echo ", $this->_data);
  79. $this->_data = preg_replace('/<\?(?!xml|php)/s', '<?php ', $this->_data);
  80. /**
  81. * file_get_contents() won't update PHP's stat cache, so we grab a stat
  82. * of the file to prevent additional reads should the script be
  83. * requested again, which will make include() happy.
  84. */
  85. $this->_stat = stat($path);
  86. return true;
  87. }
  88. /**
  89. * Included so that __FILE__ returns the appropriate info
  90. *
  91. * @return array
  92. */
  93. public function url_stat()
  94. {
  95. return $this->_stat;
  96. }
  97. /**
  98. * Reads from the stream.
  99. */
  100. public function stream_read($count)
  101. {
  102. $ret = substr($this->_data, $this->_pos, $count);
  103. $this->_pos += strlen($ret);
  104. return $ret;
  105. }
  106. /**
  107. * Tells the current position in the stream.
  108. */
  109. public function stream_tell()
  110. {
  111. return $this->_pos;
  112. }
  113. /**
  114. * Tells if we are at the end of the stream.
  115. */
  116. public function stream_eof()
  117. {
  118. return $this->_pos >= strlen($this->_data);
  119. }
  120. /**
  121. * Stream statistics.
  122. */
  123. public function stream_stat()
  124. {
  125. return $this->_stat;
  126. }
  127. /**
  128. * Seek to a specific point in the stream.
  129. */
  130. public function stream_seek($offset, $whence)
  131. {
  132. switch ($whence) {
  133. case SEEK_SET:
  134. if ($offset < strlen($this->_data) && $offset >= 0) {
  135. $this->_pos = $offset;
  136. return true;
  137. } else {
  138. return false;
  139. }
  140. break;
  141. case SEEK_CUR:
  142. if ($offset >= 0) {
  143. $this->_pos += $offset;
  144. return true;
  145. } else {
  146. return false;
  147. }
  148. break;
  149. case SEEK_END:
  150. if (strlen($this->_data) + $offset >= 0) {
  151. $this->_pos = strlen($this->_data) + $offset;
  152. return true;
  153. } else {
  154. return false;
  155. }
  156. break;
  157. default:
  158. return false;
  159. }
  160. }
  161. }