Skip to content

Instantly share code, notes, and snippets.

@aquilax
Created November 4, 2011 08:40
Show Gist options
  • Save aquilax/1338937 to your computer and use it in GitHub Desktop.
Save aquilax/1338937 to your computer and use it in GitHub Desktop.
CodeIgniter static website generator
<?php
/**
* Creates static version of CodeIgniter site
*
* @author aquilax
*/
class MY_Output extends CI_Output {
function __construct() {
parent::__construct();
}
/**
* Write a Cache File
*
* @access public
* @param string
* @return void
*/
function _write_cache($output) {
$CI = & get_instance();
$path = $CI->config->item('cache_path');
$cache_path = ($path == '') ? FCPATH : $path;
if (!is_dir($cache_path) OR !is_really_writable($cache_path)) {
log_message('error', "Unable to write cache file: " . $cache_path);
return;
}
if (pathinfo($CI->uri->uri_string(), PATHINFO_EXTENSION) == '') {
$extra = rtrim($CI->uri->uri_string(), '/') . '/index.html';
} else {
$extra = $CI->uri->uri_string();
}
if (pathinfo($extra, PATHINFO_EXTENSION) !== 'html') {
return;
}
$cache_path .= $extra;
$path = dirname($cache_path);
if (!file_exists($path)) {
if (!mkdir($path, 0755, TRUE)) {
log_message('error', "Unable to create directory: " . $path);
return;
}
}
if (!$fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE)) {
log_message('error', "Unable to write cache file: " . $cache_path);
return;
}
if (flock($fp, LOCK_EX)) {
fwrite($fp, $output);
flock($fp, LOCK_UN);
} else {
log_message('error', "Unable to secure a file lock for file at: " . $cache_path);
return;
}
fclose($fp);
@chmod($cache_path, FILE_WRITE_MODE);
log_message('debug', "Cache file written: " . $cache_path);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment