Bearsampp 2026.5.5
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 $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
36 protected function __construct() {
37 // Initialization logic can be added here if needed
38 }
39
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
81 private function createSymlink()
82 {
83 $src = UtilPath::formatWindowsPath($this->currentPath);
84 $dest = UtilPath::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 Log::error('Removing . ' . $this->symlinkPath . ' file. It should not be a regular file');
95 unlink($dest);
96 } elseif (is_dir($dest)) {
97 // Never recursively delete here: this path is expected to be a symlink.
98 // Only remove empty directories; otherwise abort to avoid data loss.
99 $it = new \FilesystemIterator($dest);
100 if (!$it->valid()) {
101 rmdir($dest);
102 } else {
103 Log::error($this->symlinkPath . ' should be a symlink to ' . $this->currentPath . '. Please remove this dir and restart bearsampp.');
104 return;
105 }
106 }
107 }
108
109 Batch::createSymlink($src, $dest);
110 }
111
118 protected function replace($key, $value) {
119 $this->replaceAll(array($key => $value));
120 }
121
127 protected function replaceAll($params) {
128 $content = file_get_contents($this->bearsamppConf);
129
130 foreach ($params as $key => $value) {
131 $content = preg_replace('|' . $key . ' = .*|', $key . ' = ' . '"' . $value.'"', $content);
132 $this->bearsamppConfRaw[$key] = $value;
133 }
134
135 file_put_contents($this->bearsamppConf, $content);
136 }
137
144 public function update($sub = 0, $showWindow = false) {
145 $this->updateConfig(null, $sub, $showWindow);
146 }
147
155 protected function updateConfig($version = null, $sub = 0, $showWindow = false) {
156 $version = $version == null ? $this->version : $version;
157 Log::debug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'Update ' . $this->name . ' ' . $version . ' config');
158 }
159
165 public function __toString() {
166 return $this->getName();
167 }
168
174 public function getType() {
175 return $this->type;
176 }
177
183 public function getId() {
184 return $this->id;
185 }
186
192 public function getName() {
193 return $this->name;
194 }
195
201 public function getVersion() {
202 return $this->version;
203 }
204
210 public function getVersionList() {
211 return Util::getVersionList($this->rootPath);
212 }
213
219 abstract public function setVersion($version);
220
226 public function getRelease() {
227 return $this->release;
228 }
229
235 public function getRootPath() {
236 return $this->rootPath;
237 }
238
244 public function getCurrentPath() {
245 return $this->currentPath;
246 }
247
253 public function getSymlinkPath() {
254 return $this->symlinkPath;
255 }
256
262 public function isEnable() {
263 return $this->enable;
264 }
265}
global $bearsamppRoot
const TYPE
static removeSymlink($link)
static createSymlink($src, $dest)
const TYPE
static debug($data, $file=null)
static error($data, $file=null)
replaceAll($params)
createSymlink()
update($sub=0, $showWindow=false)
updateConfig($version=null, $sub=0, $showWindow=false)
const BUNDLE_RELEASE
setVersion($version)
replace($key, $value)
reload($id=null, $type=null)
const TYPE
static getVersionList($path)
static formatWindowsPath($path)