Bearsampp 2026.9.23
Loading...
Searching...
No Matches
BinMemcached Class Reference
Inheritance diagram for BinMemcached:
Collaboration 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 ()
 getId ()
 getName ()
 getRelease ()
 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
 $currentPath
 $rootPath
 $symlinkPath

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

Static Public Member Functions inherited from Module
static clearMemoryCache ()
static invalidateConfigCacheForPath ($sourcePath)
Protected Attributes inherited from Module
 $bearsamppConf
 $bearsamppConfRaw
 $enable
 $name
 $release = 'N/A'
 $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 77 of file class.bin.memcached.php.

78 {
79 Log::initClass($this);
80 $this->reload($id, $type);
81 }
reload($id=null, $type=null)
static initClass($classInstance)

References Module\$id, Module\$type, Log\initClass(), and reload().

Here is the call graph for this function:

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 217 of file class.bin.memcached.php.

218 {
219 global $bearsamppWinbinder;
220
221 if (!Util::isValidPort($port)) {
222 Log::error($this->getName() . ' port not valid: ' . $port);
223
224 return false;
225 }
226
227 $port = intval($port);
228 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
229
230 $isPortInUse = ServiceHelper::isPortInUse($port);
231 if (!$checkUsed || $isPortInUse === false) {
232 // bearsampp.conf
233 $this->setPort($port);
234 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
235
236 // conf
237 $this->update();
238 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
239
240 return true;
241 }
242
243 Log::debug($this->getName() . ' port in used: ' . $port . ' - ' . $isPortInUse);
244
245 return $isPortInUse;
246 }
static debug($data, $file=null)
static error($data, $file=null)
update($sub=0, $showWindow=false)
static isPortInUse($port)
static isValidPort($port)

References $port, Log\debug(), Log\error(), Module\getName(), ServiceHelper\isPortInUse(), Util\isValidPort(), setPort(), and Module\update().

Here is the call graph for this function:

◆ 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 256 of file class.bin.memcached.php.

257 {
258 global $bearsamppLang, $bearsamppWinbinder;
259 $boxTitle = sprintf($bearsamppLang->getValue(Lang::CHECK_PORT_TITLE), $this->getName(), $port);
260
261 if (!Util::isValidPort($port)) {
262 Log::error($this->getName() . ' port not valid: ' . $port);
263
264 return false;
265 }
266
267 // Use fsockopen instead of memcache_connect to avoid file descriptor leaks in PHP 8.4+
268 $fp = @fsockopen(APP_LOCALHOST, $port, $errno, $errstr, 1);
269 if ($fp) {
270 // Port is open, verify it's memcached by sending a simple command
271 @stream_set_timeout($fp, 1);
272 @fwrite($fp, "version\r\n");
273 $response = @fgets($fp, 128);
274 @fclose($fp);
275
276 if ($response && strpos($response, 'VERSION') === 0) {
277 // Extract version from response (format: "VERSION x.x.x")
278 $version = trim(substr($response, 8));
279 Log::debug($this->getName() . ' port ' . $port . ' is used by: ' . $this->getName() . ' ' . $version);
280 if ($showWindow) {
281 $bearsamppWinbinder->messageBoxInfo(
282 sprintf($bearsamppLang->getValue(Lang::PORT_USED_BY), $port, $this->getName() . ' ' . $version),
283 $boxTitle
284 );
285 }
286
287 return true;
288 } else {
289 // Port is open but not responding as memcached
290 Log::debug($this->getName() . ' port ' . $port . ' is open but not responding as memcached');
291 if ($showWindow) {
292 $bearsamppWinbinder->messageBoxWarning(
293 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED_BY), $port),
294 $boxTitle
295 );
296 }
297
298 return false;
299 }
300 } else {
301 // Port is not open
302 Log::debug($this->getName() . ' port ' . $port . ' is not open');
303 if ($showWindow) {
304 $bearsamppWinbinder->messageBoxError(
305 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED), $port),
306 $boxTitle
307 );
308 }
309
310 return false;
311 }
312 }
global $bearsamppLang
const PORT_NOT_USED
const PORT_NOT_USED_BY
const CHECK_PORT_TITLE
const PORT_USED_BY
const APP_LOCALHOST
Definition root.php:21

