Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #7 from jurajkapsz/master
Checks & restores cache directory on the go. Fixes #4
  • Loading branch information
brendo committed May 29, 2014
2 parents 6314200 + d6e3c91 commit dd23545
Showing 1 changed file with 40 additions and 33 deletions.
73 changes: 40 additions & 33 deletions extension.driver.php
@@ -1,9 +1,9 @@
<?php

Class Extension_CacheableDatasource extends Extension {

private $_sectionsToFlush = null;

public function install() {
if(!General::realiseDirectory(CACHE . '/cacheabledatasource', Symphony::Configuration()->get('write_mode', 'directory'))) {
throw new Exception(__('Cacheable Datasource was not installed: cache directory could not be created at %s.', array('<code>/manifest/cache/cacheabledatasource</code>')));
Expand All @@ -14,7 +14,7 @@ public function install() {
public function uninstall() {
if (is_dir(CACHE . '/cacheabledatasource')) rmdir(CACHE . '/cacheabledatasource');
}

public function getSubscribedDelegates() {
return array(
array(
Expand Down Expand Up @@ -74,7 +74,7 @@ public function getSubscribedDelegates() {
),
);
}

public function flushCache($context) {

$this->__fetchSectionsFromContext($context);
Expand All @@ -87,7 +87,7 @@ public function flushCache($context) {
try {
foreach($dsm->listAll() as $ds) {
if(!in_array($ds['source'], $this->_sectionsToFlush)) continue;

$cache = glob($cacheDir.$ds['handle'].'_*.xml');
if(empty($cache)) continue;

Expand All @@ -98,7 +98,7 @@ public function flushCache($context) {
} catch(Exception $e){
Symphony::Log()->writeToLog(date('d.m.y H:i:s') . ' > CacheableDatasource: '. $e->getMessage(), true);
}

}

private function __fetchSectionsFromContext($context) {
Expand Down Expand Up @@ -127,14 +127,14 @@ private function __fetchSectionsFromContext($context) {

General::flattenArray($associatedSections);
$associatedSections = array_unique(array_values($associatedSections));

if(is_array($associatedSections) && !empty($associatedSections)) $this->_sectionsToFlush = array_merge($this->_sectionsToFlush, $associatedSections);

}

public function eventFinalSaveFilter(array $context){
if(!in_array('cacheable-datasource', $context['event']->eParamFILTERS)) return;

$this->flushCache($context);
}

Expand All @@ -145,7 +145,7 @@ public function appendEventFilter(array $context){
'Flush DS Cache'
);
}

/**
* `DatasourcePreCreate` delegate callback function
* Checks whether a data source should be cached or not: builds a filename based on
Expand All @@ -157,15 +157,15 @@ public function appendEventFilter(array $context){
* Delegate context including the data source object, output XML and param pool array
*/
public function dataSourcePreExecute($context) {

$ds = $context['datasource'];
$param_pool = $context['param_pool'];

// don't cache if no cache TTL is set at all
if(!isset($ds->dsParamCACHE)) return;
// don't cache when the TTL is zero
if((int)$ds->dsParamCACHE == 0) return;

$filename = NULL;
$file_age = 0;

Expand Down Expand Up @@ -199,7 +199,7 @@ public function dataSourcePreExecute($context) {
$xml = preg_replace('/cache-age="fresh"/', 'cache-age="'.$file_age.'s"', $xml);

} else {

// Backup the param pool, and see what's been added
$tmp = array();

Expand All @@ -225,9 +225,9 @@ public function dataSourcePreExecute($context) {

$context['xml'] = $xml;
$context['param_pool'] = $param_pool;

}

/**
* Serialises the data source object properties into a checksum hash to see
* whether the data source is currently cached, and whether it has expired.
Expand All @@ -243,6 +243,13 @@ public function dataSourcePreExecute($context) {
private function __buildCacheFilename($datasource, &$filename, &$file_age) {
$filename = null;

// Checks if cacheabledatasource directory exists. If not, try to restore.
if (!file_exists(CACHE . '/cacheabledatasource')) {
if (!General::realiseDirectory(CACHE . '/cacheabledatasource', Symphony::Configuration()->get('write_mode', 'directory'))) {
throw new Exception(__('Cacheable Datasource: Cache directory could not be restored at %s.', array('<code>/manifest/cache/cacheabledatasource</code>')));
}
}

// get resolved values of each public property of this DS
// (sort, filters, included elements etc.)
foreach (get_class_vars(get_class($datasource)) as $key => $value) {
Expand All @@ -262,10 +269,10 @@ private function __buildCacheFilename($datasource, &$filename, &$file_age) {
if (!file_exists($filename)) return false;

$file_age = (int)(floor(time() - filemtime($filename)));

return ($file_age < ($datasource->dsParamCACHE));
}

/**
* Executes a data source. Invalid XML is escaped (CDATA) but still
* cached. Prevents persistent cached XML from breaking pages.
Expand Down Expand Up @@ -320,7 +327,7 @@ private function __executeDatasource($datasource, &$param_pool=array()) {

return $ret;
}

/**
* `DatasourcePreCreate` and `DatasourcePreEdit` delegates callback function
* Adds the dsParamCACHE property to the data source class file.
Expand All @@ -329,21 +336,21 @@ private function __executeDatasource($datasource, &$param_pool=array()) {
* Delegate context including string contents of the data souce PHP file
*/
public function dataSourceSave($context) {

$contents = $context['contents'];
$cache = $_POST['fields']['cache'];

if(!isset($cache)) return;

$contents = preg_replace(
"/<!-- VAR LIST -->/",
"public \$dsParamCACHE = '$cache';\n\t\t<!-- VAR LIST -->",
$contents
);

$context['contents'] = $contents;
}

/**
* `InitaliseAdminPageHead` delegate callback function
* Appends script assets and context to page head
Expand All @@ -352,15 +359,15 @@ public function dataSourceSave($context) {
* Delegate context including page object
*/
public function initaliseAdminPageHead($context) {

$page = Administration::instance()->Page;
if(!$page instanceOf contentBlueprintsDatasources) return;

$url_context = $page->getContext();
if(!in_array($url_context[0], array('new', 'edit'))) return;

$cache = 0;

// if editing an existing data source, instantiate the DS object
// to retrieve the dsParamCACHE property if it exists
if($url_context[0] == 'edit') {
Expand All @@ -370,7 +377,7 @@ public function initaliseAdminPageHead($context) {
$cache = $datasource->dsParamCACHE;
}
if(is_null($cache)) $cache = 0;

Administration::instance()->Page->addElementToHead(
new XMLElement(
'script',
Expand All @@ -380,15 +387,15 @@ public function initaliseAdminPageHead($context) {
array('type' => 'text/javascript')
), time()
);

Administration::instance()->Page->addScriptToHead(
URL . '/extensions/cacheabledatasource/assets/cacheabledatasource.blueprintsdatasources.js',
time()
);


}

}

?>

0 comments on commit dd23545

Please sign in to comment.