Bearsampp 2025.8.29
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 if (function_exists('memcache_connect')) {
212 $memcache = @memcache_connect('127.0.0.1', $port);
213 if ($memcache) {
214 $memcacheVersion = memcache_get_version($memcache);
215 Util::logDebug($this->getName() . ' port ' . $port . ' is used by: ' . $this->getName() . ' ' . $memcacheVersion);
216 memcache_close($memcache);
217 if ($showWindow) {
218 $bearsamppWinbinder->messageBoxInfo(
219 sprintf($bearsamppLang->getValue(Lang::PORT_USED_BY), $port, $this->getName() . ' ' . $memcacheVersion),
220 $boxTitle
221 );
222 }
223 return true;
224 }
225 } else {
226 $fp = @fsockopen('127.0.0.1', $port, $errno, $errstr, 3);
227 if (!$fp) {
228 Util::logDebug($this->getName() . ' port ' . $port . ' is used by another application');
229 if ($showWindow) {
230 $bearsamppWinbinder->messageBoxWarning(
231 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED_BY), $port),
232 $boxTitle
233 );
234 }
235 } else {
236 Util::logDebug($this->getName() . ' port ' . $port . ' is not used');
237 if ($showWindow) {
238 $bearsamppWinbinder->messageBoxError(
239 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED), $port),
240 $boxTitle
241 );
242 }
243 fclose($fp);
244 }
245 }
246
247 return false;
248 }
global $bearsamppLang
const PORT_NOT_USED
const PORT_NOT_USED_BY
const CHECK_PORT_TITLE
const PORT_USED_BY

References $bearsamppLang, $port, 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 379 of file class.bin.memcached.php.

379 {
380 return $this->exe;
381 }

References $exe.

◆ getLog()

getLog ( )

Retrieves the log file path for the Memcached service.

Returns
string The log file path.

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

370 {
371 return $this->log;
372 }

References $log.

◆ getMemory()

getMemory ( )

Retrieves the memory allocation for the Memcached service.

Returns
int The memory allocation in MB.

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

388 {
389 return $this->memory;
390 }

References $memory.

◆ getPort()

getPort ( )

Retrieves the port number for the Memcached service.

Returns
int The port number.

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

406 {
407 return $this->port;
408 }

References $port.

◆ getService()

getService ( )

Retrieves the service object for the Memcached service.

Returns
Win32Service The service object.

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

329 {
330 return $this->service;
331 }

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:27
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 339 of file class.bin.memcached.php.

339 {
340 global $bearsamppConfig, $bearsamppLang, $bearsamppWinbinder;
341
342 if ($enabled == Config::ENABLED && !is_dir($this->currentPath)) {
343 Util::logDebug($this->getName() . ' cannot be enabled because bundle ' . $this->getVersion() . ' does not exist in ' . $this->currentPath);
344 if ($showWindow) {
345 $bearsamppWinbinder->messageBoxError(
346 sprintf($bearsamppLang->getValue(Lang::ENABLE_BUNDLE_NOT_EXIST), $this->getName(), $this->getVersion(), $this->currentPath),
347 sprintf($bearsamppLang->getValue(Lang::ENABLE_TITLE), $this->getName())
348 );
349 }
350 $enabled = Config::DISABLED;
351 }
352
353 Util::logInfo($this->getName() . ' switched to ' . ($enabled == Config::ENABLED ? 'enabled' : 'disabled'));
354 $this->enable = $enabled == Config::ENABLED;
355 $bearsamppConfig->replace(self::ROOT_CFG_ENABLE, $enabled);
356
357 $this->reload();
358 if ($this->enable) {
359 Util::installService($this, $this->port, null, $showWindow);
360 } else {
361 Util::removeService($this->service, $this->name);
362 }
363 }
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 397 of file class.bin.memcached.php.

397 {
398 $this->replace(self::LOCAL_CFG_MEMORY, $memory);
399 }
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 415 of file class.bin.memcached.php.

415 {
416 $this->replace(self::LOCAL_CFG_PORT, $port);
417 }

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

317 {
318 global $bearsamppConfig;
319 $this->version = $version;
320 $bearsamppConfig->replace(self::ROOT_CFG_VERSION, $version);
321 $this->reload();
322 }

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

257 {
258 Util::logDebug('Switch ' . $this->name . ' version to ' . $version);
259 return $this->updateConfig($version, 0, $showWindow);
260 }
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 270 of file class.bin.memcached.php.

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