From 44e3c3987d3e957182dd852390d2d95c81f5cc16 Mon Sep 17 00:00:00 2001 From: Andrew Collington Date: Sun, 23 Nov 2014 01:07:38 +0000 Subject: [PATCH] Tidied up code, started to revamp html --- index.php | 611 +++++++++++++++++++++++++++++------------------------- 1 file changed, 324 insertions(+), 287 deletions(-) diff --git a/index.php b/index.php index edf1ce4..16b179a 100644 --- a/index.php +++ b/index.php @@ -2,14 +2,14 @@ /** * OPcache GUI - * + * * A simple but effective single-file GUI for the OPcache PHP extension. - * + * * @author Andrew Collington, andy@amnuts.com * @license MIT, http://acollington.mit-license.org/ */ -if (!function_exists('opcache_get_status')) { +if (!extension_loaded('Zend OPcache')) { die('The Zend OPcache extension does not appear to be installed'); } @@ -20,107 +20,138 @@ $settings = array( 'allow_invalidate' => true ); - -$validPages = array('overview', 'files', 'reset', 'invalidate'); -$page = (empty($_GET['page']) || !in_array($_GET['page'], $validPages) - ? 'overview' - : strtolower($_GET['page']) -); - -if ($page == 'reset') { - opcache_reset(); - header('Location: ?page=overview'); - exit; -} - -if ($page == 'invalidate') { - $file = (isset($_GET['file']) ? trim($_GET['file']) : null); - if (!$settings['allow_invalidate'] || !function_exists('opcache_invalidate') || empty($file)) { - header('Location: ?page=files&error=1'); - exit; - } - $success = (int)opcache_invalidate(urldecode($file), true); - header("Location: ?page=files&success={$success}"); - exit; -} - -$opcache_config = opcache_get_configuration(); -$opcache_status = opcache_get_status(); -$opcache_funcs = get_extension_funcs('Zend OPcache'); - -if (!empty($opcache_status['scripts'])) { - uasort($opcache_status['scripts'], function($a, $b) { - return $a['hits'] < $b['hits']; - }); -} - -function memsize($size, $precision = 3, $space = false) +class OpCacheService { - $i = 0; - $val = array(' bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); - while (($size / 1024) > 1) { - $size /= 1024; - ++$i; - } - return sprintf("%.{$precision}f%s%s", - $size, (($space && $i) ? ' ' : ''), $val[$i]); -} + protected $config; + protected $status; + protected $functions; -function rc($at = null) -{ - static $i = 0; - if ($at !== null) { - $i = $at; - } else { - echo (++$i % 2 ? 'even' : 'odd'); + private function __construct() + { + $this->data = $this->compileState(); + } + + public static function init() + { + $self = new self; + if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) + && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' + ) { + echo json_encode($self->getData(@$_GET['section'] ?: null)); + exit; + } else if ((isset($_GET['reset']))) { + $self->resetCache(); + } else if ((isset($_GET['invalidate']))) { + $self->resetCache($_GET['invalidate']); + } + return $self; + } + + public function getData($section = null) + { + if ($section === null) { + return $this->data; + } + $section = strtolower($section); + return (isset($this->data[$section]) + ? $this->data[$section] + : null + ); + } + + public function resetCache($file = null) + { + $success = false; + if ($file === null) { + $success = opcache_reset(); + } else if (function_exists('opcache_invalidate')) { + $success = opcache_invalidate(urldecode($file), true); + } + if ($success) { + $this->compileState(); + } + return $success; + } + + protected function compileState() + { + $config = opcache_get_configuration(); + $memsize = function($size, $precision = 3, $space = false) + { + $i = 0; + $val = array(' bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); + while (($size / 1024) > 1) { + $size /= 1024; + ++$i; + } + return sprintf("%.{$precision}f%s%s", $size, (($space && $i) ? ' ' : ''), $val[$i]); + }; + + $status = opcache_get_status(); + if (!empty($status['scripts'])) { + uasort($status['scripts'], function($a, $b) { + return $a['hits'] < $b['hits']; + }); + $this->data['files'] = $status['scripts']; + } + + $overview = array_merge( + $status['memory_usage'], $status['opcache_statistics'], [ + 'used_memory_percentage' => round(100 * ( + ($status['memory_usage']['used_memory'] + $status['memory_usage']['wasted_memory']) + / $config['directives']['opcache.memory_consumption'])), + 'hit_rate_percentage' => round($status['opcache_statistics']['opcache_hit_rate']), + 'wasted_percentage' => round($status['memory_usage']['current_wasted_percentage'], 2), + 'readable' => [ + 'total_memory' => $memsize($config['directives']['opcache.memory_consumption']), + 'used_memory' => $memsize($status['memory_usage']['used_memory']), + 'free_memory' => $memsize($status['memory_usage']['free_memory']), + 'wasted_memory' => $memsize($status['memory_usage']['wasted_memory']), + 'num_cached_scripts' => number_format($status['opcache_statistics']['num_cached_scripts']), + 'hits' => number_format($status['opcache_statistics']['hits']), + 'misses' => number_format($status['opcache_statistics']['misses']), + 'blacklist_miss' => number_format($status['opcache_statistics']['blacklist_misses']), + 'num_cached_keys' => number_format($status['opcache_statistics']['num_cached_keys']), + 'max_cached_keys' => number_format($status['opcache_statistics']['max_cached_keys']), + 'start_time' => date_format(date_create("@{$status['opcache_statistics']['start_time']}"), 'Y-m-d H:i:s'), + 'last_restart_time' => ($status['opcache_statistics']['last_restart_time'] == 0 + ? 'never' + : date_format(date_create("@{$status['opcache_statistics']['last_restart_time']}"), 'Y-m-d H:i:s') + ) + ] + ] + ); + + ksort($config['directives']); + $version = array_merge( + $config['version'], + [ + 'php' => phpversion(), + 'server' => $_SERVER['SERVER_SOFTWARE'], + 'host' => (function_exists('gethostname') + ? gethostname() + : (php_uname('n') + ?: (empty($_SERVER['SERVER_NAME']) + ? $_SERVER['HOST_NAME'] + : $_SERVER['SERVER_NAME'] + ) + ) + ) + ] + ); + + return [ + 'version' => $version, + 'overview' => $overview, + 'files' => $status['scripts'], + 'directives' => $config['directives'], + 'blacklist' => $config['blacklist'], + 'functions' => get_extension_funcs('Zend OPcache') + ]; } } -$data = array_merge( - $opcache_status['memory_usage'], - $opcache_status['opcache_statistics'], - array( - 'total_memory_size' => memsize($opcache_config['directives']['opcache.memory_consumption']), - 'used_memory_percentage' => round(100 * ( - ($opcache_status['memory_usage']['used_memory'] + $opcache_status['memory_usage']['wasted_memory']) - / $opcache_config['directives']['opcache.memory_consumption'])), - 'hit_rate_percentage' => round($opcache_status['opcache_statistics']['opcache_hit_rate']), - 'wasted_percentage' => round($opcache_status['memory_usage']['current_wasted_percentage'], 2), - 'used_memory_size' => memsize($opcache_status['memory_usage']['used_memory']), - 'free_memory_size' => memsize($opcache_status['memory_usage']['free_memory']), - 'wasted_memory_size' => memsize($opcache_status['memory_usage']['wasted_memory']), - 'files_cached' => number_format($opcache_status['opcache_statistics']['num_cached_scripts']), - 'hits_size' => number_format($opcache_status['opcache_statistics']['hits']), - 'miss_size' => number_format($opcache_status['opcache_statistics']['misses']), - 'blacklist_miss_size' => number_format($opcache_status['opcache_statistics']['blacklist_misses']), - 'num_cached_keys_size' => number_format($opcache_status['opcache_statistics']['num_cached_keys']), - 'max_cached_keys_size' => number_format($opcache_status['opcache_statistics']['max_cached_keys']), - ) -); - -$threshold = ''; -if ($data['used_memory_percentage'] >= $settings['used_memory_percentage_high_threshold']) { - $threshold = ' high'; -} elseif ($data['used_memory_percentage'] >= $settings['used_memory_percentage_mid_threshold']) { - $threshold = ' mid'; -} - -if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) - && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' -) { - echo json_encode($data); - exit; -} - -$host = (function_exists('gethostname') - ? gethostname() - : (php_uname('n') - ?: (empty($_SERVER['SERVER_NAME']) - ? $_SERVER['HOST_NAME'] - : $_SERVER['SERVER_NAME'] - ) - ) -); +$opcache = OpCacheService::init(); ?> @@ -129,38 +160,52 @@ $host = (function_exists('gethostname') -