From 01368bbfce831cd8949500ffdf5f5e4614316459 Mon Sep 17 00:00:00 2001 From: Nikita Kiselev Date: Sun, 19 Oct 2025 14:34:28 +0300 Subject: [PATCH] feat(admin): remove old assets --- .../controller/extension/module/tgshop.php | 70 +++++++++++++++++-- 1 file changed, 65 insertions(+), 5 deletions(-) diff --git a/module/oc_telegram_shop/upload/admin/controller/extension/module/tgshop.php b/module/oc_telegram_shop/upload/admin/controller/extension/module/tgshop.php index c4167c2..e83a5aa 100755 --- a/module/oc_telegram_shop/upload/admin/controller/extension/module/tgshop.php +++ b/module/oc_telegram_shop/upload/admin/controller/extension/module/tgshop.php @@ -94,6 +94,7 @@ class ControllerExtensionModuleTgshop extends Controller if ($hasConfig) { $this->updateConfigFromDefaults(); + $this->cleanUpOldAssets(); $this->config(); } else { $this->init(); @@ -132,7 +133,6 @@ class ControllerExtensionModuleTgshop extends Controller foreach ($data['settings'] as $configs) { foreach ($configs as $key => $config) { if ($config['type'] === 'image') { - if (isset($this->request->post[$key]) && is_file(DIR_IMAGE . $this->request->post[$key])) { $data[$key] = $this->model_tool_image->resize($this->request->post[$key], 100, 100); } elseif ($this->config->get($key) && is_file(DIR_IMAGE . $this->config->get($key))) { @@ -225,10 +225,10 @@ class ControllerExtensionModuleTgshop extends Controller ]); - $app->bind(OcRegistryDecorator::class, fn () => new OcRegistryDecorator($this->registry)); + $app->bind(OcRegistryDecorator::class, fn() => new OcRegistryDecorator($this->registry)); $app - ->withLogger(fn () => new OpenCartLogAdapter($this->log, 'TeleCartAdmin')) + ->withLogger(fn() => new OpenCartLogAdapter($this->log, 'TeleCartAdmin')) ->bootAndHandleRequest(); } @@ -509,7 +509,7 @@ HTML, $map = []; foreach ($this->model_customer_customer_group->getCustomerGroups() as $customer_group) { - $map[(int)$customer_group['customer_group_id']] = $customer_group['name']; + $map[(int) $customer_group['customer_group_id']] = $customer_group['name']; } return $map; @@ -521,7 +521,7 @@ HTML, $map = []; foreach ($statuses as $item) { - $map[(int)$item['order_status_id']] = $item['name']; + $map[(int) $item['order_status_id']] = $item['name']; } return $map; @@ -550,4 +550,64 @@ HTML, } } } + + private function cleanUpOldAssets(): void + { + $spaPath = rtrim(DIR_IMAGE, '/') . '/catalog/tgshopspa'; + $assetsPath = $spaPath . '/assets'; + $manifestPath = $spaPath . '/manifest.json'; + if (! file_exists($manifestPath)) { + $this->log->write('[TELECART] Файл manifest.json не найден — очистка пропущена.'); + + return; + } + + try { + $contents = json_decode(file_get_contents($manifestPath), true, 512, JSON_THROW_ON_ERROR); + + $entry = $contents['index.html'] ?? null; + if (! $entry) { + throw new RuntimeException('[TELECART] Некорректный manifest.json — отсутствует ключ index.html.'); + } + + $keep = [$entry['file']]; + if (! empty($entry['css'])) { + foreach ($entry['css'] as $css) { + $keep[] = $css; + } + } + + $deletedFiles = 0; + $keptFiles = 0; + + foreach (glob($assetsPath . '/*') as $file) { + $ext = pathinfo($file, PATHINFO_EXTENSION); + if (! in_array($ext, ['js', 'css'])) { + continue; + } + $relative = 'assets/' . basename($file); + if (in_array($relative, $keep, true)) { + $keptFiles++; + continue; + } + + if (is_file($file)) { + unlink($file); + $deletedFiles++; + } + } + + $this->log->write( + sprintf( + '[TELECART] Очистка assets завершена. Удалено: %d, оставлено: %d', + $deletedFiles, + $keptFiles + ) + ); + } catch (JsonException $e) { + $this->log->write('[TELECART] Ошибка декодирования файла manifest.json: ' . $e->getMessage()); + } catch (Exception $e) { + $this->log->write('[TELECART] Ошибка удаления старых assets: ' . $e->getMessage()); + } + } }