Bearsampp 2026.9.23
Loading...
Searching...
No Matches
class.bin.nodejs.php
Go to the documentation of this file.
1<?php
2/*
3 *
4 * * Copyright (c) 2021-2026 Bearsampp
5 * * License: GNU General Public License version 3 or later; see LICENSE.txt
6 * * Website: https://bearsampp.com
7 * * Github: https://github.com/Bearsampp
8 *
9 */
10
48class BinNodejs extends Module
49{
53 const ROOT_CFG_ENABLE = 'nodejsEnable';
57 const ROOT_CFG_VERSION = 'nodejsVersion';
58
62 const LOCAL_CFG_EXE = 'nodejsExe';
66 const LOCAL_CFG_VARS = 'nodejsVars';
70 const LOCAL_CFG_NPM = 'nodejsNpm';
74 const LOCAL_CFG_LAUNCH = 'nodejsLaunch';
78 const LOCAL_CFG_CONF = 'nodejsConf';
79
83 private $exe;
87 private $conf;
91 private $vars;
95 private $npm;
99 private $launch;
100
107 public function __construct($id, $type)
108 {
109 Log::initClass($this);
110 $this->reload($id, $type);
111 }
112
119 public function reload($id = null, $type = null)
120 {
122 Log::reloadClass($this);
123
124 $this->name = $bearsamppLang->getValue(Lang::NODEJS);
125 $this->version = $bearsamppConfig->getRaw(self::ROOT_CFG_VERSION);
126 parent::reload($id, $type);
127
128 $this->enable = $this->enable && $bearsamppConfig->getRaw(self::ROOT_CFG_ENABLE);
129
130 if ($this->bearsamppConfRaw !== false) {
131 $this->exe = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_EXE];
132 $this->conf = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_CONF];
133 $this->vars = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_VARS];
134 $this->npm = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_NPM];
135 $this->launch = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_LAUNCH];
136 }
137
138 if (!$this->enable) {
139 Log::info($this->name . ' is not enabled!');
140
141 return;
142 }
143 if (!is_dir($this->currentPath)) {
144 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->currentPath));
145
146 return;
147 }
148 if (!is_dir($this->symlinkPath)) {
149 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->symlinkPath));
150
151 return;
152 }
153 if (!is_file($this->bearsamppConf)) {
154 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_CONF_NOT_FOUND), $this->name . ' ' . $this->version, $this->bearsamppConf));
155
156 return;
157 }
158 if (!is_file($this->exe)) {
159 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->exe));
160 }
161 if (!is_file($this->conf)) {
162 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_CONF_NOT_FOUND), $this->name . ' ' . $this->version, $this->conf));
163 }
164 if (!is_file($this->vars)) {
165 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->vars));
166 }
167 if (!is_file($this->npm)) {
168 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->npm));
169 }
170 if (!is_file($this->launch)) {
171 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->launch));
172 }
173 }
174
183 public function switchVersion($version, $showWindow = false)
184 {
185 Log::debug('Switch ' . $this->name . ' version to ' . $version);
186
187 return $this->updateConfig($version, 0, $showWindow);
188 }
189
199 protected function updateConfig($version = null, $sub = 0, $showWindow = false)
200 {
201 global $bearsamppLang, $bearsamppWinbinder;
202
203 if (!$this->enable) {
204 return true;
205 }
206
207 $version = $version == null ? $this->version : $version;
208 Log::debug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'Update ' . $this->name . ' ' . $version . ' config');
209
210 $boxTitle = sprintf($bearsamppLang->getValue(Lang::SWITCH_VERSION_TITLE), $this->getName(), $version);
211
212 $conf = str_replace('nodejs' . $this->getVersion(), 'nodejs' . $version, $this->getConf());
213 $bearsamppConf = str_replace('nodejs' . $this->getVersion(), 'nodejs' . $version, $this->bearsamppConf);
214
215 if (!file_exists($conf) || !file_exists($bearsamppConf)) {
216 Log::error('bearsampp config files not found for ' . $this->getName() . ' ' . $version);
217 if ($showWindow) {
218 $bearsamppWinbinder->messageBoxError(
219 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_NOT_FOUND_ERROR), $this->getName() . ' ' . $version),
220 $boxTitle
221 );
222 }
223
224 return false;
225 }
226
227 $bearsamppConfRaw = parse_ini_file($bearsamppConf);
228 if ($bearsamppConfRaw === false || !isset($bearsamppConfRaw[self::ROOT_CFG_VERSION]) || $bearsamppConfRaw[self::ROOT_CFG_VERSION] != $version) {
229 Log::error('bearsampp config file malformed for ' . $this->getName() . ' ' . $version);
230 if ($showWindow) {
231 $bearsamppWinbinder->messageBoxError(
232 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_MALFORMED_ERROR), $this->getName() . ' ' . $version),
233 $boxTitle
234 );
235 }
236
237 return false;
238 }
239
240 // bearsampp.conf
241 $this->setVersion($version);
242
243 return true;
244 }
245
251 public function setVersion($version)
252 {
253 global $bearsamppConfig;
254 $this->version = $version;
255 $bearsamppConfig->replace(self::ROOT_CFG_VERSION, $version);
256 $this->reload();
257 }
258
265 public function setEnable($enabled, $showWindow = false)
266 {
267 global $bearsamppConfig, $bearsamppLang, $bearsamppWinbinder;
268
269 if ($enabled == Config::ENABLED && !is_dir($this->currentPath)) {
270 Log::debug($this->getName() . ' cannot be enabled because bundle ' . $this->getVersion() . ' does not exist in ' . $this->currentPath);
271 if ($showWindow) {
272 $bearsamppWinbinder->messageBoxError(
273 sprintf($bearsamppLang->getValue(Lang::ENABLE_BUNDLE_NOT_EXIST), $this->getName(), $this->getVersion(), $this->currentPath),
274 sprintf($bearsamppLang->getValue(Lang::ENABLE_TITLE), $this->getName())
275 );
276 }
277 $enabled = Config::DISABLED;
278 }
279
280 Log::info($this->getName() . ' switched to ' . ($enabled == Config::ENABLED ? 'enabled' : 'disabled'));
281 $this->enable = $enabled == Config::ENABLED;
282 $bearsamppConfig->replace(self::ROOT_CFG_ENABLE, $enabled);
283 }
284
290 public function getExe()
291 {
292 return $this->exe;
293 }
294
300 public function getConf()
301 {
302 return $this->conf;
303 }
304
310 public function getVars()
311 {
312 return $this->vars;
313 }
314
320 public function getNpm()
321 {
322 return $this->npm;
323 }
324
330 public function getLaunch()
331 {
332 return $this->launch;
333 }
334}
335
global $bearsamppLang
updateConfig($version=null, $sub=0, $showWindow=false)
switchVersion($version, $showWindow=false)
const ROOT_CFG_ENABLE
setVersion($version)
setEnable($enabled, $showWindow=false)
const LOCAL_CFG_LAUNCH
reload($id=null, $type=null)
const ROOT_CFG_VERSION
__construct($id, $type)
const DISABLED
const ENABLED
const NODEJS
const ENABLE_BUNDLE_NOT_EXIST
const BEARSAMPP_CONF_MALFORMED_ERROR
const ERROR_EXE_NOT_FOUND
const ERROR_CONF_NOT_FOUND
const BEARSAMPP_CONF_NOT_FOUND_ERROR
const ENABLE_TITLE
const SWITCH_VERSION_TITLE
const ERROR_FILE_NOT_FOUND
static info($data, $file=null)
static debug($data, $file=null)
static reloadClass($classInstance)
static error($data, $file=null)
static initClass($classInstance)
global $bearsamppConfig
Definition homepage.php:46