Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eztemplatetreecache.php
Go to the documentation of this file.
1 <?php
17 class eZTemplateTreeCache
18 {
19 const CODE_DATE = 1044440833;
20
25 static function &cacheTable()
26 {
27 $templateCache =& $GLOBALS['eZTemplateTreeCacheTable'];
28 if ( !is_array( $templateCache ) )
29 $templateCache = array();
30 return $templateCache;
31 }
32
38 static function internalKey( $key )
39 {
40 $ini = eZINI::instance();
41 $debug = $ini->variable( 'TemplateSettings', 'Debug' ) == 'enabled';
42 if ( $debug )
43 $key = $key . '-debug';
44 else
45 $key = $key . '-clean';
46 return $key;
47 }
48
54 static function cachedTree( $key, $uri, $res, $templatePath, &$extraParameters )
55 {
56 $templateCache =& eZTemplateTreeCache::cacheTable();
57 $key = eZTemplateTreeCache::internalKey( $key );
58 $root = null;
59 if ( isset( $templateCache[$key] ) )
60 {
61 $root =& $templateCache[$key]['root'];
62 eZDebugSetting::writeDebug( 'eztemplate-tree-cache', "Cache hit for uri '$uri' with key '$key'", 'eZTemplateTreeCache::cachedTree' );
63 }
64 else
65 eZDebugSetting::writeDebug( 'eztemplate-tree-cache', "Cache miss for uri '$uri' with key '$key'", 'eZTemplateTreeCache::cachedTree' );
66
67 return $root;
68 }
69
74 static function setCachedTree( $originalKey, $uri, $res, $templatePath, &$extraParameters, &$root )
75 {
76 if ( $root === null )
77 return;
78 $templateCache =& eZTemplateTreeCache::cacheTable();
79 $key = eZTemplateTreeCache::internalKey( $originalKey );
80 if ( isset( $templateCache[$key] ) )
81 {
82 eZDebug::writeDebug( "Template cache for key '$key', created from uri '$uri', already exists", __METHOD__ );
83 }
84 else
85 {
86 $templateCache[$key] = array();
87 }
88 $ini = eZINI::instance();
89 $debug = $ini->variable( 'TemplateSettings', 'Debug' ) == 'enabled';
90 $templateCache[$key]['root'] =& $root;
91 $templateCache[$key]['info'] = array( 'original_key' => $originalKey,
92 'key' => $key,
93 'uri' => $uri,
94 'debug' => $debug,
95 'resource' => $res,
96 'template_path' => $templatePath,
97 'resource_parameters' => $extraParameters );
98 }
99
105 static function isCacheEnabled()
106 {
107 if ( isset( $GLOBALS['eZSiteBasics'] ) )
108 {
109 $siteBasics = $GLOBALS['eZSiteBasics'];
110 if ( isset( $siteBasics['no-cache-adviced'] ) and
111 $siteBasics['no-cache-adviced'] )
112 {
113 return false;
114 }
115 }
116
117 $ini = eZINI::instance();
118 $cacheEnabled = $ini->variable( 'TemplateSettings', 'NodeTreeCaching' ) == 'enabled';
119 return $cacheEnabled;
120 }
121
126 static function cacheDirectory()
127 {
128 $cacheDirectory =& $GLOBALS['eZTemplateTreeCacheDirectory'];
129 if ( !isset( $cacheDirectory ) )
130 {
131 $cacheDirectory = eZDir::path( array( eZSys::cacheDirectory(), 'template/tree' ) );
132 }
133 return $cacheDirectory;
134 }
135
140 static function treeCacheFilename( $key, $templateFilepath )
141 {
142 $internalCharset = eZTextCodec::internalCharset();
143 $extraName = '';
144 if ( preg_match( "#^.+/(.*)\.tpl$#", $templateFilepath, $matches ) )
145 $extraName = '-' . $matches[1];
146 else if ( preg_match( "#^(.*)\.tpl$#", $templateFilepath, $matches ) )
147 $extraName = '-' . $matches[1];
148 $cacheFileKey = "$key-$internalCharset";
149 $cacheFileName = md5( $cacheFileKey ) . $extraName . '.php';
150 return $cacheFileName;
151 }
152
159 static function canRestoreCache( $key, $timestamp, $templateFilepath )
160 {
161 if ( !eZTemplateTreeCache::isCacheEnabled() )
162 return false;
163
164 $templateCache =& eZTemplateTreeCache::cacheTable();
165 $key = eZTemplateTreeCache::internalKey( $key );
166 if ( isset( $templateCache[$key] ) )
167 {
168 return false;
169 }
170 $cacheFileName = eZTemplateTreeCache::treeCacheFilename( $key, $templateFilepath );
171
172 $php = new eZPHPCreator( eZTemplateTreeCache::cacheDirectory(), $cacheFileName );
173 return $php->canRestore( $timestamp );
174 }
175
181 static function restoreCache( $key, $templateFilepath )
182 {
183 if ( !eZTemplateTreeCache::isCacheEnabled() )
184 return false;
185
186 $templateCache =& eZTemplateTreeCache::cacheTable();
187 $key = eZTemplateTreeCache::internalKey( $key );
188 if ( isset( $templateCache[$key] ) )
189 {
190 eZDebug::writeDebug( "Template cache for key '$key' already exist, cannot restore cache", __METHOD__ );
191 return false;
192 }
193 $cacheFileName = eZTemplateTreeCache::treeCacheFilename( $key, $templateFilepath );
194
195 $php = new eZPHPCreator( eZTemplateTreeCache::cacheDirectory(), $cacheFileName );
196 $variables = $php->restore( array( 'info' => 'TemplateInfo',
197 'root' => 'TemplateRoot',
198 'cache-date' => 'eZTemplateTreeCacheCodeDate' ) );
199 if ( $variables['cache-date'] != eZTemplateTreeCache::CODE_DATE )
200 return false;
201 $cache =& $templateCache[$key];
202 $cache['root'] =& $variables['root'];
203 $cache['info'] =& $variables['info'];
204 return true;
205 }
206
212 static function storeCache( $key, $templateFilepath )
213 {
214 if ( !eZTemplateTreeCache::isCacheEnabled() )
215 return false;
216 $templateCache =& eZTemplateTreeCache::cacheTable();
217 $key = eZTemplateTreeCache::internalKey( $key );
218 if ( !isset( $templateCache[$key] ) )
219 {
220 eZDebug::writeDebug( "Template cache for key '$key' does not exist, cannot store cache", __METHOD__ );
221 return;
222 }
223 $cacheFileName = eZTemplateTreeCache::treeCacheFilename( $key, $templateFilepath );
224
225 $cache =& $templateCache[$key];
226
227 $php = new eZPHPCreator( eZTemplateTreeCache::cacheDirectory(), $cacheFileName );
228 $php->addVariable( 'eZTemplateTreeCacheCodeDate', eZTemplateTreeCache::CODE_DATE );
229 $php->addSpace();
230 $php->addVariable( 'TemplateInfo', $cache['info'] );
231 $php->addSpace();
232 $php->addVariable( 'TemplateRoot', $cache['root'] );
233 $php->store( true );
234 }
235 }
236
237 ?>
eZDebugSetting\writeDebug
static writeDebug( $conditionName, $string, $label="")
Definition: ezdebugsetting.php:127
eZDebug\writeDebug
static writeDebug( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:588
eZDir\path
static path( $names, $includeEndSeparator=false, $type=self::SEPARATOR_UNIX)
Definition: ezdir.php:237
eZPHPCreator
eZPHPCreator provides a simple interface for creating and executing PHP code.
eZSys\cacheDirectory
static cacheDirectory()
Definition: ezsys.php:527
eZTemplateTreeCache
Cache handling for template tree nodes.
Definition: eztemplatetreecache.php:18
eZTemplateTreeCache\restoreCache
static restoreCache( $key, $templateFilepath)
Definition: eztemplatetreecache.php:181
eZTemplateTreeCache\internalKey
static internalKey( $key)
Definition: eztemplatetreecache.php:38
eZTemplateTreeCache\cacheTable
static & cacheTable()
Definition: eztemplatetreecache.php:25
eZTemplateTreeCache\setCachedTree
static setCachedTree( $originalKey, $uri, $res, $templatePath, &$extraParameters, &$root)
Definition: eztemplatetreecache.php:74
eZTemplateTreeCache\isCacheEnabled
static isCacheEnabled()
Definition: eztemplatetreecache.php:105
eZTemplateTreeCache\storeCache
static storeCache( $key, $templateFilepath)
Definition: eztemplatetreecache.php:212
eZTemplateTreeCache\treeCacheFilename
static treeCacheFilename( $key, $templateFilepath)
Definition: eztemplatetreecache.php:140
eZTemplateTreeCache\cachedTree
static cachedTree( $key, $uri, $res, $templatePath, &$extraParameters)
Definition: eztemplatetreecache.php:54
eZTemplateTreeCache\cacheDirectory
static cacheDirectory()
Definition: eztemplatetreecache.php:126
eZTemplateTreeCache\canRestoreCache
static canRestoreCache( $key, $timestamp, $templateFilepath)
Definition: eztemplatetreecache.php:159
eZTemplateTreeCache\CODE_DATE
const CODE_DATE
Definition: eztemplatetreecache.php:19
eZTextCodec\internalCharset
static internalCharset()
Definition: eztextcodec.php:546
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.