eztemplatefileresource.php

00001 <?php
00002 //
00003 // Definition of eZTemplateFileResource class
00004 //
00005 // Created on: <01-Mar-2002 13:49:18 amos>
00006 //
00007 // Copyright (C) 1999-2003 Exponential. All rights reserved.
00008 //
00009 // This source file is part of the Exponential (tm) Open Source Content
00010 // Management System.
00011 //
00012 // This file may be distributed and/or modified under the terms of the
00013 // "GNU General Public License" version 2 as published by the Free
00014 // Software Foundation and appearing in the file LICENSE.GPL included in
00015 // the packaging of this file.
00016 //
00017 // Licencees holding valid "Exponential professional licences" may use this
00018 // file in accordance with the "Exponential professional licence" Agreement
00019 // provided with the Software.
00020 //
00021 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
00022 // THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00023 // PURPOSE.
00024 //
00025 // The "Exponential professional licence" is available at
00026 // http://ez.no/products/licences/professional/. For pricing of this licence
00027 // please contact us via e-mail to licence@ez.no. Further contact
00028 // information is available at http://ez.no/home/contact/.
00029 //
00030 // The "GNU General Public License" (GPL) is available at
00031 // http://www.gnu.org/copyleft/gpl.html.
00032 //
00033 // Contact licence@ez.no if any conditions of this licencing isn't clear to
00034 // you.
00035 //
00036 
00045 include_once( "lib/ezi18n/classes/eztextcodec.php" );
00046 include_once( "lib/eztemplate/classes/eztemplatetreecache.php" );
00047 include_once( "lib/eztemplate/classes/eztemplateprocesscache.php" );
00048 
eZTemplateFileResource
00050 {
eZTemplateFileResource( $name = "file", $servesStaticData = true )
00057     {
00058         $this->Name = $name;
00059         $this->ServesStaticData = $servesStaticData;
00060         $this->TemplateCache = array();
00061     }
00062 
resourceName()
00067     {
00068         return $this->Name;
00069     }
00070 
00071     /*
00072      \return true if this resource handler servers static data,
00073      this means that the data can be cached by the template system.
00074     */
00075     function servesStaticData()
00076     {
00077         return $this->ServesStaticData;
00078     }
00079 
cacheKey( $keyData, $res, $templatePath, &$extraParameters )
00090     {
00091         $key = md5( $keyData );
00092         return $key;
00093     }
00094 
hasCachedProcessTree( $keyData, $uri, $res, $templatePath, &$extraParameters, $timestamp )
00099     {
00100         return false;
00101         $key = $this->cacheKey( $keyData, $res, $templatePath, $extraParameters );
00102         if ( eZTemplateTreeCache::canRestoreCache( $key, $timestamp ) )
00103             eZTemplateTreeCache::restoreCache( $key );
00104         return eZTemplateTreeCache::cachedTree( $key, $uri, $res, $templatePath, $extraParameters );
00105     }
00106 
generateProcessCache( $keyData, $uri, $res, $templatePath, &$extraParameters, &$resourceData )
00111     {
00112         generateProcessCache( $keyData, $uri, $res, $templatePath, &$extraParameters, &$resourceData )', 'eztemplatefileresource' );
00113         $key = $this->cacheKey( $keyData, $res, $templatePath, $extraParameters );
00114         return eZTemplateProcessCache::generateCache( $key, $resourceData );
00115     }
00116 
00117     function canGenerateProcessCache()
00118     {
00119         return eZTemplateProcessCache::isCacheEnabled();
00120     }
00121 
cachedTemplateTree( $keyData, $uri, $res, $templatePath, &$extraParameters, $timestamp )
00126     {
00127         $key = $this->cacheKey( $keyData, $res, $templatePath, $extraParameters );
00128         if ( eZTemplateTreeCache::canRestoreCache( $key, $timestamp ) )
00129             eZTemplateTreeCache::restoreCache( $key );
00130         return eZTemplateTreeCache::cachedTree( $key, $uri, $res, $templatePath, $extraParameters );
00131     }
00132 
setCachedTemplateTree( $keyData, $uri, $res, $templatePath, &$extraParameters, &$root )
00137     {
00138         $key = $this->cacheKey( $keyData, $res, $templatePath, $extraParameters );
00139         eZTemplateTreeCache::setCachedTree( $key, $uri, $res, $templatePath, $extraParameters, $root );
00140         eZTemplateTreeCache::storeCache( $key );
00141     }
00142 
00147 //     function handleResource( &$tpl, &$templateRoot, &$text, &$tstamp, $uri, $resourceName, &$path, &$keyData, $method, &$extraParameters )
handleResource( &$tpl, &$resourceData, $method, &$extraParameters )
00149     {
00150         return $this->handleResourceData( $tpl, $this, $resourceData, $method, $extraParameters );
00151     }
00152 
00160 //     function handleResourceData( &$tpl, &$handler, &$templateRoot, &$text, &$tstamp, $uri, $resourceName, &$path, &$keyData, $method, &$extraParameters )
handleResourceData( &$tpl, &$handler, &$resourceData, $method, &$extraParameters )
00162     {
00163         // &$templateRoot, &$text, &$tstamp, $uri, $resourceName, &$path, &$keyData
00164         $templateRoot =& $resourceData['root-node'];
00165         $text =& $resourceData['text'];
00166         $tstamp =& $resourceData['time-stamp'];
00167         $uri =& $resourceData['uri'];
00168         $resourceName =& $resourceData['resource'];
00169         $path =& $resourceData['template-filename'];
00170         $keyData =& $resourceData['key-data'];
00171 
00172         if ( !file_exists( $path ) )
00173             return false;
00174         $tstamp = filemtime( $path );
00175         $result = false;
00176         $canCache = true;
00177         $templateRoot = null;
00178         if ( !$handler->servesStaticData() )
00179             $canCache = false;
00180         $keyData = 'file:' . $path;
00181         if ( $method == EZ_RESOURCE_FETCH )
00182         {
00183             if ( $canCache )
00184             {
00185                 if ( $handler->hasCachedProcessTree( $keyData, $uri, $resourceName, $path, $extraParameters, $tstamp ) )
00186                      $resourceData['process-cache'] = true;
00187             }
00188             if ( $canCache )
00189                 $templateRoot = $handler->cachedTemplateTree( $keyData, $uri, $resourceName, $path, $extraParameters, $tstamp );
00190 
00191             if ( $templateRoot !== null )
00192                 return true;
00193 
00194             $fd = fopen( $path, "r" );
00195             if ( $fd )
00196             {
00197                 $text = fread( $fd, filesize( $path ) );
00198 //                 $charset = "utf8";
00199                 $tplINI =& $tpl->ini();
00200                 $charset = $tplINI->variable( 'CharsetSettings', 'DefaultTemplateCharset' );
00201                 $pos = strpos( $text, "\n" );
00202                 if ( $pos !== false )
00203                 {
00204                     $line = substr( $text, 0, $pos );
00205                     if ( preg_match( "/^\{\*\?template(.+)\?\*\}/", $line, $tpl_arr ) )
00206                     {
00207                         $args = explode( " ", trim( $tpl_arr[1] ) );
00208                         foreach ( $args as $arg )
00209                         {
00210                             $vars = explode( '=', trim( $arg ) );
00211                             if ( $vars[0] == "charset" )
00212                             {
00213                                 $val = $vars[1];
00214                                 if ( $val[0] == '"' and
00215                                      strlen( $val ) > 0 and
00216                                      $val[strlen($val)-1] == '"' )
00217                                     $val = substr( $val, 1, strlen($val) - 2 );
00218                                 $charset = $val;
00219                             }
00220                         }
00221                     }
00222                 }
00223                 if ( eZTemplate::isDebugEnabled() )
00224                     eZDebug::writeNotice( "$path, $charset" );
00225                 $codec =& eZTextCodec::instance( $charset );
00226                 eZDebug::accumulatorStart( 'templage_resource_conversion', 'template_total', 'String conversion in template resource' );
00227                 $text = $codec->convertString( $text );
00228                 eZDebug::accumulatorStop( 'templage_resource_conversion', 'template_total', 'String conversion in template resource' );
00229                 $result = true;
00230                 if ( eZTemplate::isDebugEnabled() )
00231                 {
00232                     $text = "<!-- including template $path -->\n<p class=\"small\">$path</p><br/>\n" . $text;
00233                 }
00234             }
00235         }
00236         else if ( $method == EZ_RESOURCE_QUERY )
00237             $result = true;
00238         return $result;
00239     }
00240 
Name;
ServesStaticData;
TemplateCache;
00248 }
00249 
00250 ?>
 

Exponential