Bearsampp 2026.7.11
Loading...
Searching...
No Matches
class.module.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2024 Bearsampp
4 * License: GNU General Public License version 3 or later; see LICENSE.txt
5 * Author: Bear
6 * Website: https://bearsampp.com
7 * Github: https://github.com/Bearsampp
8 */
9
14abstract class Module
15{
16 const BUNDLE_RELEASE = 'bundleRelease';
17
18 private static $configCache = array();
19
20 private $type;
21 private $id;
22
23 protected $name;
24 protected $version;
25 protected $release = 'N/A';
26
27 public $rootPath;
30 protected $enable;
31 protected $bearsamppConf;
33
38 protected function __construct() {
39 // Initialization logic can be added here if needed
40 }
41
48 protected function reload($id = null, $type = null) {
49 global $bearsamppRoot;
50
51 $this->id = empty($id) ? $this->id : $id;
52 $this->type = empty($type) ? $this->type : $type;
53 $mainPath = 'N/A';
54
55 switch ($this->type) {
56 case Apps::TYPE:
57 $mainPath = Path::getAppsPath();
58 break;
59 case Bins::TYPE:
60 $mainPath = Path::getBinPath();
61 break;
62 case Tools::TYPE:
63 $mainPath = Path::getToolsPath();
64 break;
65 }
66
67 $this->rootPath = $mainPath . '/' . $this->id;
68 $this->currentPath = $this->rootPath . '/' . $this->id . $this->version;
69 $this->symlinkPath = $this->rootPath . '/current';
70 $this->enable = is_dir($this->currentPath);
71 $this->bearsamppConf = $this->currentPath . '/bearsampp.conf';
72
73 $cacheKey = md5($this->bearsamppConf);
74 if (!isset(self::$configCache[$cacheKey])) {
75 // CacheManager handles both disk cache and parsing
76 $data = CacheManager::load(
77 $this->bearsamppConf,
78 function($path) { return @parse_ini_file($path) ?: []; },
79 $cacheKey
80 );
81
82 if (!empty($data)) {
83 $this->bearsamppConfRaw = $data;
84 self::$configCache[$cacheKey] = $this->bearsamppConfRaw;
85 } else {
86 $this->bearsamppConfRaw = [];
87 // Do not cache empty results in memory to allow retry on next reload
88 }
89 } else {
90 $this->bearsamppConfRaw = self::$configCache[$cacheKey];
91 }
92
95 }
96 }
97
98
105 protected function replace($key, $value) {
106 $this->replaceAll(array($key => $value));
107 }
108
114 protected function replaceAll($params) {
115 $content = file_get_contents($this->bearsamppConf);
116
117 foreach ($params as $key => $value) {
118 $content = preg_replace('|' . $key . ' = .*|', $key . ' = ' . '"' . $value.'"', $content);
119 $this->bearsamppConfRaw[$key] = $value;
120 }
121
122 file_put_contents($this->bearsamppConf, $content);
123
124 // Invalidate both memory cache and disk cache
125 self::invalidateConfigCacheForPath($this->bearsamppConf);
126 }
127
134 public static function invalidateConfigCacheForPath($sourcePath) {
135 $cacheKey = md5($sourcePath);
136 unset(self::$configCache[$cacheKey]);
137 CacheManager::invalidate($sourcePath);
138 }
139
143 public static function clearMemoryCache() {
144 self::$configCache = array();
145 }
146
153 public function update($sub = 0, $showWindow = false) {
154 $this->updateConfig(null, $sub, $showWindow);
155 }
156
164 protected function updateConfig($version = null, $sub = 0, $showWindow = false) {
165 $version = $version == null ? $this->version : $version;
166 Log::debug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'Update ' . $this->name . ' ' . $version . ' config');
167 }
168
174 public function __toString() {
175 return $this->getName();
176 }
177
183 public function getType() {
184 return $this->type;
185 }
186
192 public function getId() {
193 return $this->id;
194 }
195
201 public function getName() {
202 return $this->name;
203 }
204
210 public function getVersion() {
211 return $this->version;
212 }
213
219 public function getVersionList() {
220 return Util::getVersionList($this->rootPath);
221 }
222
228 abstract public function setVersion($version);
229
235 public function getRelease() {
236 return $this->release;
237 }
238
239
245 public function isEnable() {
246 return $this->enable;
247 }
248
249}
global $bearsamppRoot
const TYPE
const TYPE
static load(string $sourcePath, callable $parser, ?string $cacheKey=null)
static invalidate(string $sourcePath)
static debug($data, $file=null)
static $configCache
replaceAll($params)
update($sub=0, $showWindow=false)
static clearMemoryCache()
updateConfig($version=null, $sub=0, $showWindow=false)
const BUNDLE_RELEASE
setVersion($version)
static invalidateConfigCacheForPath($sourcePath)
replace($key, $value)
reload($id=null, $type=null)
static getToolsPath($aetrayPath=false)
static getBinPath($aetrayPath=false)
static getAppsPath($aetrayPath=false)
const TYPE
static getVersionList($path)