References $bearsamppLang, $port, $response, Module\$version, APP_LOCALHOST, Lang\CHECK_PORT_TITLE, Log\debug(), Log\error(), Module\getName(), Util\isValidPort(), Lang\PORT_NOT_USED, Lang\PORT_NOT_USED_BY, and Lang\PORT_USED_BY.

Here is the call graph for this function:

◆ getExe()

getExe ( )

Retrieves the executable file path for the Memcached service.

Returns
string The executable file path.

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

455 {
456 return $this->exe;
457 }

References $exe.

◆ getLog()

getLog ( )

Retrieves the log file path for the Memcached service.

Returns
string The log file path.

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

445 {
446 return $this->log;
447 }

References $log.

◆ getMemory()

getMemory ( )

Retrieves the memory allocation for the Memcached service.

Returns
int The memory allocation in MB.

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

465 {
466 return $this->memory;
467 }

References $memory.

◆ getPort()

getPort ( )

Retrieves the port number for the Memcached service.

Returns
int The port number.

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

485 {
486 return $this->port;
487 }

References $port.

◆ getService()

getService ( )

Retrieves the service object for the Memcached service.

Returns
Win32Service The service object.

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

402 {
403 return $this->service;
404 }

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 187 of file class.bin.memcached.php.

188 {
189 global $bearsamppRegistry;
190
191 $exists = $bearsamppRegistry->exists(
193 'SYSTEM\CurrentControlSet\Services\\' . self::SERVICE_NAME . '\Parameters',
195 );
196 if ($exists) {
197 return $bearsamppRegistry->setExpandStringValue(
199 'SYSTEM\CurrentControlSet\Services\\' . self::SERVICE_NAME . '\Parameters',
201 sprintf(self::SERVICE_PARAMS, $this->memory, $this->port)
202 );
203 }
204
205 return false;
206 }
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 89 of file class.bin.memcached.php.

90 {
92 Log::reloadClass($this);
93
94 $this->name = $bearsamppLang->getValue(Lang::MEMCACHED);
95 $this->version = $bearsamppConfig->getRaw(self::ROOT_CFG_VERSION);
96 parent::reload($id, $type);
97
98 $this->enable = $this->enable && $bearsamppConfig->getRaw(self::ROOT_CFG_ENABLE);
99 $this->service = new Win32Service(self::SERVICE_NAME);
100 $this->log = Path::getLogsPath() . '/memcached.log';
101
102 if ($this->bearsamppConfRaw !== false) {
103 $this->exe = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_EXE];
104 $this->memory = intval($this->bearsamppConfRaw[self::LOCAL_CFG_MEMORY]);
105 $this->port = intval($this->bearsamppConfRaw[self::LOCAL_CFG_PORT]);
106 }
107
108 if (!$this->enable) {
109 Log::info($this->name . ' is not enabled!');
110
111 return;
112 }
113 if (!is_dir($this->currentPath)) {
114 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->currentPath));
115
116 return;
117 }
118 if (!is_dir($this->symlinkPath)) {
119 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->symlinkPath));
120
121 return;
122 }
123 if (!is_file($this->bearsamppConf)) {
124 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_CONF_NOT_FOUND), $this->name . ' ' . $this->version, $this->bearsamppConf));
125
126 return;
127 }
128 if (!is_file($this->exe)) {
129 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->exe));
130
131 return;
132 }
133 if (empty($this->memory)) {
134 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_INVALID_PARAMETER), self::LOCAL_CFG_MEMORY, $this->memory));
135
136 return;
137 }
138 if (empty($this->port)) {
139 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_INVALID_PARAMETER), self::LOCAL_CFG_PORT, $this->port));
140
141 return;
142 }
143
144 $nssm = new Nssm(self::SERVICE_NAME);
145 $nssm->setDisplayName(APP_TITLE . ' ' . $this->getName());
146 $nssm->setBinPath($this->exe);
147 $nssm->setParams(sprintf(self::SERVICE_PARAMS, $this->memory, $this->port));
148 $nssm->setStart(Nssm::SERVICE_DEMAND_START);
149 $nssm->setStdout(Path::getLogsPath() . '/memcached.out.log');
150 $nssm->setStderr(Path::getLogsPath() . '/memcached.err.log');
151
152 $this->service->setNssm($nssm);
153 }
global $bearsamppRoot
const ERROR_EXE_NOT_FOUND
const ERROR_CONF_NOT_FOUND
const MEMCACHED
const ERROR_INVALID_PARAMETER
const ERROR_FILE_NOT_FOUND
static info($data, $file=null)
static reloadClass($classInstance)
const SERVICE_DEMAND_START
static getLogsPath($aetrayPath=false)
global $bearsamppConfig
Definition homepage.php:46
const APP_TITLE
Definition root.php:19

