|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of phpCacheAdmin. |
| 4 | + * |
| 5 | + * Copyright (c) Róbert Kelčák (https://kelcak.com/) |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +declare(strict_types=1); |
| 12 | + |
| 13 | +namespace RobiNN\Pca\Dashboards\APCu; |
| 14 | + |
| 15 | +use RobiNN\Pca\Dashboards\DashboardInterface; |
| 16 | +use RobiNN\Pca\Helpers; |
| 17 | +use RobiNN\Pca\Template; |
| 18 | + |
| 19 | +class APCuDashboard implements DashboardInterface { |
| 20 | + use APCuTrait; |
| 21 | + |
| 22 | + private Template $template; |
| 23 | + |
| 24 | + public function __construct(Template $template) { |
| 25 | + $this->template = $template; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Check if an extension is installed. |
| 30 | + * |
| 31 | + * @return bool |
| 32 | + */ |
| 33 | + public function check(): bool { |
| 34 | + return extension_loaded('apcu'); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Get dashboard info. |
| 39 | + * |
| 40 | + * @return array<string, string> |
| 41 | + */ |
| 42 | + public function getDashboardInfo(): array { |
| 43 | + return [ |
| 44 | + 'key' => 'apcu', |
| 45 | + 'icon' => 'opcache', // it doesn't have its own logo.. |
| 46 | + 'title' => 'APCu', |
| 47 | + 'color' => 'slate', |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Ajax content. |
| 53 | + * |
| 54 | + * @return string |
| 55 | + */ |
| 56 | + public function ajax(): string { |
| 57 | + $return = ''; |
| 58 | + |
| 59 | + if (isset($_GET['deleteall']) && apcu_clear_cache()) { |
| 60 | + $return = $this->template->render('components/alert', [ |
| 61 | + 'message' => 'Cache has been cleaned.', |
| 62 | + ]); |
| 63 | + } |
| 64 | + |
| 65 | + if (isset($_GET['delete'])) { |
| 66 | + $return = $this->deletekey(); |
| 67 | + } |
| 68 | + |
| 69 | + return $return; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Data for info panels. |
| 74 | + * |
| 75 | + * @return array<string, mixed> |
| 76 | + */ |
| 77 | + public function info(): array { |
| 78 | + $info = apcu_cache_info(); |
| 79 | + |
| 80 | + $memory_info = apcu_sma_info(); |
| 81 | + |
| 82 | + $total_memory = $memory_info['num_seg'] * $memory_info['seg_size']; |
| 83 | + $memory_used = ($memory_info['num_seg'] * $memory_info['seg_size']) - $memory_info['avail_mem']; |
| 84 | + |
| 85 | + $hit_rate = (int) $info['num_hits'] !== 0 ? $info['num_hits'] / ($info['num_hits'] + $info['num_misses']) : 0; |
| 86 | + |
| 87 | + return [ |
| 88 | + 'panels' => [ |
| 89 | + [ |
| 90 | + 'title' => 'Status', |
| 91 | + 'moreinfo' => true, |
| 92 | + 'data' => [ |
| 93 | + 'Start time' => Helpers::formatTime($info['start_time']), |
| 94 | + 'Cache full count' => $info['expunges'], |
| 95 | + ], |
| 96 | + ], |
| 97 | + [ |
| 98 | + 'title' => 'Memory', |
| 99 | + 'data' => [ |
| 100 | + 'Total' => Helpers::formatBytes((int) $total_memory), |
| 101 | + 'Used' => Helpers::formatBytes((int) $memory_used), |
| 102 | + 'Free' => Helpers::formatBytes((int) $memory_info['avail_mem']), |
| 103 | + ], |
| 104 | + ], |
| 105 | + [ |
| 106 | + 'title' => 'Stats', |
| 107 | + 'data' => [ |
| 108 | + 'Cached scripts' => $info['num_entries'], |
| 109 | + 'Hits' => Helpers::formatNumber((int) $info['num_hits']), |
| 110 | + 'Misses' => Helpers::formatNumber((int) $info['num_misses']), |
| 111 | + 'Hit rate' => round($hit_rate * 100).'%', |
| 112 | + ], |
| 113 | + ], |
| 114 | + ], |
| 115 | + ]; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Show info panels. |
| 120 | + * |
| 121 | + * @return string |
| 122 | + */ |
| 123 | + public function showPanels(): string { |
| 124 | + if (isset($_GET['moreinfo']) || isset($_GET['form']) || isset($_GET['view'], $_GET['key'])) { |
| 125 | + return ''; |
| 126 | + } |
| 127 | + |
| 128 | + return $this->template->render('partials/info', [ |
| 129 | + 'title' => 'APCu', |
| 130 | + 'extension_version' => phpversion('apcu'), |
| 131 | + 'info' => $this->info(), |
| 132 | + ]); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Dashboard content. |
| 137 | + * |
| 138 | + * @return string |
| 139 | + */ |
| 140 | + public function dashboard(): string { |
| 141 | + $info = apcu_cache_info(); |
| 142 | + |
| 143 | + if (isset($_GET['moreinfo'])) { |
| 144 | + $return = $this->moreInfo($info); |
| 145 | + } elseif (isset($_GET['view']) && !empty($_GET['key'])) { |
| 146 | + $return = $this->viewKey(); |
| 147 | + } elseif (isset($_GET['form'])) { |
| 148 | + $return = $this->form(); |
| 149 | + } else { |
| 150 | + $return = $this->mainDashboard($info); |
| 151 | + } |
| 152 | + |
| 153 | + return $return; |
| 154 | + } |
| 155 | +} |
0 commit comments