[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/eztemplate/classes/ -> eztemplatetreecache.php (source)

   1  <?php
   2  //
   3  // Definition of eZTemplateTreeCache class
   4  //
   5  // Created on: <28-Nov-2002 07:44:29 amos>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*! \file eztemplatetreecache.php
  30  */
  31  
  32  /*!
  33    \class eZTemplateTreeCache eztemplatetreecache.php
  34    \brief Cache handling for template tree nodes.
  35  
  36  */
  37  
  38  include_once ( 'lib/ezutils/classes/ezdebug.php' );
  39  
  40  define( 'EZ_TEMPLATE_TREE_CACHE_CODE_DATE', 1044440833 );
  41  
  42  class eZTemplateTreeCache
  43  {
  44      /*!
  45       \static
  46       \return the cache table which has cache keys and cache data.
  47      */
  48      function &cacheTable()
  49      {
  50          $templateCache =& $GLOBALS['eZTemplateTreeCacheTable'];
  51          if ( !is_array( $templateCache ) )
  52              $templateCache = array();
  53          return $templateCache;
  54      }
  55  
  56      /*!
  57       \private
  58       \static
  59       \return a new key from \a $key which has some additional info.
  60      */
  61      function internalKey( $key )
  62      {
  63          include_once ( 'lib/ezutils/classes/ezini.php' );
  64          $ini =& eZINI::instance();
  65          $debug = $ini->variable( 'TemplateSettings', 'Debug' ) == 'enabled';
  66          if ( $debug )
  67              $key = $key . '-debug';
  68          else
  69              $key = $key . '-clean';
  70          return $key;
  71      }
  72  
  73      /*!
  74       \static
  75       \return the cache node tree which is stored with the cache key \a $key.
  76               Returns \c null if no cache data was found.
  77      */
  78      function cachedTree( $key, $uri, $res, $templatePath, &$extraParameters )
  79      {
  80          $templateCache =& eZTemplateTreeCache::cacheTable();
  81          $key = eZTemplateTreeCache::internalKey( $key );
  82          $root = null;
  83          if ( isset( $templateCache[$key] ) )
  84          {
  85              $root =& $templateCache[$key]['root'];
  86              eZDebugSetting::writeDebug( 'eztemplate-tree-cache', "Cache hit for uri '$uri' with key '$key'", 'eZTemplateTreeCache::cachedTree' );
  87          }
  88          else
  89              eZDebugSetting::writeDebug( 'eztemplate-tree-cache', "Cache miss for uri '$uri' with key '$key'", 'eZTemplateTreeCache::cachedTree' );
  90  
  91          return $root;
  92      }
  93  
  94      /*!
  95       Sets the template tree node \a $root to be cached with the cache key $root.
  96       \note Trying to overwrite and existing cache key will give a warning and fail.
  97      */
  98      function setCachedTree( $originalKey, $uri, $res, $templatePath, &$extraParameters, &$root )
  99      {
 100          if ( $root === null )
 101              return;
 102          $templateCache =& eZTemplateTreeCache::cacheTable();
 103          $key = eZTemplateTreeCache::internalKey( $originalKey );
 104          if ( isset( $templateCache[$key] ) )
 105          {
 106              eZDebug::writeWarning( "Template cache for key '$key', created from uri '$uri', already exists", 'eZTemplateTreeCache::setCachedTree' );
 107          }
 108          else
 109          {
 110              $templateCache[$key] = array();
 111          }
 112          include_once ( 'lib/ezutils/classes/ezini.php' );
 113          $ini =& eZINI::instance();
 114          $debug = $ini->variable( 'TemplateSettings', 'Debug' ) == 'enabled';
 115          $templateCache[$key]['root'] =& $root;
 116          $templateCache[$key]['info'] = array( 'original_key' => $originalKey,
 117                                                'key' => $key,
 118                                                'uri' => $uri,
 119                                                'debug' => $debug,
 120                                                'resource' => $res,
 121                                                'template_path' => $templatePath,
 122                                                'resource_parameters' => $extraParameters );
 123      }
 124  
 125      /*!
 126       \static
 127       \return true if template tree node caching is enabled.
 128       \note To change this setting edit settings/site.ini and locate the group TemplateSettings and the entry NodeTreeCaching.
 129      */
 130      function isCacheEnabled()
 131      {
 132          if ( isset( $GLOBALS['eZSiteBasics'] ) )
 133          {
 134              $siteBasics = $GLOBALS['eZSiteBasics'];
 135              if ( isset( $siteBasics['no-cache-adviced'] ) and
 136                   $siteBasics['no-cache-adviced'] )
 137              {
 138                  return false;
 139              }
 140          }
 141  
 142          include_once ( 'lib/ezutils/classes/ezini.php' );
 143          $ini =& eZINI::instance();
 144          $cacheEnabled = $ini->variable( 'TemplateSettings', 'NodeTreeCaching' ) == 'enabled';
 145          return $cacheEnabled;
 146      }
 147  
 148      /*!
 149       \static
 150       \return the cache directory for tree node cache files.
 151      */
 152      function cacheDirectory()
 153      {
 154          $cacheDirectory =& $GLOBALS['eZTemplateTreeCacheDirectory'];
 155          if ( !isset( $cacheDirectory ) )
 156          {
 157              include_once ( 'lib/ezfile/classes/ezdir.php' );
 158              include_once ( 'lib/ezutils/classes/ezsys.php' );
 159              $cacheDirectory = eZDir::path( array( eZSys::cacheDirectory(), 'template/tree' ) );
 160          }
 161          return $cacheDirectory;
 162      }
 163  
 164      /*!
 165       Creates the name for the tree cache file and returns it.
 166       The name conists of the md5 of the key and charset with the original filename appended.
 167      */
 168      function treeCacheFilename( $key, $templateFilepath )
 169      {
 170          $internalCharset = eZTextCodec::internalCharset();
 171          $extraName = '';
 172          if ( preg_match( "#^.+/(.*)\.tpl$#", $templateFilepath, $matches ) )
 173              $extraName = '-' . $matches[1];
 174          else if ( preg_match( "#^(.*)\.tpl$#", $templateFilepath, $matches ) )
 175              $extraName = '-' . $matches[1];
 176          $cacheFileKey = "$key-$internalCharset";
 177          $cacheFileName = md5( $cacheFileKey ) . $extraName . '.php';
 178          return $cacheFileName;
 179      }
 180  
 181      /*!
 182       \static
 183       \return true if the cache with the key \a $key can be restored.
 184               A cache file is found restorable when it exists and has a timestamp
 185               higher or equal to \a $timestamp.
 186      */
 187      function canRestoreCache( $key, $timestamp, $templateFilepath )
 188      {
 189          if ( !eZTemplateTreeCache::isCacheEnabled() )
 190              return false;
 191  
 192          $templateCache =& eZTemplateTreeCache::cacheTable();
 193          $key = eZTemplateTreeCache::internalKey( $key );
 194          if ( isset( $templateCache[$key] ) )
 195          {
 196              return false;
 197          }
 198          $cacheFileName = eZTemplateTreeCache::treeCacheFilename( $key, $templateFilepath );
 199  
 200          include_once ( 'lib/ezutils/classes/ezphpcreator.php' );
 201  
 202          $php = new eZPHPCreator( eZTemplateTreeCache::cacheDirectory(), $cacheFileName );
 203          return $php->canRestore( $timestamp );
 204      }
 205  
 206      /*!
 207       \static
 208       Loads the cache with the key \a $key from a file and sets the result in the cache table.
 209       \return true if the cache was successfully restored.
 210      */
 211      function restoreCache( $key, $templateFilepath )
 212      {
 213          if ( !eZTemplateTreeCache::isCacheEnabled() )
 214              return false;
 215  
 216          $templateCache =& eZTemplateTreeCache::cacheTable();
 217          $key = eZTemplateTreeCache::internalKey( $key );
 218          if ( isset( $templateCache[$key] ) )
 219          {
 220              eZDebug::writeWarning( "Template cache for key '$key' already exist, cannot restore cache", 'eZTemplateTreeCache::restoreCache' );
 221              return false;
 222          }
 223          $cacheFileName = eZTemplateTreeCache::treeCacheFilename( $key, $templateFilepath );
 224  
 225          include_once ( 'lib/ezutils/classes/ezphpcreator.php' );
 226  
 227          $php = new eZPHPCreator( eZTemplateTreeCache::cacheDirectory(), $cacheFileName );
 228          $variables = $php->restore( array( 'info' => 'TemplateInfo',
 229                                             'root' => 'TemplateRoot',
 230                                             'cache-date' => 'eZTemplateTreeCacheCodeDate' ) );
 231          if ( $variables['cache-date'] != EZ_TEMPLATE_TREE_CACHE_CODE_DATE )
 232              return false;
 233          $cache =& $templateCache[$key];
 234          $cache['root'] =& $variables['root'];
 235          $cache['info'] =& $variables['info'];
 236          return true;
 237      }
 238  
 239      /*!
 240       \static
 241       Stores the data of the cache with the key \a $key to a file.
 242       \return false if the cache does not exist.
 243      */
 244      function storeCache( $key, $templateFilepath )
 245      {
 246          if ( !eZTemplateTreeCache::isCacheEnabled() )
 247              return false;
 248          $templateCache =& eZTemplateTreeCache::cacheTable();
 249          $key = eZTemplateTreeCache::internalKey( $key );
 250          if ( !isset( $templateCache[$key] ) )
 251          {
 252              eZDebug::writeWarning( "Template cache for key '$key' does not exist, cannot store cache", 'eZTemplateTreeCache::storeCache' );
 253              return;
 254          }
 255          $cacheFileName = eZTemplateTreeCache::treeCacheFilename( $key, $templateFilepath );
 256  
 257          $cache =& $templateCache[$key];
 258  
 259          include_once ( 'lib/ezutils/classes/ezphpcreator.php' );
 260  
 261          $php = new eZPHPCreator( eZTemplateTreeCache::cacheDirectory(), $cacheFileName );
 262          $php->addVariable( 'eZTemplateTreeCacheCodeDate', EZ_TEMPLATE_TREE_CACHE_CODE_DATE );
 263          $php->addSpace();
 264          $php->addVariable( 'TemplateInfo', $cache['info'] );
 265          $php->addSpace();
 266          $php->addVariable( 'TemplateRoot', $cache['root'] );
 267          $php->store();
 268      }
 269  }
 270  
 271  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7