Skip to content

Commit

Permalink
Add comprehensive PHPDoc comments to class.bin.php, `class.bin.node…
Browse files Browse the repository at this point in the history
…js.php`, and `class.bins.php` for better documentation and clarity
  • Loading branch information
N6REJ committed Aug 23, 2024
1 parent 6e2fed2 commit 82c8320
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 16 deletions.
48 changes: 43 additions & 5 deletions core/classes/bins/class.bin.nodejs.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
<?php
/*
* Copyright (c) 2021-2024 Bearsampp
* License: GNU General Public License version 3 or later; see LICENSE.txt
* Author: Bear
* Website: https://bearsampp.com
* Github: https://github.com/Bearsampp
*
* * Copyright (c) 2021-2024 Bearsampp
* * License: GNU General Public License version 3 or later; see LICENSE.txt
* * Website: https://bearsampp.com
* * Github: https://github.com/Bearsampp
*
*/

/**
* Class BinNodejs
*
* The `BinNodejs` class extends the `Module` class and provides functionalities specific to managing
* the Node.js module within the Bearsampp application. It includes methods for reloading the module
* configuration, switching versions, enabling/disabling the module, and retrieving various configuration
* paths such as the executable, configuration file, variables file, npm executable, and launch script.
*
* Constants:
* - `ROOT_CFG_ENABLE`: Configuration key for enabling the Node.js module.
* - `ROOT_CFG_VERSION`: Configuration key for the Node.js version.
* - `LOCAL_CFG_EXE`: Configuration key for the Node.js executable.
* - `LOCAL_CFG_VARS`: Configuration key for the Node.js variables.
* - `LOCAL_CFG_NPM`: Configuration key for the npm executable.
* - `LOCAL_CFG_LAUNCH`: Configuration key for the Node.js launch script.
* - `LOCAL_CFG_CONF`: Configuration key for the Node.js configuration file.
*
* Properties:
* - `exe`: Path to the Node.js executable.
* - `conf`: Path to the Node.js configuration file.
* - `vars`: Path to the Node.js variables file.
* - `npm`: Path to the npm executable.
* - `launch`: Path to the Node.js launch script.
*
* Methods:
* - `__construct($id, $type)`: Constructs a `BinNodejs` object and initializes the module with the given ID and type.
* - `reload($id = null, $type = null)`: Reloads the module configuration based on the provided ID and type.
* - `switchVersion($version, $showWindow = false)`: Switches the Node.js version to the specified version.
* - `updateConfig($version = null, $sub = 0, $showWindow = false)`: Updates the module configuration with a specific version.
* - `setVersion($version)`: Sets the version of the module.
* - `setEnable($enabled, $showWindow = false)`: Enables or disables the module.
* - `getExe()`: Retrieves the executable path for Node.js.
* - `getConf()`: Retrieves the configuration file path for Node.js.
* - `getVars()`: Retrieves the variables file path for Node.js.
* - `getNpm()`: Retrieves the npm executable path for Node.js.
* - `getLaunch()`: Retrieves the launch script path for Node.js.
*/
class BinNodejs extends Module
{
const ROOT_CFG_ENABLE = 'nodejsEnable';
Expand Down
152 changes: 146 additions & 6 deletions core/classes/bins/class.bin.php.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
<?php

/*
*
* * Copyright (c) 2021-2024 Bearsampp
* * License: GNU General Public License version 3 or later; see LICENSE.txt
* * Website: https://bearsampp.com
* * Github: https://github.com/Bearsampp
*
*/

/**
* Class BinPhp
*
* This class extends the Module class and provides functionalities specific to managing PHP binaries within the Bearsampp application.
* It includes methods for reloading configurations, switching versions, updating configurations, and retrieving various PHP settings and extensions.
*/
class BinPhp extends Module
{
const ROOT_CFG_ENABLE = 'phpEnable';
Expand Down Expand Up @@ -86,11 +100,23 @@ class BinPhp extends Module
private $conf;
private $pearExe;

/**
* Constructor for the BinPhp class.
*
* @param string $id The ID of the module.
* @param string $type The type of the module.
*/
public function __construct($id, $type) {
Util::logInitClass($this);
$this->reload($id, $type);
}

/**
* Reloads the module configuration based on the provided ID and type.
*
* @param string|null $id The ID of the module. If null, the current ID is used.
* @param string|null $type The type of the module. If null, the current type is used.
*/
public function reload($id = null, $type = null) {
global $bearsamppRoot, $bearsamppConfig, $bearsamppBins, $bearsamppLang;
Util::logReloadClass($this);
Expand Down Expand Up @@ -140,11 +166,26 @@ public function reload($id = null, $type = null) {
}
}

/**
* Switches the PHP version to the specified version.
*
* @param string $version The version to switch to.
* @param bool $showWindow Whether to show a window during the switch process.
* @return bool True if the switch was successful, false otherwise.
*/
public function switchVersion($version, $showWindow = false) {
Util::logDebug('Switch ' . $this->name . ' version to ' . $version);
return $this->updateConfig($version, 0, $showWindow);
}

/**
* Updates the PHP configuration to the specified version.
*
* @param string|null $version The version to update to. If null, the current version is used.
* @param int $sub The sub-level for logging indentation.
* @param bool $showWindow Whether to show a window during the update process.
* @return bool True if the update was successful, false otherwise.
*/
protected function updateConfig($version = null, $sub = 0, $showWindow = false) {
global $bearsamppLang, $bearsamppBins, $bearsamppApps, $bearsamppWinbinder;

Expand All @@ -157,13 +198,10 @@ protected function updateConfig($version = null, $sub = 0, $showWindow = false)

$boxTitle = sprintf($bearsamppLang->getValue(Lang::SWITCH_VERSION_TITLE), $this->getName(), $version);

//$phpPath = str_replace('php' . $this->getVersion(), 'php' . $version, $this->getCurrentPath());
$conf = str_replace('php' . $this->getVersion(), 'php' . $version, $this->getConf());
$bearsamppConf = str_replace('php' . $this->getVersion(), 'php' . $version, $this->bearsamppConf);

$tsDll = $this->getTsDll($version);
//$apacheShortVersion = substr(str_replace('.', '', $bearsamppBins->getApache()->getVersion()), 0, 2);
//$apachePhpModuleName = $tsDll !== false ? substr($tsDll, 0, 4) . '_module' : null;
$apachePhpModulePath = $this->getApacheModule($bearsamppBins->getApache()->getVersion(), $version);

Util::logDebug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'PHP TsDll found: ' . $tsDll);
Expand Down Expand Up @@ -221,6 +259,11 @@ protected function updateConfig($version = null, $sub = 0, $showWindow = false)
return true;
}

/**
* Retrieves the PHP settings.
*
* @return array An associative array of PHP settings.
*/
public function getSettings() {
return array(
'Language options' => array(
Expand Down Expand Up @@ -341,6 +384,11 @@ public function getSettings() {
);
}

/**
* Retrieves the default, off, and current values for various PHP settings.
*
* @return array An associative array where the key is the setting name and the value is an array containing the default, off, and current values.
*/
public function getSettingsValues() {
return array(
self::INI_SHORT_OPEN_TAG => array('On', 'Off', 'On'),
Expand Down Expand Up @@ -410,6 +458,12 @@ public function getSettingsValues() {
);
}

/**
* Checks if a specific PHP setting is active.
*
* @param string $name The name of the setting to check.
* @return bool True if the setting is active, false otherwise.
*/
public function isSettingActive($name) {
$settingsValues = $this->getSettingsValues();

Expand All @@ -424,6 +478,12 @@ public function isSettingActive($name) {
return false;
}

/**
* Checks if a specific PHP setting exists in the configuration file.
*
* @param string $name The name of the setting to check.
* @return bool True if the setting exists, false otherwise.
*/
public function isSettingExists($name) {
$confContent = file($this->getConf());
foreach ($confContent as $row) {
Expand All @@ -435,6 +495,11 @@ public function isSettingExists($name) {
return false;
}

/**
* Retrieves the list of PHP extensions from both the configuration file and the extensions folder.
*
* @return array An associative array where the key is the extension name and the value is the status (on/off).
*/
public function getExtensions() {
$fromFolder = $this->getExtensionsFromConf();
$fromConf = $this->getExtensionsFromFolder();
Expand All @@ -443,13 +508,24 @@ public function getExtensions() {
return $result;
}

/**
* Checks if a specific PHP extension is excluded from the list.
*
* @param string $ext The name of the extension to check.
* @return bool True if the extension is excluded, false otherwise.
*/
private function isExtensionExcluded($ext) {
return in_array($ext, array(
'opcache',
'xdebug'
));
}

/**
* Retrieves the list of PHP extensions from the configuration file.
*
* @return array An associative array where the key is the extension name and the value is the status (on/off).
*/
public function getExtensionsFromConf() {
$result = array();

Expand All @@ -473,6 +549,11 @@ public function getExtensionsFromConf() {
return $result;
}

/**
* Retrieves the list of currently loaded PHP extensions.
*
* @return array An array of extension names that are currently loaded.
*/
public function getExtensionsLoaded() {
$result = array();
foreach ($this->getExtensionsFromConf() as $name => $status) {
Expand All @@ -483,6 +564,11 @@ public function getExtensionsLoaded() {
return $result;
}

/**
* Retrieves the list of PHP extensions from the extensions folder.
*
* @return array An associative array where the key is the extension name and the value is the status (off).
*/
public function getExtensionsFromFolder() {
$result = array();

Expand All @@ -506,6 +592,13 @@ public function getExtensionsFromFolder() {
return $result;
}

/**
* Retrieves the path to the Apache module for the specified Apache and PHP versions.
*
* @param string $apacheVersion The version of Apache.
* @param string|null $phpVersion The version of PHP. If null, the current PHP version is used.
* @return string|false The path to the Apache module, or false if not found.
*/
public function getApacheModule($apacheVersion, $phpVersion = null) {
$apacheVersion = substr(str_replace('.', '', $apacheVersion), 0, 2);
$phpVersion = $phpVersion == null ? $this->getVersion() : $phpVersion;
Expand All @@ -529,6 +622,12 @@ public function getApacheModule($apacheVersion, $phpVersion = null) {
return false;
}

/**
* Retrieves the name of the PHP Thread Safe (TS) DLL for the specified PHP version.
*
* @param string|null $phpVersion The version of PHP. If null, the current PHP version is used.
* @return string|false The name of the PHP TS DLL, or false if not found.
*/
public function getTsDll($phpVersion = null) {
$phpVersion = $phpVersion == null ? $this->getVersion() : $phpVersion;
$currentPath = str_replace('php' . $this->getVersion(), 'php' . $phpVersion, $this->getCurrentPath());
Expand All @@ -542,13 +641,24 @@ public function getTsDll($phpVersion = null) {
return false;
}

/**
* Sets the PHP version and updates the configuration.
*
* @param string $version The version to set.
*/
public function setVersion($version) {
global $bearsamppConfig;
$this->version = $version;
$bearsamppConfig->replace(self::ROOT_CFG_VERSION, $version);
$this->reload();
}

/**
* Enables or disables the module and updates the configuration.
*
* @param int $enabled The enable status (Config::ENABLED or Config::DISABLED).
* @param bool $showWindow Whether to show a window during the process.
*/
public function setEnable($enabled, $showWindow = false) {
global $bearsamppConfig, $bearsamppBins, $bearsamppLang, $bearsamppWinbinder;

Expand All @@ -575,31 +685,61 @@ public function setEnable($enabled, $showWindow = false) {
}
}

/**
* Retrieves the path to the PHP error log file.
*
* @return string The path to the PHP error log file.
*/
public function getErrorLog() {
return $this->errorLog;
}

/**
* Retrieves the path to the PHP CLI executable.
*
* @return string The path to the PHP CLI executable.
*/
public function getCliExe() {
return $this->cliExe;
}

/**
* Retrieves the path to the PHP CLI silent executable.
*
* @return string The path to the PHP CLI silent executable.
*/
public function getCliSilentExe() {
return $this->cliSilentExe;
}

/**
* Retrieves the path to the PHP configuration file.
*
* @return string The path to the PHP configuration file.
*/
public function getConf() {
return $this->conf;
}

/**
* Retrieves the path to the PHP PEAR executable.
*
* @return string The path to the PHP PEAR executable.
*/
public function getPearExe() {
return $this->pearExe;
}

/**
* Retrieves the version of PEAR.
*
* @param bool $cache Whether to use the cached version.
* @return string|null The PEAR version, or null if not found.
*/
public function getPearVersion($cache = false) {
$cacheFile = $this->getCurrentPath() . '/pear/version';
if (!$cache) {
file_put_contents($cacheFile, Batch::getPearVersion());
}
return file_exists($cacheFile) ? file_get_contents($cacheFile) : null;
}
}
}}
16 changes: 11 additions & 5 deletions core/classes/bins/class.bins.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<?php
/*
* Copyright (c) 2021-2024 Bearsampp
* License: GNU General Public License version 3 or later; see LICENSE.txt
* Author: Bear
* Website: https://bearsampp.com
* Github: https://github.com/Bearsampp
*
* * Copyright (c) 2021-2024 Bearsampp
* * License: GNU General Public License version 3 or later; see LICENSE.txt
* * Website: https://bearsampp.com
* * Github: https://github.com/Bearsampp
*
*/

/**
* The `Bins` class manages various bin modules such as Mailhog, Mailpit, Memcached, Apache, PHP, MySQL, MariaDB, PostgreSQL, Node.js, FileZilla, and Xlight.
* It provides methods to initialize, reload, update, and retrieve these modules.
* The class also handles logging and service management for the enabled bin modules.
*/
class Bins
{
const TYPE = 'bins';
Expand Down

0 comments on commit 82c8320

Please sign in to comment.