Stream.php 4.5 KB

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