Bearsampp 2025.8.29
Loading...
Searching...
No Matches
class.bin.nodejs.php
Go to the documentation of this file.
1<?php
2/*
3 *
4 * * Copyright (c) 2021-2024 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{
50 const ROOT_CFG_ENABLE = 'nodejsEnable';
51 const ROOT_CFG_VERSION = 'nodejsVersion';
52
53 const LOCAL_CFG_EXE = 'nodejsExe';
54 const LOCAL_CFG_VARS = 'nodejsVars';
55 const LOCAL_CFG_NPM = 'nodejsNpm';
56 const LOCAL_CFG_LAUNCH = 'nodejsLaunch';
57 const LOCAL_CFG_CONF = 'nodejsConf';
58
59 private $exe;
60 private $conf;
61 private $vars;
62 private $npm;
63 private $launch;
64
71 public function __construct($id, $type) {
72 Util::logInitClass($this);
73 $this->reload($id, $type);
74 }
75
82 public function reload($id = null, $type = null) {
85
86 $this->name = $bearsamppLang->getValue(Lang::NODEJS);
87 $this->version = $bearsamppConfig->getRaw(self::ROOT_CFG_VERSION);
88 parent::reload($id, $type);
89
90 $this->enable = $this->enable && $bearsamppConfig->getRaw(self::ROOT_CFG_ENABLE);
91
92 if ($this->bearsamppConfRaw !== false) {
93 $this->exe = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_EXE];
94 $this->conf = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_CONF];
95 $this->vars = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_VARS];
96 $this->npm = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_NPM];
97 $this->launch = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_LAUNCH];
98 }
99
100 if (!$this->enable) {
101 Util::logInfo($this->name . ' is not enabled!');
102 return;
103 }
104 if (!is_dir($this->currentPath)) {
105 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->currentPath));
106 return;
107 }
108 if (!is_dir($this->symlinkPath)) {
109 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->symlinkPath));
110 return;
111 }
112 if (!is_file($this->bearsamppConf)) {
113 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_CONF_NOT_FOUND), $this->name . ' ' . $this->version, $this->bearsamppConf));
114 return;
115 }
116 if (!is_file($this->exe)) {
117 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->exe));
118 }
119 if (!is_file($this->conf)) {
120 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_CONF_NOT_FOUND), $this->name . ' ' . $this->version, $this->conf));
121 }
122 if (!is_file($this->vars)) {
123 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->vars));
124 }
125 if (!is_file($this->npm)) {
126 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->npm));
127 }
128 if (!is_file($this->launch)) {
129 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->launch));
130 }
131 }
132
140 public function switchVersion($version, $showWindow = false) {
141 Util::logDebug('Switch ' . $this->name . ' version to ' . $version);
142 return $this->updateConfig($version, 0, $showWindow);
143 }
144
153 protected function updateConfig($version = null, $sub = 0, $showWindow = false) {
154 global $bearsamppLang, $bearsamppWinbinder;
155
156 if (!$this->enable) {
157 return true;
158 }
159
160 $version = $version == null ? $this->version : $version;
161 Util::logDebug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'Update ' . $this->name . ' ' . $version . ' config');
162
163 $boxTitle = sprintf($bearsamppLang->getValue(Lang::SWITCH_VERSION_TITLE), $this->getName(), $version);
164
165 $conf = str_replace('nodejs' . $this->getVersion(), 'nodejs' . $version, $this->getConf());
166 $bearsamppConf = str_replace('nodejs' . $this->getVersion(), 'nodejs' . $version, $this->bearsamppConf);
167
168 if (!file_exists($conf) || !file_exists($bearsamppConf)) {
169 Util::logError('bearsampp config files not found for ' . $this->getName() . ' ' . $version);
170 if ($showWindow) {
171 $bearsamppWinbinder->messageBoxError(
172 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_NOT_FOUND_ERROR), $this->getName() . ' ' . $version),
173 $boxTitle
174 );
175 }
176 return false;
177 }
178
179 $bearsamppConfRaw = parse_ini_file($bearsamppConf);
180 if ($bearsamppConfRaw === false || !isset($bearsamppConfRaw[self::ROOT_CFG_VERSION]) || $bearsamppConfRaw[self::ROOT_CFG_VERSION] != $version) {
181 Util::logError('bearsampp config file malformed for ' . $this->getName() . ' ' . $version);
182 if ($showWindow) {
183 $bearsamppWinbinder->messageBoxError(
184 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_MALFORMED_ERROR), $this->getName() . ' ' . $version),
185 $boxTitle
186 );
187 }
188 return false;
189 }
190
191 // bearsampp.conf
192 $this->setVersion($version);
193
194 return true;
195 }
196
202 public function setVersion($version) {
203 global $bearsamppConfig;
204 $this->version = $version;
205 $bearsamppConfig->replace(self::ROOT_CFG_VERSION, $version);
206 $this->reload();
207 }
208
215 public function setEnable($enabled, $showWindow = false) {
216 global $bearsamppConfig, $bearsamppLang, $bearsamppWinbinder;
217
218 if ($enabled == Config::ENABLED && !is_dir($this->currentPath)) {
219 Util::logDebug($this->getName() . ' cannot be enabled because bundle ' . $this->getVersion() . ' does not exist in ' . $this->currentPath);
220 if ($showWindow) {
221 $bearsamppWinbinder->messageBoxError(
222 sprintf($bearsamppLang->getValue(Lang::ENABLE_BUNDLE_NOT_EXIST), $this->getName(), $this->getVersion(), $this->currentPath),
223 sprintf($bearsamppLang->getValue(Lang::ENABLE_TITLE), $this->getName())
224 );
225 }
226 $enabled = Config::DISABLED;
227 }
228
229 Util::logInfo($this->getName() . ' switched to ' . ($enabled == Config::ENABLED ? 'enabled' : 'disabled'));
230 $this->enable = $enabled == Config::ENABLED;
231 $bearsamppConfig->replace(self::ROOT_CFG_ENABLE, $enabled);
232 }
233
239 public function getExe() {
240 return $this->exe;
241 }
242
248 public function getConf() {
249 return $this->conf;
250 }
251
257 public function getVars() {
258 return $this->vars;
259 }
260
266 public function getNpm() {
267 return $this->npm;
268 }
269
275 public function getLaunch() {
276 return $this->launch;
277 }
278}
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 logError($data, $file=null)
static logInitClass($classInstance)
static logInfo($data, $file=null)
static logDebug($data, $file=null)
static logReloadClass($classInstance)
global $bearsamppConfig
Definition homepage.php:27