References $bearsamppConfig, $bearsamppLang, $bearsamppRoot, Module\$id, Module\$type, APP_TITLE, Log\error(), Lang\ERROR_CONF_NOT_FOUND, Lang\ERROR_EXE_NOT_FOUND, Lang\ERROR_FILE_NOT_FOUND, Lang\ERROR_INVALID_PARAMETER, Path\getLogsPath(), Module\getName(), Log\info(), Lang\MEMCACHED, Log\reloadClass(), and Nssm\SERVICE_DEMAND_START.

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 160 of file class.bin.memcached.php.

161 {
162 $content = file_get_contents($this->bearsamppConf);
163
164 foreach ($params as $key => $value) {
165 $content = preg_replace('|' . $key . ' = .*|', $key . ' = ' . '"' . $value . '"', $content);
166 $this->bearsamppConfRaw[$key] = $value;
167 switch ($key) {
168 case self::LOCAL_CFG_MEMORY:
169 $this->memory = intval($value);
170 break;
171 case self::LOCAL_CFG_PORT:
172 $this->port = intval($value);
173 break;
174 }
175 }
176
177 file_put_contents($this->bearsamppConf, $content);
178
179 self::invalidateConfigCacheForPath($this->bearsamppConf);
180 }
static invalidateConfigCacheForPath($sourcePath)

References Module\invalidateConfigCacheForPath().

Here is the call graph for this function:

◆ 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 412 of file class.bin.memcached.php.

413 {
414 global $bearsamppConfig, $bearsamppLang, $bearsamppWinbinder;
415
416 if ($enabled == Config::ENABLED && !is_dir($this->currentPath)) {
417 Log::debug($this->getName() . ' cannot be enabled because bundle ' . $this->getVersion() . ' does not exist in ' . $this->currentPath);
418 if ($showWindow) {
419 $bearsamppWinbinder->messageBoxError(
420 sprintf($bearsamppLang->getValue(Lang::ENABLE_BUNDLE_NOT_EXIST), $this->getName(), $this->getVersion(), $this->currentPath),
421 sprintf($bearsamppLang->getValue(Lang::ENABLE_TITLE), $this->getName())
422 );
423 }
424 $enabled = Config::DISABLED;
425 }
426
427 Log::info($this->getName() . ' switched to ' . ($enabled == Config::ENABLED ? 'enabled' : 'disabled'));
428 $this->enable = $enabled == Config::ENABLED;
429 $bearsamppConfig->replace(self::ROOT_CFG_ENABLE, $enabled);
430
431 $this->reload();
432 if ($this->enable) {
433 ServiceHelper::installService($this, $this->port, null, $showWindow);
434 } else {
435 ServiceHelper::removeService($this->service, $this->name);
436 }
437 }
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, Log\debug(), Config\DISABLED, Lang\ENABLE_BUNDLE_NOT_EXIST, Lang\ENABLE_TITLE, Config\ENABLED, Module\getName(), Module\getVersion(), Log\info(), ServiceHelper\installService(), reload(), and ServiceHelper\removeService().

Here is the call graph for this function:

◆ setMemory()

setMemory ( $memory)

Sets the memory allocation for the Memcached service.

Parameters
int$memoryThe memory allocation in MB.

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

475 {
476 $this->replace(self::LOCAL_CFG_MEMORY, $memory);
477 }
replace($key, $value)

References $memory, and Module\replace().

Here is the call graph for this function:

◆ setPort()

setPort ( $port)

Sets the port number for the Memcached service.

