[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/pattemplate/patTemplate/Reader/ -> IT.php (source)

   1  <?PHP
   2  /**
   3   * patTemplate reader that reads HTML_Template_IT files
   4   *
   5   * $Id: IT.php 10381 2008-06-01 03:35:53Z pasamio $
   6   *
   7   * @package        patTemplate
   8   * @subpackage    Readers
   9   * @author        Stephan Schmidt <schst@php.net>
  10   */
  11  
  12  // Check to ensure this file is within the rest of the framework
  13  defined('JPATH_BASE') or die();
  14  
  15  /**
  16   * patTemplate reader that reads HTML_Template_IT files
  17   *
  18   * @package        patTemplate
  19   * @subpackage    Readers
  20   * @author        Stephan Schmidt <schst@php.net>
  21   */
  22  class patTemplate_Reader_IT extends patTemplate_Reader
  23  {
  24      /**
  25      * reader name
  26      * @access    private
  27      * @var        string
  28      */
  29      var    $_name    =    'IT';
  30  
  31      /**
  32      * files that have been used
  33      * @access    private
  34      * @var        array
  35      */
  36      var    $_files    =    array();
  37  
  38      /**
  39      * parse templates from string
  40      *
  41      * @access    private
  42      * @param    string        string to parse
  43      * @return    array        templates
  44      */
  45  	function parseString( $string )
  46      {
  47          /**
  48           * apply input filter before parsing
  49           */
  50          $string = $this->_tmpl->applyInputFilters( $string );
  51  
  52          $this->_inheritAtts    =    array();
  53          $this->_elStack        =    array();
  54          $this->_data        =    array( '' );
  55          $this->_tmplStack    =    array();
  56          $this->_depth        =    0;
  57          $this->_templates    =    array();
  58          $this->_path        =    array();
  59          $this->_processedData    =    '';
  60  
  61          $this->_defaultAtts    =    $this->_tmpl->getDefaultAttributes();
  62  
  63          if( !isset( $this->_defaultAtts['autoload'] ) )
  64              $this->_defaultAtts['autoload']    =    'on';
  65  
  66          /**
  67           * create a special root template
  68           */
  69          $attributes        = $this->_rootAtts;
  70          $attributes['name']    = '__global';
  71  
  72          $rootTemplate    = $this->_initTemplate( $attributes );
  73  
  74          array_push( $this->_tmplStack, $rootTemplate );
  75  
  76          /**
  77           *start parsing
  78           */
  79          $patNamespace    =    strtolower( $this->_tmpl->getNamespace() );
  80  
  81          $regexp    =    '/(<!-- (BEGIN|END) ([a-zA-Z]+) -->)/m';
  82  
  83          $tokens    =    preg_split( $regexp, $string, -1, PREG_SPLIT_DELIM_CAPTURE );
  84  
  85          /**
  86           * the first token is always character data
  87           * Though it could just be empty
  88           */
  89          if( $tokens[0] != '' )
  90              $this->_characterData( $tokens[0] );
  91  
  92          $cnt    =    count( $tokens );
  93          $i        =    1;
  94          // process all tokens
  95          while( $i < $cnt )
  96          {
  97              $fullTag    =    $tokens[$i++];
  98              $closing    =    strtoupper( $tokens[$i++] ) == 'END' ? true : false;
  99              $tmplName    =    $tokens[$i++];
 100              $namespace  =   $patNamespace;
 101              $tagname    =    'tmpl';
 102              $data        =    $tokens[$i++];
 103  
 104              /**
 105               * is it a closing tag?
 106               */
 107              if( $closing === true )
 108              {
 109                  $result    =    $this->_endElement( $namespace, $tagname );
 110                  if( patErrorManager::isError( $result ) )
 111                  {
 112                      return    $result;
 113                  }
 114                  $this->_characterData( $data );
 115                  continue;
 116              }
 117  
 118              $attributes    =    array( 'name' => $tmplName );
 119              $result     =    $this->_startElement( $namespace, $tagname, $attributes );
 120              if( patErrorManager::isError( $result ) )
 121              {
 122                  return    $result;
 123              }
 124  
 125              $this->_characterData( $data );
 126          }
 127  
 128          $rootTemplate = array_pop( $this->_tmplStack );
 129  
 130          $this->_closeTemplate( $rootTemplate, $this->_data[0] );
 131  
 132          /**
 133           * check for tags that are still open
 134           */
 135          if( $this->_depth > 0 )
 136          {
 137              $el    =    array_pop( $this->_elStack );
 138              return patErrorManager::raiseError(
 139                  PATTEMPLATE_READER_ERROR_NO_CLOSING_TAG,
 140                  $this->_createErrorMessage( "No closing tag for {$el['ns']}:{$el['name']} found" )
 141              );
 142          }
 143  
 144          return    $this->_templates;
 145      }
 146  
 147      /**
 148      * read templates from any input
 149      *
 150      * @final
 151      * @access    public
 152      * @param    string    file to parse
 153      * @return    array    templates
 154      */
 155  	function readTemplates( $input )
 156      {
 157          $this->_currentInput = $input;
 158          $fullPath    =    $this->_resolveFullPath( $input );
 159          if( patErrorManager::isError( $fullPath ) )
 160              return $fullPath;
 161          $content    =    $this->_getFileContents( $fullPath );
 162          if( patErrorManager::isError( $content ) )
 163              return $content;
 164  
 165          $templates    =    $this->parseString( $content );
 166  
 167          return    $templates;
 168      }
 169  
 170      /**
 171      * load template from any input
 172      *
 173      * If the a template is loaded, the content will not get
 174      * analyzed but the whole content is returned as a string.
 175      *
 176      * @abstract    must be implemented in the template readers
 177      * @param    mixed    input to load from.
 178      *                    This can be a string, a filename, a resource or whatever the derived class needs to read from
 179      * @return    string  template content
 180      */
 181  	function loadTemplate( $input )
 182      {
 183          $fullPath    =    $this->_resolveFullPath( $input );
 184          if( patErrorManager::isError( $fullPath ) )
 185              return $fullPath;
 186          return $this->_getFileContents( $fullPath );
 187      }
 188  
 189      /**
 190      * resolve path for a template
 191      *
 192      * @access    private
 193      * @param    string        filename
 194      * @return    string        full path
 195      */
 196  	function _resolveFullPath( $filename )
 197      {
 198          $baseDir  = $this->getTemplateRoot();
 199          $fullPath = $baseDir . '/' . $filename;
 200          return    $fullPath;
 201      }
 202  
 203      /**
 204      * get the contents of a file
 205      *
 206      * @access    private
 207      * @param    string        filename
 208      * @return    string        file contents
 209      */
 210  	function _getFileContents( $file )
 211      {
 212          if( !file_exists( $file ) || !is_readable( $file ) )
 213          {
 214              return patErrorManager::raiseError(
 215                                          PATTEMPLATE_READER_ERROR_NO_INPUT,
 216                                          "Could not load templates from $file."
 217                                          );
 218          }
 219  
 220          if( function_exists( 'file_get_contents' ) )
 221              $content    =    @file_get_contents( $file );
 222          else
 223              $content    =    implode( '', file( $file ) );
 224  
 225          /**
 226           * store the file name
 227           */
 228          array_push( $this->_files, $file );
 229  
 230          return    $content;
 231      }
 232  }
 233  ?>


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1