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