Parameters
int$portThe port number.

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

495 {
496 $this->replace(self::LOCAL_CFG_PORT, $port);
497 }

References $port, and Module\replace().

Referenced by changePort().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setVersion()

setVersion ( $version)

Sets the version of the Memcached service.

Parameters
string$versionThe version to set.

Reimplemented from Module.

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

389 {
390 global $bearsamppConfig;
391 $this->version = $version;
392 $bearsamppConfig->replace(self::ROOT_CFG_VERSION, $version);
393 $this->reload();
394 }

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

Referenced by updateConfig().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 322 of file class.bin.memcached.php.

323 {
324 Log::debug('Switch ' . $this->name . ' version to ' . $version);
325
326 return $this->updateConfig($version, 0, $showWindow);
327 }
updateConfig($version=null, $sub=0, $showWindow=false)

References Module\$version, Log\debug(), and updateConfig().

Here is the call graph for this function:

◆ 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 338 of file class.bin.memcached.php.

339 {
340 global $bearsamppLang, $bearsamppApps, $bearsamppWinbinder;
341
342 if (!$this->enable) {
343 return true;
344 }
345
346 $version = $version == null ? $this->version : $version;
347 Log::debug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'Update ' . $this->name . ' ' . $version . ' config');
348
349 $boxTitle = sprintf($bearsamppLang->getValue(Lang::SWITCH_VERSION_TITLE), $this->getName(), $version);
350
351 $bearsamppConf = str_replace('memcached' . $this->getVersion(), 'memcached' . $version, $this->bearsamppConf);
352 if (!file_exists($bearsamppConf)) {
353 Log::error('bearsampp config files not found for ' . $this->getName() . ' ' . $version);
354 if ($showWindow) {
355 $bearsamppWinbinder->messageBoxError(
356 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_NOT_FOUND_ERROR), $this->getName() . ' ' . $version),
357 $boxTitle
358 );
359 }
360
361 return false;
362 }
363
364 $bearsamppConfRaw = parse_ini_file($bearsamppConf);
365 if ($bearsamppConfRaw === false || !isset($bearsamppConfRaw[self::ROOT_CFG_VERSION]) || $bearsamppConfRaw[self::ROOT_CFG_VERSION] != $version) {
366 Log::error('bearsampp config file malformed for ' . $this->getName() . ' ' . $version);
367 if ($showWindow) {
368 $bearsamppWinbinder->messageBoxError(
369 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_MALFORMED_ERROR), $this->getName() . ' ' . $version),
370 $boxTitle
371 );
372 }
373
374 return false;
375 }
376
377 // bearsampp.conf
378 $this->setVersion($version);
379
380 return true;
381 }
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, Log\debug(), Log\error(), Module\getName(), Module\getVersion(), setVersion(), and Lang\SWITCH_VERSION_TITLE.

Referenced by switchVersion().

Here is the call graph for this function:
Here is the caller graph for this function:

Field Documentation

◆ $exe

$exe
private

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

Referenced by getExe().

◆ $log

$log
private

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

Referenced by getLog().

◆ $memory

$memory
private

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

Referenced by getMemory(), and setMemory().

◆ $port

$port
private

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

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

◆ $service

$service
private

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

Referenced by getService().

◆ LOCAL_CFG_EXE

const LOCAL_CFG_EXE = 'memcachedExe'

Configuration key for the Memcached executable in the local configuration.

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

◆ LOCAL_CFG_MEMORY

const LOCAL_CFG_MEMORY = 'memcachedMemory'

Configuration key for the Memcached memory allocation in the local configuration.

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

◆ LOCAL_CFG_PORT

const LOCAL_CFG_PORT = 'memcachedPort'

Configuration key for the Memcached port in the local configuration.

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

◆ ROOT_CFG_ENABLE

const ROOT_CFG_ENABLE = 'memcachedEnable'

Configuration key for the Memcached enable flag in the root configuration.

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

◆ ROOT_CFG_VERSION

const ROOT_CFG_VERSION = 'memcachedVersion'

Configuration key for the Memcached version in the root configuration.

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

◆ SERVICE_NAME

◆ SERVICE_PARAMS

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

The command line parameters passed to the Memcached service.

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


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