Skip to content
Snippets Groups Projects
Verified Commit 90cde162 authored by Alberto Paderno's avatar Alberto Paderno
Browse files

Issue #3463295: Replace the shutdown callback with hook_page_alter()

parent c32f471d
No related branches found
No related tags found
1 merge request!27Issue #3463295: Replace the shutdown callback with hook_page_alter()
Pipeline #232408 passed
......@@ -7,21 +7,6 @@
// phpcs:disable SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator.NullCoalesceOperatorNotUsed
/**
* Implements hook_init().
*/
function apc_init() {
global $user;
if (($user->uid == 0) || !variable_get('apc_show_debug', FALSE)
|| !user_access('access apc statistics') || strstr($_SERVER['PHP_SELF'], 'update.php')
|| strstr($_GET['q'], 'autocomplete')) {
return;
}
drupal_register_shutdown_function('apc_shutdown');
}
/**
* Implements hook_permission().
*/
......@@ -34,6 +19,42 @@ function apc_permission() {
);
}
/**
* Implements hook_page_alter().
*/
function apc_page_alter(&$page) {
if (variable_get('apc_show_debug', FALSE) && user_access('access apc statistics')) {
$operations = DrupalApcCache::operations();
$rows = array();
if (!empty($operations) && is_array($operations)) {
foreach ($operations as $row) {
if (is_array($row[2])) {
$row[2] = implode(',<br />', $row[2]);
}
$rows[] = $row;
}
}
$page['page_top']['apc_cache_info'] = array(
'#type' => 'markup',
'#prefix' => '<div class="apc-cache-info">',
'#suffix' => '</div>',
'#markup' => '<h2>' . t('APCu information') . '</h2>',
);
$page['page_bottom']['apc_cache_info']['operations'] = array(
'#type' => 'table',
'#header' => array(t('Operation'), t('Bin'), t('Cache IDs')),
'#rows' => $rows,
'#empty' => t('No APC cache operations have been performed.'),
);
drupal_page_is_cacheable(FALSE);
}
}
/**
* Implements hook_form_FORM_ID_alter() for system_performance_settings().
*/
......@@ -103,48 +124,3 @@ function apc_clear_cache($key, $bins = array()) {
return TRUE;
}
/**
* Shutdown function: Displays APC stats in the footer.
*/
function apc_shutdown() {
global $_apc_statistics;
// Don't call theme() during shutdown if the registry has been rebuilt (such
// as when enabling/disabling modules on admin/build/modules) as things break.
// Instead, simply exit without displaying admin statistics for this page
// load. See http://drupal.org/node/616282 for discussion.
if (!function_exists('theme_get_registry') || !theme_get_registry()) {
return;
}
// Try not to break non-HTML pages.
if (function_exists('drupal_get_http_header')) {
$header = drupal_get_http_header('content-type');
if ($header) {
$formats = array('xml', 'javascript', 'json', 'plain', 'image', 'application', 'csv', 'x-comma-separated-values');
foreach ($formats as $format) {
if (strstr($header, $format)) {
return;
}
}
}
}
if (isset($_apc_statistics) && is_array($_apc_statistics)) {
print '<div id="apc-devel"><h2>' . t('APC statistics') . '</h2>';
$rows = array();
foreach ($_apc_statistics as $row) {
if (is_array($row[2])) {
$row[2] = implode(',<br />', $row[2]);
}
$rows[] = $row;
}
print theme('table', array(
'header' => array(('Type'), t('Bin'), t('Cid(s)')),
'rows' => $rows,
));
print '</div>';
}
}
......@@ -22,6 +22,12 @@ class DrupalApcCache implements DrupalCacheInterface {
*/
protected static $pendingRequests = array();
/**
* The list of all the operations done to the cache.
*
* @var array
*/
protected static $operations = array();
/**
* The cache bin.
*
......@@ -167,8 +173,7 @@ protected function keyName($cid = NULL) {
* {@inheritdoc}
*/
public function get($cid) {
// Add a get to our statistics.
$GLOBALS['_apc_statistics'][] = array('get', $this->bin, array($cid));
$this->operations(array('get()', $this->bin, array($cid)));
return $this->prepareItem(apcu_fetch($this->keyName($cid)));
}
......@@ -196,7 +201,7 @@ public function getMultiple(&$cids) {
$cache = array();
// Add a get to our statistics.
$GLOBALS['_apc_statistics'][] = array('get', $this->bin, $cids);
$this->operations(array('getMultiple()', $this->bin, $cids));
if (!$cids) {
return $cache;
......@@ -220,7 +225,7 @@ public function getMultiple(&$cids) {
*/
public function set($cid, $data, $expire = CACHE_PERMANENT) {
// Add set to statistics.
$GLOBALS['_apc_statistics'][] = array('set', $this->bin, $cid);
$this->operations(array('set()', $this->bin, $cid));
// Create new cache object.
$cache = new stdClass();
......@@ -259,7 +264,7 @@ public function set($cid, $data, $expire = CACHE_PERMANENT) {
* @param string $prefix
* The prefix for the cache IDs to delete.
*/
protected function deleteKeys($prefix = '') {
protected function deleteKeys($prefix = NULL) {
if (class_exists('APCUIterator')) {
$escaped_key = preg_quote($this->keyName($prefix), '/');
$iterator = new APCUIterator("/^$escaped_key/", APC_ITER_KEY);
......@@ -280,7 +285,7 @@ public function clear($cid = NULL, $wildcard = FALSE) {
}
// Add a get to our statistics.
$GLOBALS['_apc_statistics'][] = array('clear', $this->bin, $cid, (int) $wildcard);
$this->operations(array('clear()', $this->bin, $cid, (int) $wildcard));
if (empty($cid)) {
$this->deleteKeys();
......@@ -332,4 +337,21 @@ public static function pendingRequests() {
return self::$pendingRequests;
}
/**
* Adds a new operation to the list of operations, or returns the list.
*
* @param array|null $new_operation
* The new operation to add.
*
* @return array
* The list of cache operations.
*/
public static function operations($new_operation = NULL) {
if (!is_null($new_operation)) {
self::$operations[] = $new_operation;
}
return self::$operations;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment