/libraries/koowa/template/stream.php

https://bitbucket.org/nlabyt/bcf-ball-4eb2 · PHP · 227 lines · 87 code · 28 blank · 112 comment · 9 complexity · 0fbf67ce66769abfd6e7396be56b3025 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: stream.php 4266 2011-10-08 23:57:41Z johanjanssens $
  4. * @category Koowa
  5. * @package Koowa_Template
  6. * @copyright Copyright (C) 2007 - 2010 Johan Janssens. All rights reserved.
  7. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
  8. * @link http://www.nooku.org
  9. */
  10. /**
  11. * Abstract stream wrapper to convert markup of mostly-PHP templates into PHP prior to include().
  12. *
  13. * Based in large part on the example at
  14. * http://www.php.net/manual/en/function.stream-wrapper-register.php
  15. *
  16. * @author Johan Janssens <johan@nooku.org>
  17. * @category Koowa
  18. * @package Koowa_Template
  19. */
  20. class KTemplateStream
  21. {
  22. /**
  23. * Current stream position.
  24. *
  25. * @var int
  26. */
  27. private $_pos = 0;
  28. /**
  29. * Template data
  30. *
  31. * @var string
  32. */
  33. private $_data;
  34. /**
  35. * Stream stats.
  36. *
  37. * @var array
  38. */
  39. private $_stat;
  40. /**
  41. * Template path
  42. *
  43. * @var string
  44. */
  45. private $_path;
  46. /**
  47. * Register the stream wrapper
  48. *
  49. * Function prevents from registering the wrapper twice
  50. */
  51. public static function register()
  52. {
  53. if (!in_array('tmpl', stream_get_wrappers())) {
  54. stream_wrapper_register('tmpl', __CLASS__);
  55. }
  56. }
  57. /**
  58. * Opens the template file and converts markup.
  59. *
  60. * This function filters the data from the stream by pushing it through the template's
  61. * read filter chain. The template object to use for filtering is the top node on the
  62. * template stack
  63. *
  64. * @param string The stream path
  65. * @return boolean
  66. */
  67. public function stream_open($path)
  68. {
  69. //Get the view script source
  70. $identifier = str_replace('tmpl://', '', $path);
  71. //Get the template object from the template stack and parse it
  72. $template = KService::get($identifier)->top();
  73. //Get the template path
  74. $this->_path = $template->getPath();
  75. //Get the template data
  76. $this->_data = $template->parse();
  77. // file_get_contents() won't update PHP's stat cache, so performing
  78. // another stat() on it will hit the filesystem again. Since the file
  79. // has been successfully read, avoid this and just fake the stat
  80. // so include() is happy.
  81. $this->_stat = array('mode' => 0100777, 'size' => strlen($this->_data));
  82. return true;
  83. }
  84. /**
  85. * Reads from the stream.
  86. *
  87. * @return string
  88. */
  89. public function stream_read($count)
  90. {
  91. $ret = substr($this->_data, $this->_pos, $count);
  92. $this->_pos += strlen($ret);
  93. return $ret;
  94. }
  95. /**
  96. * Tells the current position in the stream.
  97. *
  98. * @return int
  99. */
  100. public function stream_tell()
  101. {
  102. return $this->_pos;
  103. }
  104. /**
  105. * Tells if we are at the end of the stream.
  106. *
  107. * @return bool
  108. */
  109. public function stream_eof()
  110. {
  111. return $this->_pos >= strlen($this->_data);
  112. }
  113. /**
  114. * Stream statistics.
  115. *
  116. * @return array
  117. */
  118. public function stream_stat()
  119. {
  120. return $this->_stat;
  121. }
  122. /**
  123. * Flushes the output
  124. *
  125. * @return boolean
  126. */
  127. public function stream_flush()
  128. {
  129. return false;
  130. }
  131. /**
  132. * Close the stream
  133. *
  134. * @return void
  135. */
  136. public function stream_close()
  137. {
  138. }
  139. /**
  140. * Signal that stream_select is not supported by returning false
  141. *
  142. * @param int Can be STREAM_CAST_FOR_SELECT or STREAM_CAST_AS_STREAM
  143. * @return bool Always returns false as there is nounderlaying resource to return.
  144. */
  145. public function stream_cast($cast_as)
  146. {
  147. return false;
  148. }
  149. /**
  150. * Seek to a specific point in the stream.
  151. *
  152. * @return bool
  153. */
  154. public function stream_seek($offset, $whence)
  155. {
  156. switch ($whence)
  157. {
  158. case SEEK_SET:
  159. if ($offset < strlen($this->_data) && $offset >= 0) {
  160. $this->_pos = $offset;
  161. return true;
  162. }
  163. else return false;
  164. break;
  165. case SEEK_CUR:
  166. if ($offset >= 0)
  167. {
  168. $this->_pos += $offset;
  169. return true;
  170. }
  171. else return false;
  172. break;
  173. case SEEK_END:
  174. if (strlen($this->_data) + $offset >= 0)
  175. {
  176. $this->_pos = strlen($this->_data) + $offset;
  177. return true;
  178. }
  179. else return false;
  180. break;
  181. default:
  182. return false;
  183. }
  184. }
  185. /**
  186. * Url statistics.
  187. *
  188. * This method is called in response to all stat() related functions on the stream
  189. *
  190. * @param string The file path or URL to stat
  191. * @param int Holds additional flags set by the streams API
  192. *
  193. * @return array
  194. */
  195. public function url_stat($path, $flags)
  196. {
  197. return $this->_stat;
  198. }
  199. }