2024.8.23
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
10/**
11 * Abstract class representing a module in the Bearsampp application.
12 * This class provides common functionalities for managing modules such as apps, bins, and tools.
13 */
14abstract class Module
15{
16 const BUNDLE_RELEASE = 'bundleRelease';
17
18 private $type;
19 private $id;
20
21 protected $name;
22 protected $version;
23 protected $release = 'N/A';
24
25 protected $rootPath;
26 protected $currentPath;
27 protected $symlinkPath;
28 protected $enable;
29 protected $bearsamppConf;
31
32 /**
33 * Constructor for the Module class.
34 * Initializes the module with default values.
35 */
36 protected function __construct() {
37 // Initialization logic can be added here if needed
38 }
39
40 /**
41 * Reloads the module configuration based on the provided ID and type.
42 *
43 * @param string|null $id The ID of the module. If null, the current ID is used.
44 * @param string|null $type The type of the module. If null, the current type is used.
45 */
46 protected function reload($id = null, $type = null) {
47 global $bearsamppRoot;
48
49 $this->id = empty($id) ? $this->id : $id;
50 $this->type = empty($type) ? $this->type : $type;
51 $mainPath = 'N/A';
52
53 switch ($this->type) {
54 case Apps::TYPE:
55 $mainPath = $bearsamppRoot->getAppsPath();
56 break;
57 case Bins::TYPE:
58 $mainPath = $bearsamppRoot->getBinPath();
59 break;
60 case Tools::TYPE:
61 $mainPath = $bearsamppRoot->getToolsPath();
62 break;
63 }
64
65 $this->rootPath = $mainPath . '/' . $this->id;
66 $this->currentPath = $this->rootPath . '/' . $this->id . $this->version;
67 $this->symlinkPath = $this->rootPath . '/current';
68 $this->enable = is_dir($this->currentPath);
69 $this->bearsamppConf = $this->currentPath . '/bearsampp.conf';
70 $this->bearsamppConfRaw = @parse_ini_file($this->bearsamppConf);
71
72 if ($bearsamppRoot->isRoot()) {
73 $this->createSymlink();
74 }
75 }
76
77 /**
78 * Creates a symbolic link from the current path to the symlink path.
79 * If the symlink already exists and points to the correct target, no action is taken.
80 */
81 private function createSymlink()
82 {
83 $src = Util::formatWindowsPath($this->currentPath);
84 $dest = Util::formatWindowsPath($this->symlinkPath);
85
86 if(file_exists($dest)) {
87 if (is_link($dest)) {
88 $target = readlink($dest);
89 if ($target == $src) {
90 return;
91 }
93 } elseif (is_file($dest)) {
94 Util::logError('Removing . ' . $this->symlinkPath . ' file. It should not be a regular file');
95 unlink($dest);
96 } elseif (is_dir($dest)) {
97 if (!(new \FilesystemIterator($dest))->valid()) {
98 rmdir($dest);
99 } else {
100 Util::logError($this->symlinkPath . ' should be a symlink to ' . $this->currentPath . '. Please remove this dir and restart bearsampp.');
101 return;
102 }
103 }
104 }
105
106 Batch::createSymlink($src, $dest);
107 }
108
109 /**
110 * Replaces a specific key-value pair in the configuration file.
111 *
112 * @param string $key The key to replace.
113 * @param string $value The new value for the key.
114 */
115 protected function replace($key, $value) {
116 $this->replaceAll(array($key => $value));
117 }
118
119 /**
120 * Replaces multiple key-value pairs in the configuration file.
121 *
122 * @param array $params An associative array of key-value pairs to replace.
123 */
124 protected function replaceAll($params) {
125 $content = file_get_contents($this->bearsamppConf);
126
127 foreach ($params as $key => $value) {
128 $content = preg_replace('|' . $key . ' = .*|', $key . ' = ' . '"' . $value.'"', $content);
129 $this->bearsamppConfRaw[$key] = $value;
130 }
131
132 file_put_contents($this->bearsamppConf, $content);
133 }
134
135 /**
136 * Updates the module configuration.
137 *
138 * @param int $sub The sub-level for logging indentation.
139 * @param bool $showWindow Whether to show a window during the update process.
140 */
141 public function update($sub = 0, $showWindow = false) {
142 $this->updateConfig(null, $sub, $showWindow);
143 }
144
145 /**
146 * Updates the module configuration with a specific version.
147 *
148 * @param string|null $version The version to update to. If null, the current version is used.
149 * @param int $sub The sub-level for logging indentation.
150 * @param bool $showWindow Whether to show a window during the update process.
151 */
152 protected function updateConfig($version = null, $sub = 0, $showWindow = false) {
153 $version = $version == null ? $this->version : $version;
154 Util::logDebug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'Update ' . $this->name . ' ' . $version . ' config');
155 }
156
157 /**
158 * Returns the name of the module.
159 *
160 * @return string The name of the module.
161 */
162 public function __toString() {
163 return $this->getName();
164 }
165
166 /**
167 * Gets the type of the module.
168 *
169 * @return string The type of the module.
170 */
171 public function getType() {
172 return $this->type;
173 }
174
175 /**
176 * Gets the ID of the module.
177 *
178 * @return string The ID of the module.
179 */
180 public function getId() {
181 return $this->id;
182 }
183
184 /**
185 * Gets the name of the module.
186 *
187 * @return string The name of the module.
188 */
189 public function getName() {
190 return $this->name;
191 }
192
193 /**
194 * Gets the version of the module.
195 *
196 * @return string The version of the module.
197 */
198 public function getVersion() {
199 return $this->version;
200 }
201
202 /**
203 * Gets the list of available versions for the module.
204 *
205 * @return array The list of available versions.
206 */
207 public function getVersionList() {
208 return Util::getVersionList($this->rootPath);
209 }
210
211 /**
212 * Sets the version of the module.
213 *
214 * @param string $version The version to set.
215 */
216 abstract public function setVersion($version);
217
218 /**
219 * Gets the release information of the module.
220 *
221 * @return string The release information.
222 */
223 public function getRelease() {
224 return $this->release;
225 }
226
227 /**
228 * Gets the root path of the module.
229 *
230 * @return string The root path of the module.
231 */
232 public function getRootPath() {
233 return $this->rootPath;
234 }
235
236 /**
237 * Gets the current path of the module.
238 *
239 * @return string The current path of the module.
240 */
241 public function getCurrentPath() {
242 return $this->currentPath;
243 }
244
245 /**
246 * Gets the symlink path of the module.
247 *
248 * @return string The symlink path of the module.
249 */
250 public function getSymlinkPath() {
251 return $this->symlinkPath;
252 }
253
254 /**
255 * Checks if the module is enabled.
256 *
257 * @return bool True if the module is enabled, false otherwise.
258 */
259 public function isEnable() {
260 return $this->enable;
261 }
262}
global $bearsamppRoot
const TYPE
static removeSymlink($link)
static createSymlink($src, $dest)
const TYPE
createSymlink()
reload($id=null, $type=null)
update($sub=0, $showWindow=false)
updateConfig($version=null, $sub=0, $showWindow=false)
setVersion($version)
replaceAll($params)
const BUNDLE_RELEASE
replace($key, $value)
const TYPE
static getVersionList($path)
static logError($data, $file=null)
static logDebug($data, $file=null)
static formatWindowsPath($path)