Skip to content

Instantly share code, notes, and snippets.

@Fi1osof
Created January 4, 2013 00:31
Show Gist options
  • Save Fi1osof/4448846 to your computer and use it in GitHub Desktop.
Save Fi1osof/4448846 to your computer and use it in GitHub Desktop.
Modified modCacheManager::generateContext()
<?php
public function generateContext($key, array $options = array()) {
$results = array();
$contextConfig= array_merge($this->modx->_systemConfig, $options);
/** @var modContext $obj */
$obj= $this->modx->getObject('modContext', $key, true);
if (is_object($obj) && $obj instanceof modContext && $obj->get('key')) {
$cacheKey = $obj->getCacheKey();
$contextKey = is_object($this->modx->context) ? $this->modx->context->get('key') : $key;
/* generate the ContextSettings */
$results['config']= array();
if ($settings= $obj->getMany('ContextSettings')) {
/** @var modContextSetting $setting */
foreach ($settings as $setting) {
$k= $setting->get('key');
$v= $setting->get('value');
$matches = array();
if (preg_match_all('~\{(.*?)\}~', $v, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
if (array_key_exists("{$match[1]}", $contextConfig)) {
$matchValue= $contextConfig["{$match[1]}"];
} else {
$matchValue= '';
}
$v= str_replace($match[0], $matchValue, $v);
}
}
$results['config'][$k]= $v;
$contextConfig[$k]= $v;
}
}
$results['config'] = array_merge($results['config'], $options);
if (!$this->getOption('transient_context', $contextConfig, false)) {
/* generate the aliasMap and resourceMap */
$collResources = $obj->getResourceCacheMap();
$results['resourceMap']= array ();
$results['aliasMap']= array ();
if ($collResources) {
/** @var Object $r */
while ($r = $collResources->fetch(PDO::FETCH_OBJ)) {
$results['resourceMap'][(string) $r->parent][] = (string) $r->id;
if ($this->modx->getOption('friendly_urls', $contextConfig, false)) {
if (array_key_exists($r->uri, $results['aliasMap'])) {
$this->modx->log(xPDO::LOG_LEVEL_ERROR, "Resource URI {$r->uri} already exists for resource id = {$results['aliasMap'][$r->uri]}; skipping duplicate resource URI for resource id = {$r->id}");
continue;
}
$results['aliasMap'][$r->uri]= $r->id;
}
}
}
/* generate the webLinkMap */
$collWebLinks = $obj->getWebLinkCacheMap();
$results['webLinkMap']= array();
if ($collWebLinks) {
while ($wl = $collWebLinks->fetch(PDO::FETCH_OBJ)) {
$results['webLinkMap'][$wl->id] = $wl->content;
}
}
/* generate the eventMap and pluginCache */
$results['eventMap'] = array();
$results['pluginCache'] = array();
$eventMap= $this->modx->getEventMap($obj->get('key'));
if (is_array ($eventMap) && !empty($eventMap)) {
$results['eventMap'] = $eventMap;
$pluginIds= array();
$plugins= array();
$this->modx->loadClass('modScript');
foreach ($eventMap as $pluginKeys) {
foreach ($pluginKeys as $pluginKey) {
if (isset ($pluginIds[$pluginKey])) {
continue;
}
$pluginIds[$pluginKey]= $pluginKey;
}
}
if (!empty($pluginIds)) {
$pluginQuery = $this->modx->newQuery('modPlugin', array('id:IN' => array_keys($pluginIds)), true);
$pluginQuery->select($this->modx->getSelectColumns('modPlugin', 'modPlugin'));
if ($pluginQuery->prepare() && $pluginQuery->stmt->execute()) {
$plugins= $pluginQuery->stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
if (!empty($plugins)) {
foreach ($plugins as $plugin) {
$results['pluginCache'][(string) $plugin['id']]= $plugin;
}
}
}
} //else {
//$results = $this->getOption("{$key}_results", $options, array());
//$cacheKey = "{$key}/context";
//$options['cache_context_settings'] = array_key_exists('cache_context_settings', $results) ? (boolean) $results : false;
//}
/* cache the Context ACL policies */
$results['policies'] = $obj->findPolicy($contextKey);
}
if ($this->getOption('cache_context_settings', $options, true) && is_array($results) && !empty($results)) {
$options[xPDO::OPT_CACHE_KEY] = $this->getOption('cache_context_settings_key', $options, 'context_settings');
$options[xPDO::OPT_CACHE_HANDLER] = $this->getOption('cache_context_settings_handler', $options, $this->getOption(xPDO::OPT_CACHE_HANDLER, $options));
$options[xPDO::OPT_CACHE_FORMAT] = (integer) $this->getOption('cache_context_settings_format', $options, $this->getOption(xPDO::OPT_CACHE_FORMAT, $options, xPDOCacheManager::CACHE_PHP));
$options[xPDO::OPT_CACHE_ATTEMPTS] = (integer) $this->getOption('cache_context_settings_attempts', $options, $this->getOption(xPDO::OPT_CACHE_ATTEMPTS, $options, 10));
$options[xPDO::OPT_CACHE_ATTEMPT_DELAY] = (integer) $this->getOption('cache_context_settings_attempt_delay', $options, $this->getOption(xPDO::OPT_CACHE_ATTEMPT_DELAY, $options, 1000));
$lifetime = (integer) $this->getOption('cache_context_settings_expires', $options, $this->getOption(xPDO::OPT_CACHE_EXPIRES, $options, 0));
if (!$this->set($cacheKey, $results, $lifetime, $options)) {
$this->modx->log(modX::LOG_LEVEL_ERROR, 'Could not cache context settings for ' . $key . '.');
}
}
return $results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment