Bearsampp 2026.3.26
API documentation
Loading...
Searching...
No Matches
BinMemcached Class Reference
Inheritance diagram for BinMemcached:

Public Member Functions

 __construct ($id, $type)
 changePort ($port, $checkUsed=false, $wbProgressBar=null)
 checkPort ($port, $showWindow=false)
 getExe ()
 getLog ()
 getMemory ()
 getPort ()
 getService ()
 rebuildConf ()
 reload ($id=null, $type=null)
 setEnable ($enabled, $showWindow=false)
 setMemory ($memory)
 setPort ($port)
 setVersion ($version)
 switchVersion ($version, $showWindow=false)
Public Member Functions inherited from Module
 __toString ()
 getCurrentPath ()
 getId ()
 getName ()
 getRelease ()
 getRootPath ()
 getSymlinkPath ()
 getType ()
 getVersion ()
 getVersionList ()
 isEnable ()
 update ($sub=0, $showWindow=false)

Data Fields

const LOCAL_CFG_EXE = 'memcachedExe'
const LOCAL_CFG_MEMORY = 'memcachedMemory'
const LOCAL_CFG_PORT = 'memcachedPort'
const ROOT_CFG_ENABLE = 'memcachedEnable'
const ROOT_CFG_VERSION = 'memcachedVersion'
const SERVICE_NAME = 'bearsamppmemcached'
const SERVICE_PARAMS = '-m %d -p %d -U 0 -vv'
Data Fields inherited from Module
const BUNDLE_RELEASE = 'bundleRelease'

Protected Member Functions

 replaceAll ($params)
 updateConfig ($version=null, $sub=0, $showWindow=false)
Protected Member Functions inherited from Module
 __construct ()
 replace ($key, $value)

Private Attributes

 $exe
 $log
 $memory
 $port
 $service

Additional Inherited Members

Protected Attributes inherited from Module
 $bearsamppConf
 $bearsamppConfRaw
 $currentPath
 $enable
 $name
 $release = 'N/A'
 $rootPath
 $symlinkPath
 $version

Detailed Description

Class BinMemcached

This class represents the Memcached service module in the Bearsampp application. It handles the configuration, initialization, and management of the Memcached service.

Definition at line 16 of file class.bin.memcached.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( $id,
$type )

Constructs a BinMemcached object and initializes the Memcached service.

Parameters
string$idThe ID of the module.
string$typeThe type of the module.

Definition at line 41 of file class.bin.memcached.php.

41 {
42 Util::logInitClass($this);
43 $this->reload($id, $type);
44 }
reload($id=null, $type=null)
static logInitClass($classInstance)

References Module\$id, Module\$type, Util\logInitClass(), and reload().

Member Function Documentation

◆ changePort()

changePort ( $port,
$checkUsed = false,
$wbProgressBar = null )

Changes the port for the Memcached service.

Parameters
int$portThe new port number.
bool$checkUsedWhether to check if the port is already in use.
mixed$wbProgressBarThe progress bar object for UI updates.
Returns
bool|int True if the port was successfully changed, false if the port is invalid, or the process using the port.

Definition at line 167 of file class.bin.memcached.php.

167 {
168 global $bearsamppWinbinder;
169
170 if (!Util::isValidPort($port)) {
171 Util::logError($this->getName() . ' port not valid: ' . $port);
172 return false;
173 }
174
175 $port = intval($port);
176 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
177
178 $isPortInUse = Util::isPortInUse($port);
179 if (!$checkUsed || $isPortInUse === false) {
180 // bearsampp.conf
181 $this->setPort($port);
182 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
183
184 // conf
185 $this->update();
186 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
187
188 return true;
189 }
190
191 Util::logDebug($this->getName() . ' port in used: ' . $port . ' - ' . $isPortInUse);
192 return $isPortInUse;
193 }
update($sub=0, $showWindow=false)
static logError($data, $file=null)
static isValidPort($port)
static logDebug($data, $file=null)
static isPortInUse($port)

References $port, Module\getName(), Util\isPortInUse(), Util\isValidPort(), Util\logDebug(), Util\logError(), setPort(), and Module\update().

◆ checkPort()

checkPort ( $port,
$showWindow = false )

Checks if the specified port is in use by the Memcached service.

Parameters
int$portThe port number to check.
bool$showWindowWhether to show a message box with the result.
Returns
bool True if the port is in use by Memcached, false otherwise.

