Reordered the OPCache logic

main
Anthony 2025-06-17 01:06:56 +08:00
parent 458c89312e
commit 4b93c45b5a
1 changed files with 131 additions and 92 deletions

View File

@ -19,7 +19,7 @@ if ($mode !== '--simple') {
echo "PHP SAPI: " . php_sapi_name() . "\n"; echo "PHP SAPI: " . php_sapi_name() . "\n";
} }
// Check if OPCache is enabled // Check if OPCache is installed
if (!function_exists('opcache_get_status')) { if (!function_exists('opcache_get_status')) {
if ($mode === '--simple') { if ($mode === '--simple') {
echo "OPcache status: Disabled - OPcache is not installed"; echo "OPcache status: Disabled - OPcache is not installed";
@ -29,18 +29,7 @@ if (!function_exists('opcache_get_status')) {
exit(1); exit(1);
} }
// Get OPCache status // Get OPCache configuration first
$status = opcache_get_status(false);
if ($status === false) {
if ($mode === '--simple') {
echo "OPcache status: Error - Failed to get OPcache status";
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache status']);
}
exit(1);
}
// Get OPCache configuration
$config = opcache_get_configuration(); $config = opcache_get_configuration();
if ($config === false) { if ($config === false) {
if ($mode === '--simple') { if ($mode === '--simple') {
@ -51,11 +40,27 @@ if ($config === false) {
exit(1); exit(1);
} }
// Check if OPcache is enabled in configuration
$opcache_enabled = $config['directives']['opcache.enable'];
// Get OPCache status only if it's enabled
$status = null;
if ($opcache_enabled) {
$status = opcache_get_status(false);
if ($status === false) {
if ($mode === '--simple') {
echo "OPcache status: Error - Failed to get OPcache status";
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache status']);
}
exit(1);
}
}
// Handle different output modes // Handle different output modes
switch ($mode) { switch ($mode) {
case '--simple': case '--simple':
$enabled = $config['directives']['opcache.enable']; if ($opcache_enabled) {
if ($enabled) {
echo "OPcache status: Enabled"; echo "OPcache status: Enabled";
} else { } else {
echo "OPcache status: Disabled"; echo "OPcache status: Disabled";
@ -64,10 +69,19 @@ switch ($mode) {
case '--summary': case '--summary':
// Summary: Basic info + memory usage + key stats // Summary: Basic info + memory usage + key stats
if (!$opcache_enabled) {
$response = [ $response = [
'status' => 'success', 'status' => 'success',
'summary' => [ 'summary' => [
'enabled' => $config['directives']['opcache.enable'], 'enabled' => false,
'message' => 'OPcache is disabled. No runtime data available.'
]
];
} else {
$response = [
'status' => 'success',
'summary' => [
'enabled' => true,
'memory_usage' => [ 'memory_usage' => [
'used_mb' => round($status['memory_usage']['used_memory'] / 1024 / 1024, 2), 'used_mb' => round($status['memory_usage']['used_memory'] / 1024 / 1024, 2),
'free_mb' => round($status['memory_usage']['free_memory'] / 1024 / 1024, 2), 'free_mb' => round($status['memory_usage']['free_memory'] / 1024 / 1024, 2),
@ -84,14 +98,25 @@ switch ($mode) {
] ]
] ]
]; ];
}
echo json_encode($response, JSON_PRETTY_PRINT); echo json_encode($response, JSON_PRETTY_PRINT);
break; break;
case '--statistics': case '--statistics':
// Statistics: Detailed performance metrics // Statistics: Detailed performance metrics
if (!$opcache_enabled) {
$response = [ $response = [
'status' => 'success', 'status' => 'success',
'statistics' => [ 'statistics' => [
'enabled' => false,
'message' => 'OPcache is disabled. No statistics available.'
]
];
} else {
$response = [
'status' => 'success',
'statistics' => [
'enabled' => true,
'cache_performance' => [ 'cache_performance' => [
'hits' => $status['opcache_statistics']['hits'], 'hits' => $status['opcache_statistics']['hits'],
'misses' => $status['opcache_statistics']['misses'], 'misses' => $status['opcache_statistics']['misses'],
@ -117,11 +142,12 @@ switch ($mode) {
] ]
] ]
]; ];
}
echo json_encode($response, JSON_PRETTY_PRINT); echo json_encode($response, JSON_PRETTY_PRINT);
break; break;
case '--settings': case '--settings':
// Settings: Configuration directives only // Settings: Configuration directives only (always available)
$response = [ $response = [
'status' => 'success', 'status' => 'success',
'settings' => [ 'settings' => [
@ -157,10 +183,22 @@ switch ($mode) {
default: default:
// Full response (original functionality) // Full response (original functionality)
if (!$opcache_enabled) {
$response = [ $response = [
'status' => 'success', 'status' => 'success',
'data' => [ 'data' => [
'enabled' => $config['directives']['opcache.enable'], 'enabled' => false,
'message' => 'OPcache is disabled. Only configuration data is available.',
'configuration' => [
'directives' => $config['directives']
]
]
];
} else {
$response = [
'status' => 'success',
'data' => [
'enabled' => true,
'memory_usage' => [ 'memory_usage' => [
'used' => $status['memory_usage']['used_memory'], 'used' => $status['memory_usage']['used_memory'],
'free' => $status['memory_usage']['free_memory'], 'free' => $status['memory_usage']['free_memory'],
@ -188,6 +226,7 @@ switch ($mode) {
] ]
] ]
]; ];
}
echo json_encode($response, JSON_PRETTY_PRINT); echo json_encode($response, JSON_PRETTY_PRINT);
break; break;
} }