Implemented a cache for documentation.
This commit is contained in:
parent
fcc58778c5
commit
50b5ecc4a0
84
manual/new/Cache.php
Normal file
84
manual/new/Cache.php
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class Cache
|
||||||
|
{
|
||||||
|
protected $_dir;
|
||||||
|
protected $_ext;
|
||||||
|
protected $_page;
|
||||||
|
protected $_file;
|
||||||
|
protected $_timeToLive;
|
||||||
|
|
||||||
|
public function __construct($dir, $ext, $timeToLive)
|
||||||
|
{
|
||||||
|
$this->_dir = $dir;
|
||||||
|
$this->_ext = $ext;
|
||||||
|
$this->_timeToLive = $timeToLive;
|
||||||
|
|
||||||
|
$this->_page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
|
||||||
|
$this->_file = $this->_dir . md5($this->_page) . '.' . $this->_ext;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Begins caching the output.
|
||||||
|
*
|
||||||
|
* @return A boolean value indicating whether a valid cached version of the
|
||||||
|
* page was found and echoed (false), or not (true).
|
||||||
|
*/
|
||||||
|
public function begin()
|
||||||
|
{
|
||||||
|
$showCache = (file_exists($this->_file) && $this->isValid());
|
||||||
|
clearstatcache();
|
||||||
|
|
||||||
|
if ($showCache) {
|
||||||
|
readfile($this->_file);
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
ob_start();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ends caching the output and saves it to a cache file.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function end()
|
||||||
|
{
|
||||||
|
// Generate a new cache file
|
||||||
|
$fp = @fopen($this->_file, 'w');
|
||||||
|
|
||||||
|
// Save the contents of output buffer to the file
|
||||||
|
@fwrite($fp, ob_get_contents());
|
||||||
|
@fclose($fp);
|
||||||
|
|
||||||
|
ob_end_flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes all files in the cache directory.
|
||||||
|
*/
|
||||||
|
public function clear()
|
||||||
|
{
|
||||||
|
if ($handle = opendir($this->dir)) {
|
||||||
|
while ($file = readdir($handle)) {
|
||||||
|
if ($file !== '.' && $file !== '..') {
|
||||||
|
unlink($this->dir . '/' . $file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
closedir($handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is used to check whether the cache file is valid to use.
|
||||||
|
*
|
||||||
|
* Currently it compares the modification date of the cache file to the
|
||||||
|
* time-to-live value.
|
||||||
|
*
|
||||||
|
* @return True, if cache file is valid; false otherwise.
|
||||||
|
*/
|
||||||
|
protected function isValid()
|
||||||
|
{
|
||||||
|
return (time() - filemtime($this->_file)) < $this->_timeToLive;
|
||||||
|
}
|
||||||
|
}
|
@ -7,47 +7,66 @@ set_include_path($includePath);
|
|||||||
|
|
||||||
require_once('Sensei/Sensei.php');
|
require_once('Sensei/Sensei.php');
|
||||||
require_once('DocTool.php');
|
require_once('DocTool.php');
|
||||||
|
require_once('Cache.php');
|
||||||
|
|
||||||
spl_autoload_register(array('Sensei', 'autoload'));
|
spl_autoload_register(array('Sensei', 'autoload'));
|
||||||
|
|
||||||
$tool = new DocTool('docs/en/root.txt');
|
// Executes the svn info command for the current directory and parses the last
|
||||||
// $tool->setOption('clean-url', true);
|
// changed date in order to calculate the time-to-live value for cache.
|
||||||
|
$timeToLive = 0;
|
||||||
$supportedLangs = array('en', 'fi');
|
exec('svn info .', $output);
|
||||||
foreach ($supportedLangs as $language) {
|
foreach ($output as $line) {
|
||||||
include "lang/$language.php";
|
if (preg_match('/^Last Changed Date: (.*) \(.*\)$/', $line, $matches)) {
|
||||||
$tool->addLanguage($lang[$language], $language);
|
$timeToLive = time() - strtotime($matches[1]);
|
||||||
}
|
break;
|
||||||
|
|
||||||
$baseUrl = '';
|
|
||||||
$title = 'Doctrine Manual';
|
|
||||||
$section = null;
|
|
||||||
|
|
||||||
if (isset($_GET['chapter'])) {
|
|
||||||
$section = $tool->findByPath($_GET['chapter']);
|
|
||||||
if ($tool->getOption('clean-url')) {
|
|
||||||
$baseUrl = '../';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($_GET['one-page'])) {
|
$cache = new Cache('./cache/', 'cache', $timeToLive);
|
||||||
$tool->setOption('one-page', true);
|
|
||||||
$tool->setOption('max-level', 0);
|
if ($cache->begin()) {
|
||||||
$section = null;
|
|
||||||
|
$tool = new DocTool('docs/en/root.txt');
|
||||||
|
// $tool->setOption('clean-url', true);
|
||||||
|
|
||||||
|
$supportedLangs = array('en', 'fi');
|
||||||
|
foreach ($supportedLangs as $language) {
|
||||||
|
include "lang/$language.php";
|
||||||
|
$tool->addLanguage($lang[$language], $language);
|
||||||
|
}
|
||||||
|
|
||||||
$baseUrl = '';
|
$baseUrl = '';
|
||||||
}
|
$title = 'Doctrine Manual';
|
||||||
|
$section = null;
|
||||||
if ($section) {
|
|
||||||
while ($section->getLevel() > 1) {
|
if (isset($_GET['chapter'])) {
|
||||||
$section = $section->getParent();
|
$section = $tool->findByPath($_GET['chapter']);
|
||||||
|
if ($tool->getOption('clean-url')) {
|
||||||
|
$baseUrl = '../';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$tool->setOption('section', $section);
|
if (isset($_GET['one-page'])) {
|
||||||
$title .= ' - Chapter ' . $section->getIndex() . ' ' . $section->getName();
|
$tool->setOption('one-page', true);
|
||||||
|
$tool->setOption('max-level', 0);
|
||||||
|
$section = null;
|
||||||
|
$baseUrl = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($section) {
|
||||||
|
while ($section->getLevel() > 1) {
|
||||||
|
$section = $section->getParent();
|
||||||
|
}
|
||||||
|
|
||||||
|
$tool->setOption('section', $section);
|
||||||
|
$title .= ' - Chapter ' . $section->getIndex() . ' ' . $section->getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($tool->getOption('clean-url')) {
|
||||||
|
$tool->setOption('base-url', $baseUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
include 'template.php';
|
||||||
|
|
||||||
|
$cache->end();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($tool->getOption('clean-url')) {
|
|
||||||
$tool->setOption('base-url', $baseUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
include 'template.php';
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user