10-25-2008, 04:45 PM
|
#6 (permalink)
|
|
The Addict
Join Date: Jun 2008
Posts: 335
Thanks: 2
|
In order to make a more reliable form of publishing I included a semicache system that includes the cache'd file and exit all processing after that. The class below is just an example because you'd really need to get rid of ALL the white space before taking and comparing md5 as FTP transfer tends to add white spaces.
PHP Code:
<?php
class View { private $template; private $d; private $md5; private $key;
public function __construct() { //Check if the cache folder exists if ( !is_dir(WEB_HOST . 'components/cache') ) { @mkdir( WEB_HOST . 'components/cache' ); }
//Now establish default delimiter and key array $this->d = '%'; $this->key = array(); }
public function load( $f ) { if ( file_exists(WEB_HOST . 'compnents/' . $f) ) { $this->template = $f; return $this; } }
public function add( $key, $item ) { $this->key[$key] = $item; return $this; }
public function delimit( $delimeter ) { $this->d = $delimeter; }
public function publish() { //Check the file's MD5 $file = WEB_HOST . 'components/' . $this->template; $storage = file_get_contents( $file ); $md5 = md5_file( $file );
//Check cache md5 $cacheFile = WEB_HOST . 'components/cache/' . $this->template; $cacheMd5 = ''; if( file_exists($cacheFile) ) { $cacheStorage = file_get_contents( $cacheFile ); $cacheMd5 = substr( $cacheStorage, 0, 32 ); }
//now check the md5 sums if ( $md5 !== $cacheMd5 ) { //It's not the same so send it through the loop foreach ( $this->key as $key => $val ) { $storage = str_replace( $this->d . $key . $this->d, $val, $storage ); }
//now put it into the cache $storage = $md5 . $storage; file_put_contents( $cacheFile, $storage ); }
//include the file and kill any PHP after that as it should be it include ( $cacheFile ); exit;
}
} ?>
|
|
|
|