2025-06-13 17:17:12 +00:00
|
|
|
<?php
|
2025-06-13 17:23:54 +00:00
|
|
|
// Get the action from command line arguments
|
2025-06-13 17:17:12 +00:00
|
|
|
$action = $argv[1] ?? 'status';
|
|
|
|
|
2025-06-13 17:23:54 +00:00
|
|
|
// Check if OPCache is installed
|
|
|
|
if (!function_exists('opcache_get_status')) {
|
|
|
|
echo json_encode(['status' => 'error', 'message' => 'OPCache is not installed']);
|
2025-06-13 17:17:12 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2025-06-13 17:23:54 +00:00
|
|
|
// Function to get current OPCache status
|
|
|
|
function get_opcache_status() {
|
|
|
|
$status = opcache_get_status(false);
|
|
|
|
if ($status === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function to get OPCache configuration
|
|
|
|
function get_opcache_config() {
|
|
|
|
$config = opcache_get_configuration();
|
|
|
|
if ($config === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $config;
|
2025-06-13 17:17:12 +00:00
|
|
|
}
|
|
|
|
|
2025-06-13 17:23:54 +00:00
|
|
|
// Handle different actions
|
2025-06-13 17:17:12 +00:00
|
|
|
switch ($action) {
|
|
|
|
case 'enable':
|
2025-06-13 17:23:54 +00:00
|
|
|
// Enable OPCache
|
|
|
|
if (!ini_set('opcache.enable', '1')) {
|
|
|
|
echo json_encode(['status' => 'error', 'message' => 'Failed to enable OPCache']);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
$message = 'OPCache enabled successfully';
|
2025-06-13 17:17:12 +00:00
|
|
|
break;
|
2025-06-13 17:23:54 +00:00
|
|
|
|
2025-06-13 17:17:12 +00:00
|
|
|
case 'disable':
|
2025-06-13 17:23:54 +00:00
|
|
|
// Disable OPCache
|
|
|
|
if (!ini_set('opcache.enable', '0')) {
|
|
|
|
echo json_encode(['status' => 'error', 'message' => 'Failed to disable OPCache']);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
$message = 'OPCache disabled successfully';
|
2025-06-13 17:17:12 +00:00
|
|
|
break;
|
2025-06-13 17:23:54 +00:00
|
|
|
|
|
|
|
case 'status':
|
2025-06-13 17:17:12 +00:00
|
|
|
default:
|
2025-06-13 17:23:54 +00:00
|
|
|
// Get current status
|
|
|
|
$status = get_opcache_status();
|
|
|
|
$config = get_opcache_config();
|
|
|
|
|
|
|
|
if ($status === false || $config === false) {
|
|
|
|
echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache status']);
|
|
|
|
exit(1);
|
|
|
|
}
|
2025-06-13 17:17:12 +00:00
|
|
|
|
2025-06-13 17:23:54 +00:00
|
|
|
echo json_encode([
|
|
|
|
'status' => 'success',
|
|
|
|
'data' => [
|
|
|
|
'enabled' => $config['directives']['opcache.enable'],
|
|
|
|
'memory_usage' => [
|
|
|
|
'used' => $status['memory_usage']['used_memory'],
|
|
|
|
'free' => $status['memory_usage']['free_memory'],
|
|
|
|
'wasted' => $status['memory_usage']['wasted_memory']
|
|
|
|
],
|
|
|
|
'statistics' => [
|
|
|
|
'num_cached_scripts' => $status['opcache_statistics']['num_cached_scripts'],
|
|
|
|
'hits' => $status['opcache_statistics']['hits'],
|
|
|
|
'misses' => $status['opcache_statistics']['misses']
|
|
|
|
]
|
|
|
|
]
|
|
|
|
], JSON_PRETTY_PRINT);
|
|
|
|
exit(0);
|
2025-06-13 17:17:12 +00:00
|
|
|
}
|
|
|
|
|
2025-06-13 17:23:54 +00:00
|
|
|
// Get status after action
|
|
|
|
$status = get_opcache_status();
|
|
|
|
$config = get_opcache_config();
|
|
|
|
|
|
|
|
if ($status === false || $config === false) {
|
|
|
|
echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache status after action']);
|
2025-06-13 17:17:12 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2025-06-13 17:23:54 +00:00
|
|
|
echo json_encode([
|
|
|
|
'status' => 'success',
|
|
|
|
'message' => $message,
|
|
|
|
'data' => [
|
|
|
|
'enabled' => $config['directives']['opcache.enable'],
|
|
|
|
'memory_usage' => [
|
|
|
|
'used' => $status['memory_usage']['used_memory'],
|
|
|
|
'free' => $status['memory_usage']['free_memory'],
|
|
|
|
'wasted' => $status['memory_usage']['wasted_memory']
|
|
|
|
],
|
|
|
|
'statistics' => [
|
|
|
|
'num_cached_scripts' => $status['opcache_statistics']['num_cached_scripts'],
|
|
|
|
'hits' => $status['opcache_statistics']['hits'],
|
|
|
|
'misses' => $status['opcache_statistics']['misses']
|
|
|
|
]
|
|
|
|
]
|
|
|
|
], JSON_PRETTY_PRINT);
|