Definition at line 202 of file class.bin.memcached.php.

202 {
203 global $bearsamppLang, $bearsamppWinbinder;
204 $boxTitle = sprintf($bearsamppLang->getValue(Lang::CHECK_PORT_TITLE), $this->getName(), $port);
205
206 if (!Util::isValidPort($port)) {
207 Util::logError($this->getName() . ' port not valid: ' . $port);
208 return false;
209 }
210
211 // Use fsockopen instead of memcache_connect to avoid file descriptor leaks in PHP 8.4+
212 $fp = @fsockopen('127.0.0.1', $port, $errno, $errstr, 1);
213 if ($fp) {
214 // Port is open, verify it's memcached by sending a simple command
215 @stream_set_timeout($fp, 1);
216 @fwrite($fp, "version\r\n");
217 $response = @fgets($fp, 128);
218 @fclose($fp);
219
220 if ($response && strpos($response, 'VERSION') === 0) {
221 // Extract version from response (format: "VERSION x.x.x")
222 $version = trim(substr($response, 8));
223 Util::logDebug($this->getName() . ' port ' . $port . ' is used by: ' . $this->getName() . ' ' . $version);
224 if ($showWindow) {
225 $bearsamppWinbinder->messageBoxInfo(
226 sprintf($bearsamppLang->getValue(Lang::PORT_USED_BY), $port, $this->getName() . ' ' . $version),
227 $boxTitle
228 );
229 }
230 return true;
231 } else {
232 // Port is open but not responding as memcached
233 Util::logDebug($this->getName() . ' port ' . $port . ' is open but not responding as memcached');
234 if ($showWindow) {
235 $bearsamppWinbinder->messageBoxWarning(
236 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED_BY), $port),
237 $boxTitle
238 );
239 }
240 return false;
241 }
242 } else {
243 // Port is not open
244 Util::logDebug($this->getName() . ' port ' . $port . ' is not open');
245 if ($showWindow) {
246 $bearsamppWinbinder->messageBoxError(
247 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED), $port),
248 $boxTitle
249 );
250 }
251 return false;
252 }
253 }
global $bearsamppLang
const PORT_NOT_USED
const PORT_NOT_USED_BY
const CHECK_PORT_TITLE
const PORT_USED_BY

References $bearsamppLang, $port, $response, Module\$version, Lang\CHECK_PORT_TITLE, Module\getName(), Util\isValidPort(), Util\logDebug(), Util\logError(), Lang\PORT_NOT_USED, Lang\PORT_NOT_USED_BY, and Lang\PORT_USED_BY.

◆ getExe()

getExe ( )

Retrieves the executable file path for the Memcached service.

Returns
string The executable file path.

Definition at line 384 of file class.bin.memcached.php.

384 {
385 return $this->exe;
386 }

References $exe.

◆ getLog()

getLog ( )

Retrieves the log file path for the Memcached service.

Returns
string The log file path.

Definition at line 375 of file class.bin.memcached.php.

375 {
376 return $this->log;
377 }

References $log.

◆ getMemory()

getMemory ( )

Retrieves the memory allocation for the Memcached service.

Returns
int The memory allocation in MB.

Definition at line 393 of file class.bin.memcached.php.

393 {
394 return $this->memory;
395 }

References $memory.

◆ getPort()

getPort ( )

Retrieves the port number for the Memcached service.

Returns
int The port number.

Definition at line 411 of file class.bin.memcached.php.

411 {
412 return $this->port;
413 }

References $port.

◆ getService()

getService ( )

Retrieves the service object for the Memcached service.

Returns
Win32Service The service object.

Definition at line 334 of file class.bin.memcached.php.

334 {
335 return $this->service;
336 }

References $service.

◆ rebuildConf()

rebuildConf ( )

Rebuilds the configuration for the Memcached service in the Windows Registry.

Returns
bool True if the configuration was successfully rebuilt, false otherwise.

Definition at line 139 of file class.bin.memcached.php.

139 {
140 global $bearsamppRegistry;
141
142 $exists = $bearsamppRegistry->exists(
144 'SYSTEM\CurrentControlSet\Services\\' . self::SERVICE_NAME . '\Parameters',
146 );
147 if ($exists) {
148 return $bearsamppRegistry->setExpandStringValue(
150 'SYSTEM\CurrentControlSet\Services\\' . self::SERVICE_NAME . '\Parameters',
152 sprintf(self::SERVICE_PARAMS, $this->memory, $this->port)
153 );
154 }
155
156 return false;
157 }
const INFO_APP_PARAMETERS
const HKEY_LOCAL_MACHINE

References Registry\HKEY_LOCAL_MACHINE, and Nssm\INFO_APP_PARAMETERS.

◆ reload()

reload ( $id = null,
$type = null )

Reloads the configuration and settings for the Memcached service.

Parameters
string | null$idThe ID of the module. If null, the current ID is used.
string | null$typeThe type of the module. If null, the current type is used.

Reimplemented from Module.

Definition at line 52 of file class.bin.memcached.php.

52 {
55
56 $this->name = $bearsamppLang->getValue(Lang::MEMCACHED);
57 $this->version = $bearsamppConfig->getRaw(self::ROOT_CFG_VERSION);
58 parent::reload($id, $type);
59
60 $this->enable = $this->enable && $bearsamppConfig->getRaw(self::ROOT_CFG_ENABLE);
61 $this->service = new Win32Service(self::SERVICE_NAME);
62 $this->log = $bearsamppRoot->getLogsPath() . '/memcached.log';
63
64 if ($this->bearsamppConfRaw !== false) {
65 $this->exe = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_EXE];
66 $this->memory = intval($this->bearsamppConfRaw[self::LOCAL_CFG_MEMORY]);
67 $this->port = intval($this->bearsamppConfRaw[self::LOCAL_CFG_PORT]);
68 }
69
70 if (!$this->enable) {
71 Util::logInfo($this->name . ' is not enabled!');
72 return;
73 }
74 if (!is_dir($this->currentPath)) {
75 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->currentPath));
76 return;
77 }
78 if (!is_dir($this->symlinkPath)) {
79 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->symlinkPath));
80 return;
81 }
82 if (!is_file($this->bearsamppConf)) {
83 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_CONF_NOT_FOUND), $this->name . ' ' . $this->version, $this->bearsamppConf));
84 return;
85 }
86 if (!is_file($this->exe)) {
87 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->exe));
88 return;
89 }
90 if (empty($this->memory)) {
91 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_INVALID_PARAMETER), self::LOCAL_CFG_MEMORY, $this->memory));
92 return;
93 }
94 if (empty($this->port)) {
95 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_INVALID_PARAMETER), self::LOCAL_CFG_PORT, $this->port));
96 return;
97 }
98
99 $nssm = new Nssm(self::SERVICE_NAME);
100 $nssm->setDisplayName(APP_TITLE . ' ' . $this->getName());
101 $nssm->setBinPath($this->exe);
102 $nssm->setParams(sprintf(self::SERVICE_PARAMS, $this->memory, $this->port));
103 $nssm->setStart(Nssm::SERVICE_DEMAND_START);
104 $nssm->setStdout($bearsamppRoot->getLogsPath() . '/memcached.out.log');
105 $nssm->setStderr($bearsamppRoot->getLogsPath() . '/memcached.err.log');
106
107 $this->service->setNssm($nssm);
108 }
global $bearsamppRoot
const ERROR_EXE_NOT_FOUND
const ERROR_CONF_NOT_FOUND
const MEMCACHED
const ERROR_INVALID_PARAMETER
const ERROR_FILE_NOT_FOUND
const SERVICE_DEMAND_START
static logInfo($data, $file=null)
static logReloadClass($classInstance)
global $bearsamppConfig
Definition homepage.php:41
const APP_TITLE
Definition root.php:13

References $bearsamppConfig, $bearsamppLang, $bearsamppRoot, Module\$id, Module\$type, APP_TITLE, Lang\ERROR_CONF_NOT_FOUND, Lang\ERROR_EXE_NOT_FOUND, Lang\ERROR_FILE_NOT_FOUND, Lang\ERROR_INVALID_PARAMETER, Module\getName(), Util\logError(), Util\logInfo(), Util\logReloadClass(), Lang\MEMCACHED, and Nssm\SERVICE_DEMAND_START.

Referenced by __construct(), setEnable(), and setVersion().

◆ replaceAll()

replaceAll ( $params)
protected

Replaces multiple key-value pairs in the configuration file.

Parameters
array$paramsAn associative array of key-value pairs to replace.

Reimplemented from Module.

Definition at line 115 of file class.bin.memcached.php.

115 {
116 $content = file_get_contents($this->bearsamppConf);
117
118 foreach ($params as $key => $value) {
119 $content = preg_replace('|' . $key . ' = .*|', $key . ' = ' . '"' . $value.'"', $content);
120 $this->bearsamppConfRaw[$key] = $value;
121 switch ($key) {
122 case self::LOCAL_CFG_MEMORY:
123 $this->memory = intval($value);
124 break;
125 case self::LOCAL_CFG_PORT:
126 $this->port = intval($value);
127 break;
128 }
129 }
130
131 file_put_contents($this->bearsamppConf, $content);
132 }

◆ setEnable()

setEnable ( $enabled,
$showWindow = false )

Enables or disables the Memcached service.

Parameters
bool$enabledWhether to enable or disable the service.
bool$showWindowWhether to show a message box with the result.

Definition at line 344 of file class.bin.memcached.php.

344 {
345 global $bearsamppConfig, $bearsamppLang, $bearsamppWinbinder;
346
347 if ($enabled == Config::ENABLED && !is_dir($this->currentPath)) {
348 Util::logDebug($this->getName() . ' cannot be enabled because bundle ' . $this->getVersion() . ' does not exist in ' . $this->currentPath);
349 if ($showWindow) {
350 $bearsamppWinbinder->messageBoxError(
351 sprintf($bearsamppLang->getValue(Lang::ENABLE_BUNDLE_NOT_EXIST), $this->getName(), $this->getVersion(), $this->currentPath),
352 sprintf($bearsamppLang->getValue(Lang::ENABLE_TITLE), $this->getName())
353 );
354 }
355 $enabled = Config::DISABLED;
356 }
357
358 Util::logInfo($this->getName() . ' switched to ' . ($enabled == Config::ENABLED ? 'enabled' : 'disabled'));
359 $this->enable = $enabled == Config::ENABLED;
360 $bearsamppConfig->replace(self::ROOT_CFG_ENABLE, $enabled);
361
362 $this->reload();
363 if ($this->enable) {
364 Util::installService($this, $this->port, null, $showWindow);
365 } else {
366 Util::removeService($this->service, $this->name);
367 }
368 }
const DISABLED
const ENABLED
const ENABLE_BUNDLE_NOT_EXIST
const ENABLE_TITLE
static installService($bin, $port, $syntaxCheckCmd, $showWindow=false)
static removeService($service, $name)

References $bearsamppConfig, $bearsamppLang, Config\DISABLED, Lang\ENABLE_BUNDLE_NOT_EXIST, Lang\ENABLE_TITLE, Config\ENABLED, Module\getName(), Module\getVersion(), Util\installService(), Util\logDebug(), Util\logInfo(), reload(), and Util\removeService().

◆ setMemory()

setMemory ( $memory)

Sets the memory allocation for the Memcached service.

Parameters
int$memoryThe memory allocation in MB.

Definition at line 402 of file class.bin.memcached.php.

402 {
403 $this->replace(self::LOCAL_CFG_MEMORY, $memory);
404 }
replace($key, $value)

References $memory, and Module\replace().

◆ setPort()

setPort ( $port)

Sets the port number for the Memcached service.

Parameters
int$portThe port number.

Definition at line 420 of file class.bin.memcached.php.

420 {
421 $this->replace(self::LOCAL_CFG_PORT, $port);
422 }

References $port, and Module\replace().

Referenced by changePort().

◆ setVersion()

setVersion ( $version)

Sets the version of the Memcached service.

Parameters
string$versionThe version to set.

Reimplemented from Module.

Definition at line 322 of file class.bin.memcached.php.

322 {
323 global $bearsamppConfig;
324 $this->version = $version;
325 $bearsamppConfig->replace(self::ROOT_CFG_VERSION, $version);
326 $this->reload();
327 }

References $bearsamppConfig, Module\$version, and reload().

Referenced by updateConfig().

◆ switchVersion()

switchVersion ( $version,
$showWindow = false )

Switches the version of the Memcached service.

Parameters
string$versionThe version to switch to.
bool$showWindowWhether to show a message box with the result.
Returns
bool True if the version was successfully switched, false otherwise.

Definition at line 262 of file class.bin.memcached.php.

262 {
263 Util::logDebug('Switch ' . $this->name . ' version to ' . $version);
264 return $this->updateConfig($version, 0, $showWindow);
265 }
updateConfig($version=null, $sub=0, $showWindow=false)

References Module\$version, Util\logDebug(), and updateConfig().

◆ updateConfig()

