[ZF-6295] Generic:
[zend.git] / library / Zend / View / Stream.php
blob55354057f1cc545faf74d30fa8f17c776599c735
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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
21 /**
22 * Stream wrapper to convert markup of mostly-PHP templates into PHP prior to
23 * include().
25 * Based in large part on the example at
26 * http://www.php.net/manual/en/function.stream-wrapper-register.php
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)
34 * @category Zend
35 * @package Zend_View
36 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
37 * @license http://framework.zend.com/license/new-bsd New BSD License
39 class Zend_View_Stream
41 /**
42 * Current stream position.
44 * @var int
46 protected $_pos = 0;
48 /**
49 * Data for streaming.
51 * @var string
53 protected $_data;
55 /**
56 * Stream stats.
58 * @var array
60 protected $_stat;
62 /**
63 * Opens the script file and converts markup.
65 public function stream_open($path, $mode, $options, &$opened_path)
67 // get the view script source
68 $path = str_replace('zend.view://', '', $path);
69 $this->_data = file_get_contents($path);
71 /**
72 * If reading the file failed, update our local stat store
73 * to reflect the real stat of the file, then return on failure
75 if ($this->_data === false) {
76 $this->_stat = stat($path);
77 return false;
80 /**
81 * Convert <?= ?> to long-form <?php echo ?> and <? ?> to <?php ?>
84 $this->_data = preg_replace('/\<\?\=/', "<?php echo ", $this->_data);
85 $this->_data = preg_replace('/<\?(?!xml|php)/s', '<?php ', $this->_data);
87 /**
88 * file_get_contents() won't update PHP's stat cache, so we grab a stat
89 * of the file to prevent additional reads should the script be
90 * requested again, which will make include() happy.
92 $this->_stat = stat($path);
94 return true;
97 /**
98 * Included so that __FILE__ returns the appropriate info
100 * @return array
102 public function url_stat()
104 return $this->_stat;
108 * Reads from the stream.
110 public function stream_read($count)
112 $ret = substr($this->_data, $this->_pos, $count);
113 $this->_pos += strlen($ret);
114 return $ret;
119 * Tells the current position in the stream.
121 public function stream_tell()
123 return $this->_pos;
128 * Tells if we are at the end of the stream.
130 public function stream_eof()
132 return $this->_pos >= strlen($this->_data);
137 * Stream statistics.
139 public function stream_stat()
141 return $this->_stat;
146 * Seek to a specific point in the stream.
148 public function stream_seek($offset, $whence)
150 switch ($whence) {
151 case SEEK_SET:
152 if ($offset < strlen($this->_data) && $offset >= 0) {
153 $this->_pos = $offset;
154 return true;
155 } else {
156 return false;
158 break;
160 case SEEK_CUR:
161 if ($offset >= 0) {
162 $this->_pos += $offset;
163 return true;
164 } else {
165 return false;
167 break;
169 case SEEK_END:
170 if (strlen($this->_data) + $offset >= 0) {
171 $this->_pos = strlen($this->_data) + $offset;
172 return true;
173 } else {
174 return false;
176 break;
178 default:
179 return false;