-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.php
More file actions
175 lines (154 loc) · 6.09 KB
/
Copy pathinit.php
File metadata and controls
175 lines (154 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* Плагін перегляду кешу - ініціалізація
*/
declare(strict_types=1);
$rootDir = defined('ROOT_DIR') ? ROOT_DIR : dirname(__DIR__, 2);
require_once $rootDir . '/engine/core/support/base/BasePlugin.php';
require_once $rootDir . '/engine/core/support/helpers/UrlHelper.php';
if (! function_exists('addHook')) {
require_once $rootDir . '/engine/includes/functions.php';
}
// Завантажуємо ClassAutoloader для реєстрації класів
if (file_exists($rootDir . '/engine/core/system/ClassAutoloader.php')) {
require_once $rootDir . '/engine/core/system/ClassAutoloader.php';
}
class CacheViewPlugin extends BasePlugin
{
private string $pluginDir;
public function __construct()
{
parent::__construct();
$reflection = new ReflectionClass($this);
$this->pluginDir = dirname($reflection->getFileName());
}
public function init(): void
{
addHook('admin_register_routes', [$this, 'registerAdminRoute'], 10, 1);
addFilter('admin_menu', [$this, 'registerAdminMenu'], 15);
addFilter('settings_categories', [$this, 'registerSettingsCategory'], 10);
// Підписуємось на хуки кешу для відстеження статистики
addHook('cache_get', [$this, 'trackCacheAccess'], 10, 1);
}
/**
* Відстеження доступу до кешу
*/
public function trackCacheAccess(string $key): void
{
try {
$trackerFile = $this->pluginDir . '/src/Services/CacheStatsTracker.php';
if (!file_exists($trackerFile)) {
return;
}
if (!class_exists('CacheStatsTracker', false)) {
require_once $trackerFile;
}
$cacheDir = defined('CACHE_DIR') ? CACHE_DIR : (defined('ROOT_DIR') ? ROOT_DIR . '/storage/cache' : null);
if ($cacheDir) {
$tracker = new \CacheStatsTracker($cacheDir);
$tracker->trackAccess(md5($key));
}
} catch (\Throwable $e) {
// Тихо ігноруємо помилки
}
}
public function registerAdminRoute($router): void
{
$pageFile = $this->pluginDir . '/src/admin/pages/CacheViewAdminPage.php';
if (file_exists($pageFile)) {
// Реєструємо клас в автозавантажувачі
if (isset($GLOBALS['engineAutoloader'])) {
$autoloader = $GLOBALS['engineAutoloader'];
if ($autoloader instanceof ClassAutoloader ||
(is_object($autoloader) && method_exists($autoloader, 'addClassMap'))) {
$autoloader->addClassMap([
'CacheViewAdminPage' => $pageFile
]);
}
}
require_once $pageFile;
if (class_exists('CacheViewAdminPage')) {
$router->add(['GET', 'POST'], 'cache-view', 'CacheViewAdminPage');
}
}
}
/**
* @param array<int, array<string, mixed>> $menu
*/
public function registerAdminMenu(array $menu): array
{
// Шукаємо існуюче меню "Система"
$found = false;
foreach ($menu as &$item) {
if (isset($item['page']) && $item['page'] === 'system') {
if (!isset($item['submenu'])) {
$item['submenu'] = [];
}
$item['submenu'][] = [
'text' => 'Кеш',
'icon' => 'fas fa-database',
'href' => UrlHelper::admin('cache-view'),
'page' => 'cache-view',
'order' => 10,
'permission' => 'admin.cache.view',
];
$found = true;
break;
}
}
// Якщо меню "Система" не знайдено, створюємо його
if (!$found) {
$menu[] = [
'text' => 'Система',
'icon' => 'fas fa-server',
'href' => '#',
'page' => 'system',
'order' => 60,
'permission' => null,
'submenu' => [
[
'text' => 'Кеш',
'icon' => 'fas fa-database',
'href' => UrlHelper::admin('cache-view'),
'page' => 'cache-view',
'order' => 10,
'permission' => 'admin.cache.view',
],
],
];
}
return $menu;
}
/**
* Реєстрація в категоріях налаштувань
*
* Додає плагін до категорії "Система" на сторінці /admin/settings
*
* @param array<string, mixed> $categories Поточні категорії
* @return array<string, mixed> Оновлені категорії
*/
public function registerSettingsCategory(array $categories): array
{
// Перевіряємо, чи плагін активний
$pluginManager = function_exists('pluginManager') ? pluginManager() : null;
if (!$pluginManager || !method_exists($pluginManager, 'isPluginActive') || !$pluginManager->isPluginActive('cache-view')) {
return $categories;
}
// Додаємо до категорії "Система"
if (isset($categories['system'])) {
$categories['system']['items'][] = [
'title' => 'Кеш',
'description' => 'Управління кешем',
'url' => UrlHelper::admin('cache-view'),
'icon' => 'fas fa-database',
'permission' => 'admin.cache.view',
];
}
return $categories;
}
public function install(): void
{
// Немає таблиць для встановлення
}
}
return new CacheViewPlugin();