updateConfig ( $version = null,
$sub = 0,
$showWindow = false )
protected

Updates the configuration for the Memcached service.

Parameters
string | null$versionThe version to update to. If null, the current version is used.
int$subThe sub-level for logging indentation.
bool$showWindowWhether to show a message box with the result.
Returns
bool True if the configuration was successfully updated, false otherwise.

Reimplemented from Module.

Definition at line 275 of file class.bin.memcached.php.

275 {
276 global $bearsamppLang, $bearsamppApps, $bearsamppWinbinder;
277
278 if (!$this->enable) {
279 return true;
280 }
281
282 $version = $version == null ? $this->version : $version;
283 Util::logDebug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'Update ' . $this->name . ' ' . $version . ' config');
284
285 $boxTitle = sprintf($bearsamppLang->getValue(Lang::SWITCH_VERSION_TITLE), $this->getName(), $version);
286
287 $bearsamppConf = str_replace('memcached' . $this->getVersion(), 'memcached' . $version, $this->bearsamppConf);
288 if (!file_exists($bearsamppConf)) {
289 Util::logError('bearsampp config files not found for ' . $this->getName() . ' ' . $version);
290 if ($showWindow) {
291 $bearsamppWinbinder->messageBoxError(
292 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_NOT_FOUND_ERROR), $this->getName() . ' ' . $version),
293 $boxTitle
294 );
295 }
296 return false;
297 }
298
299 $bearsamppConfRaw = parse_ini_file($bearsamppConf);
300 if ($bearsamppConfRaw === false || !isset($bearsamppConfRaw[self::ROOT_CFG_VERSION]) || $bearsamppConfRaw[self::ROOT_CFG_VERSION] != $version) {
301 Util::logError('bearsampp config file malformed for ' . $this->getName() . ' ' . $version);
302 if ($showWindow) {
303 $bearsamppWinbinder->messageBoxError(
304 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_MALFORMED_ERROR), $this->getName() . ' ' . $version),
305 $boxTitle
306 );
307 }
308 return false;
309 }
310
311 // bearsampp.conf
312 $this->setVersion($version);
313
314 return true;
315 }
const BEARSAMPP_CONF_MALFORMED_ERROR
const BEARSAMPP_CONF_NOT_FOUND_ERROR
const SWITCH_VERSION_TITLE

References Module\$bearsamppConf, Module\$bearsamppConfRaw, $bearsamppLang, Module\$version, Lang\BEARSAMPP_CONF_MALFORMED_ERROR, Lang\BEARSAMPP_CONF_NOT_FOUND_ERROR, Module\getName(), Module\getVersion(), Util\logDebug(), Util\logError(), setVersion(), and Lang\SWITCH_VERSION_TITLE.

Referenced by switchVersion().

Field Documentation

◆ $exe

$exe
private

Definition at line 31 of file class.bin.memcached.php.

Referenced by getExe().

◆ $log

$log
private

Definition at line 29 of file class.bin.memcached.php.

Referenced by getLog().

◆ $memory

$memory
private

Definition at line 32 of file class.bin.memcached.php.

Referenced by getMemory(), and setMemory().

◆ $port

$port
private

Definition at line 33 of file class.bin.memcached.php.

Referenced by changePort(), checkPort(), getPort(), and setPort().

◆ $service

$service
private

Definition at line 28 of file class.bin.memcached.php.

Referenced by getService().

◆ LOCAL_CFG_EXE

const LOCAL_CFG_EXE = 'memcachedExe'

Definition at line 24 of file class.bin.memcached.php.

◆ LOCAL_CFG_MEMORY

const LOCAL_CFG_MEMORY = 'memcachedMemory'

Definition at line 25 of file class.bin.memcached.php.

◆ LOCAL_CFG_PORT

const LOCAL_CFG_PORT = 'memcachedPort'

Definition at line 26 of file class.bin.memcached.php.

◆ ROOT_CFG_ENABLE

const ROOT_CFG_ENABLE = 'memcachedEnable'

Definition at line 21 of file class.bin.memcached.php.

◆ ROOT_CFG_VERSION

const ROOT_CFG_VERSION = 'memcachedVersion'

Definition at line 22 of file class.bin.memcached.php.

◆ SERVICE_NAME

◆ SERVICE_PARAMS

const SERVICE_PARAMS = '-m %d -p %d -U 0 -vv'

Definition at line 19 of file class.bin.memcached.php.


The documentation for this class was generated from the following file: