mb-admin/scripts/toggle_opcache.php

104 lines
3.2 KiB
PHP

<?php
// Get the action from command line arguments
$action = $argv[1] ?? 'status';
// Check if OPCache is installed
if (!function_exists('opcache_get_status')) {
echo json_encode(['status' => 'error', 'message' => 'OPCache is not installed']);
exit(1);
}
// 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;
}
// Handle different actions
switch ($action) {
case 'enable':
// Enable OPCache
if (!ini_set('opcache.enable', '1')) {
echo json_encode(['status' => 'error', 'message' => 'Failed to enable OPCache']);
exit(1);
}
$message = 'OPCache enabled successfully';
break;
case 'disable':
// Disable OPCache
if (!ini_set('opcache.enable', '0')) {
echo json_encode(['status' => 'error', 'message' => 'Failed to disable OPCache']);
exit(1);
}
$message = 'OPCache disabled successfully';
break;
case 'status':
default:
// 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);
}
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);
}
// 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']);
exit(1);
}
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);