Bearsampp 2025.8.29
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 = 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
115 protected function replace($key, $value) {
116 $this->replaceAll(array($key => $value));
117 }
118
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
141 public function update($sub = 0, $showWindow = false) {
142 $this->updateConfig(null, $sub, $showWindow);
143 }
144
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
162 public function __toString() {
163 return $this->getName();
164 }
165
171 public function getType() {
172 return $this->type;
173 }
174
180 public function getId() {
181 return $this->id;
182 }
183
189 public function getName() {
190 return $this->name;
191 }
192
198 public function getVersion() {
199 return $this->version;
200 }
201
207 public function getVersionList() {
208 return Util::getVersionList($this->rootPath);
209 }
210
216 abstract public function setVersion($version);
217
223 public function getRelease() {
224 return $this->release;
225 }
226
232 public function getRootPath() {
233 return $this->rootPath;
234 }
235
241 public function getCurrentPath() {
242 return $this->currentPath;
243 }
244
250 public function getSymlinkPath() {
251 return $this->symlinkPath;
252 }
253
259 public function isEnable() {
260 return $this->enable;
261 }
262}
global $bearsamppRoot
const TYPE
static removeSymlink($link)
static createSymlink($src, $dest)
const TYPE
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 logError($data, $file=null)
static getVersionList($path)
static logDebug($data, $file=null)
static formatWindowsPath($path)