File indexing completed on 2024-05-12 06:03:16

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_View
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * Stream wrapper to convert markup of mostly-PHP templates into PHP prior to
0024  * include().
0025  *
0026  * Based in large part on the example at
0027  * http://www.php.net/manual/en/function.stream-wrapper-register.php
0028  *
0029  * As well as the example provided at:
0030  *     http://mikenaberezny.com/2006/02/19/symphony-templates-ruby-erb/
0031  * written by
0032  *     Mike Naberezny (@link http://mikenaberezny.com)
0033  *     Paul M. Jones  (@link http://paul-m-jones.com)
0034  *
0035  * @category   Zend
0036  * @package    Zend_View
0037  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0038  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0039  */
0040 class Zend_View_Stream
0041 {
0042     /**
0043      * Current stream position.
0044      *
0045      * @var int
0046      */
0047     protected $_pos = 0;
0048 
0049     /**
0050      * Data for streaming.
0051      *
0052      * @var string
0053      */
0054     protected $_data;
0055 
0056     /**
0057      * Stream stats.
0058      *
0059      * @var array
0060      */
0061     protected $_stat;
0062 
0063     /**
0064      * Opens the script file and converts markup.
0065      */
0066     public function stream_open($path, $mode, $options, &$opened_path)
0067     {
0068         // get the view script source
0069         $path        = str_replace('zend.view://', '', $path);
0070         $this->_data = file_get_contents($path);
0071 
0072         /**
0073          * If reading the file failed, update our local stat store
0074          * to reflect the real stat of the file, then return on failure
0075          */
0076         if ($this->_data === false) {
0077             $this->_stat = stat($path);
0078             return false;
0079         }
0080 
0081         /**
0082          * Convert <?= ?> to long-form <?php echo ?> and <? ?> to <?php ?>
0083          *
0084          */
0085         $this->_data = preg_replace('/\<\?\=/',          "<?php echo ",  $this->_data);
0086         $this->_data = preg_replace('/<\?(?!xml|php)/s', '<?php ',       $this->_data);
0087 
0088         /**
0089          * file_get_contents() won't update PHP's stat cache, so we grab a stat
0090          * of the file to prevent additional reads should the script be
0091          * requested again, which will make include() happy.
0092          */
0093         $this->_stat = stat($path);
0094 
0095         return true;
0096     }
0097 
0098     /**
0099      * Included so that __FILE__ returns the appropriate info
0100      *
0101      * @return array
0102      */
0103     public function url_stat()
0104     {
0105         return $this->_stat;
0106     }
0107 
0108     /**
0109      * Reads from the stream.
0110      */
0111     public function stream_read($count)
0112     {
0113         $ret = substr($this->_data, $this->_pos, $count);
0114         $this->_pos += strlen($ret);
0115         return $ret;
0116     }
0117 
0118 
0119     /**
0120      * Tells the current position in the stream.
0121      */
0122     public function stream_tell()
0123     {
0124         return $this->_pos;
0125     }
0126 
0127 
0128     /**
0129      * Tells if we are at the end of the stream.
0130      */
0131     public function stream_eof()
0132     {
0133         return $this->_pos >= strlen($this->_data);
0134     }
0135 
0136 
0137     /**
0138      * Stream statistics.
0139      */
0140     public function stream_stat()
0141     {
0142         return $this->_stat;
0143     }
0144 
0145 
0146     /**
0147      * Seek to a specific point in the stream.
0148      */
0149     public function stream_seek($offset, $whence)
0150     {
0151         switch ($whence) {
0152             case SEEK_SET:
0153                 if ($offset < strlen($this->_data) && $offset >= 0) {
0154                 $this->_pos = $offset;
0155                     return true;
0156                 } else {
0157                     return false;
0158                 }
0159                 break;
0160 
0161             case SEEK_CUR:
0162                 if ($offset >= 0) {
0163                     $this->_pos += $offset;
0164                     return true;
0165                 } else {
0166                     return false;
0167                 }
0168                 break;
0169 
0170             case SEEK_END:
0171                 if (strlen($this->_data) + $offset >= 0) {
0172                     $this->_pos = strlen($this->_data) + $offset;
0173                     return true;
0174                 } else {
0175                     return false;
0176                 }
0177                 break;
0178 
0179             default:
0180                 return false;
0181         }
0182     }
0183 }