2024.8.23
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
11/**
12 * Class BinNodejs
13 *
14 * The `BinNodejs` class extends the `Module` class and provides functionalities specific to managing
15 * the Node.js module within the Bearsampp application. It includes methods for reloading the module
16 * configuration, switching versions, enabling/disabling the module, and retrieving various configuration
17 * paths such as the executable, configuration file, variables file, npm executable, and launch script.
18 *
19 * Constants:
20 * - `ROOT_CFG_ENABLE`: Configuration key for enabling the Node.js module.
21 * - `ROOT_CFG_VERSION`: Configuration key for the Node.js version.
22 * - `LOCAL_CFG_EXE`: Configuration key for the Node.js executable.
23 * - `LOCAL_CFG_VARS`: Configuration key for the Node.js variables.
24 * - `LOCAL_CFG_NPM`: Configuration key for the npm executable.
25 * - `LOCAL_CFG_LAUNCH`: Configuration key for the Node.js launch script.
26 * - `LOCAL_CFG_CONF`: Configuration key for the Node.js configuration file.
27 *
28 * Properties:
29 * - `exe`: Path to the Node.js executable.
30 * - `conf`: Path to the Node.js configuration file.
31 * - `vars`: Path to the Node.js variables file.
32 * - `npm`: Path to the npm executable.
33 * - `launch`: Path to the Node.js launch script.
34 *
35 * Methods:
36 * - `__construct($id, $type)`: Constructs a `BinNodejs` object and initializes the module with the given ID and type.
37 * - `reload($id = null, $type = null)`: Reloads the module configuration based on the provided ID and type.
38 * - `switchVersion($version, $showWindow = false)`: Switches the Node.js version to the specified version.
39 * - `updateConfig($version = null, $sub = 0, $showWindow = false)`: Updates the module configuration with a specific version.
40 * - `setVersion($version)`: Sets the version of the module.
41 * - `setEnable($enabled, $showWindow = false)`: Enables or disables the module.
42 * - `getExe()`: Retrieves the executable path for Node.js.
43 * - `getConf()`: Retrieves the configuration file path for Node.js.
44 * - `getVars()`: Retrieves the variables file path for Node.js.
45 * - `getNpm()`: Retrieves the npm executable path for Node.js.
46 * - `getLaunch()`: Retrieves the launch script path for Node.js.
47 */
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
65 /**
66 * Constructs a BinNodejs object and initializes the module with the given ID and type.
67 *
68 * @param string $id The ID of the module.
69 * @param string $type The type of the module.
70 */
71 public function __construct($id, $type) {
72 Util::logInitClass($this);
73 $this->reload($id, $type);
74 }
75
76 /**
77 * Reloads the module configuration based on the provided ID and type.
78 *
79 * @param string|null $id The ID of the module. If null, the current ID is used.
80 * @param string|null $type The type of the module. If null, the current type is used.
81 */
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
133 /**
134 * Switches the Node.js version to the specified version.
135 *
136 * @param string $version The version to switch to.
137 * @param bool $showWindow Whether to show a window during the switch process.
138 * @return bool True if the switch was successful, false otherwise.
139 */
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
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 * @return bool True if the update was successful, false otherwise.
152 */
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
197 /**
198 * Sets the version of the module.
199 *
200 * @param string $version The version to set.
201 */
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
209 /**
210 * Enables or disables the module.
211 *
212 * @param int $enabled The enable status (1 for enabled, 0 for disabled).
213 * @param bool $showWindow Whether to show a window during the enable/disable process.
214 */
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
234 /**
235 * Retrieves the executable path for Node.js.
236 *
237 * @return string The executable path.
238 */
239 public function getExe() {
240 return $this->exe;
241 }
242
243 /**
244 * Retrieves the configuration file path for Node.js.
245 *
246 * @return string The configuration file path.
247 */
248 public function getConf() {
249 return $this->conf;
250 }
251
252 /**
253 * Retrieves the variables file path for Node.js.
254 *
255 * @return string The variables file path.
256 */
257 public function getVars() {
258 return $this->vars;
259 }
260
261 /**
262 * Retrieves the npm executable path for Node.js.
263 *
264 * @return string The npm executable path.
265 */
266 public function getNpm() {
267 return $this->npm;
268 }
269
270 /**
271 * Retrieves the launch script path for Node.js.
272 *
273 * @return string The launch script path.
274 */
275 public function getLaunch() {
276 return $this->launch;
277 }
278}
global $bearsamppLang
switchVersion($version, $showWindow=false)
const ROOT_CFG_VERSION
setEnable($enabled, $showWindow=false)
const LOCAL_CFG_LAUNCH
__construct($id, $type)
updateConfig($version=null, $sub=0, $showWindow=false)
const ROOT_CFG_ENABLE
reload($id=null, $type=null)
setVersion($version)
const DISABLED
const ENABLED
const ENABLE_TITLE
const ERROR_FILE_NOT_FOUND
const NODEJS
const BEARSAMPP_CONF_NOT_FOUND_ERROR
const ENABLE_BUNDLE_NOT_EXIST
const ERROR_CONF_NOT_FOUND
const SWITCH_VERSION_TITLE
const ERROR_EXE_NOT_FOUND
const BEARSAMPP_CONF_MALFORMED_ERROR
static logReloadClass($classInstance)
static logError($data, $file=null)
static logDebug($data, $file=null)
static logInitClass($classInstance)
static logInfo($data, $file=null)
global $bearsamppConfig
Definition homepage.php:26