Bearsampp
2026.7.28
Toggle main menu visibility
Loading...
Searching...
No Matches
class.action.reload.php
Go to the documentation of this file.
1
<?php
2
/*
3
* Copyright (c) 2021-2024 Bearsampp
4
* License: GNU General Public License version 3 or later; see LICENSE.txt
5
* Author: Bear
6
* Website: https://bearsampp.com
7
* Github: https://github.com/Bearsampp
8
*/
9
17
class
ActionReload
18
{
31
public
function
__construct
($args)
32
{
33
global
$bearsamppRoot
,
$bearsamppCore
,
$bearsamppConfig
,
$bearsamppBins
, $bearsamppApps,
$bearsamppHomepage
;
34
35
// If the executable file exists, return early.
36
if
(file_exists(
$bearsamppCore
->getExec())) {
37
return
;
38
}
39
40
// Start loading process
41
Util::startLoading
();
42
43
// Capture which database services are currently running so we can restart them once
44
// the reload has finished. A running database server keeps executing the binary it was
45
// launched with, so it keeps reporting the old version to clients such as phpMyAdmin
46
// until it is restarted from the updated 'current' symlink. Only the database services
47
// are handled here: the rest of the reload already refreshes the other modules, and
48
// restarting every service needlessly disrupts them (and makes the tray flash a window
49
// per service).
50
$dbServiceNames = array(
51
BinMysql::SERVICE_NAME
,
52
BinMariadb::SERVICE_NAME
,
53
BinPostgresql::SERVICE_NAME
,
54
);
55
56
$runningServices = array();
57
$stoppedServices = array();
58
$services =
$bearsamppBins
->getServices();
59
foreach
($dbServiceNames as $serviceName) {
60
if
(isset($services[$serviceName]) && $services[$serviceName] !=
null
&& $services[$serviceName]->isRunning()) {
61
$runningServices[] = $serviceName;
62
Log::info
(
'Stopping '
. $serviceName .
' before reload'
);
63
if
($services[$serviceName]->stop()) {
64
$stoppedServices[] = $serviceName;
65
}
else
{
66
Log::error
(
'Failed to stop '
. $serviceName .
' before reload'
);
67
}
68
}
69
}
70
71
try
{
72
// Scan and update paths in bin configuration files (replaces path placeholders
73
// such as ~BEARSAMPP_LIN_PATH~ for the newly selected version, like switchVersion does)
74
$pathsToScan = array();
75
76
// MySQL
77
$folderList =
Util::getFolderList
(
Path::getModuleRootPath
(
$bearsamppBins
->getMysql()));
78
if
($folderList ===
false
) {
79
Log::error
(
'Failed to scan MySQL module folder list for path updates'
);
80
$folderList = array();
81
}
82
foreach
($folderList as $folder) {
83
$pathsToScan[] = array(
84
'path'
=>
Path::getModuleRootPath
(
$bearsamppBins
->getMysql()) .
'/'
. $folder,
85
'includes'
=> array(
'my.ini'
),
86
'recursive'
=>
false
87
);
88
}
89
90
// MariaDB
91
$folderList =
Util::getFolderList
(
Path::getModuleRootPath
(
$bearsamppBins
->getMariadb()));
92
if
($folderList ===
false
) {
93
Log::error
(
'Failed to scan MariaDB module folder list for path updates'
);
94
$folderList = array();
95
}
96
foreach
($folderList as $folder) {
97
$pathsToScan[] = array(
98
'path'
=>
Path::getModuleRootPath
(
$bearsamppBins
->getMariadb()) .
'/'
. $folder,
99
'includes'
=> array(
'my.ini'
),
100
'recursive'
=>
false
101
);
102
}
103
104
// Update paths in scanned files
105
if
(!empty($pathsToScan)) {
106
Path::changePath
(
Util::getFilesToScan
($pathsToScan));
107
}
108
109
// Reload bins and apps to recreate symlinks if needed
110
$bearsamppBins
->reload();
111
$bearsamppApps->reload();
112
113
// Refresh application configs (ports/credentials) against the reloaded bins
114
$bearsamppApps->getPhpmyadmin()->update();
115
$bearsamppApps->getPhppgadmin()->update();
116
117
// Refresh hostname in the configuration
118
$bearsamppConfig
->replace(
Config::CFG_HOSTNAME
, gethostname());
119
120
// Refresh launch startup setting in the configuration
121
$bearsamppConfig
->replace(
Config::CFG_LAUNCH_STARTUP
,
Util::isLaunchStartup
() ?
Config::ENABLED
:
Config::DISABLED
);
122
123
// Check and update the browser setting in the configuration
124
$currentBrowser =
$bearsamppConfig
->getBrowser();
125
if
(empty($currentBrowser) || !file_exists($currentBrowser)) {
126
$bearsamppConfig
->replace(
Config::CFG_BROWSER
,
Win32Native::getDefaultBrowser
());
127
}
128
129
// Process and update the bearsampp.ini file
130
file_put_contents(
Path::getIniFilePath
(),
Util::utf8ToCp1252
(
TplApp::process
()));
131
132
// Process and update the PowerShell configuration
133
TplPowerShell::process
();
134
135
// Refresh PEAR version cache file
136
$bearsamppBins
->getPhp()->getPearVersion();
137
138
// Rebuild alias homepage content
139
$bearsamppHomepage
->refreshAliasContent();
140
141
// Rebuild _commons.js content
142
$bearsamppHomepage
->refreshCommonsJsContent();
143
}
finally
{
144
// Restart the services that were successfully stopped. Because the bins have
145
// been reloaded and the 'current' symlink now points to the selected version, the
146
// service starts from the new binary and reports the new version to clients such
147
// as phpMyAdmin. Fresh service references are taken from the reloaded bins.
148
$failedServices = array();
149
if
(!empty($stoppedServices)) {
150
$services =
$bearsamppBins
->getServices();
151
foreach
($stoppedServices as $serviceName) {
152
if
(isset($services[$serviceName]) && $services[$serviceName] !=
null
) {
153
Log::info
(
'Restarting '
. $serviceName .
' after reload'
);
154
if
(!$services[$serviceName]->start()) {
155
Log::error
(
'Failed to restart '
. $serviceName .
' after reload'
);
156
$failedServices[] = $serviceName;
157
}
158
}
159
}
160
}
161
162
// Write reload status file for UI to poll and display results
163
$reloadStatusFile =
Path::getLogsPath
() .
'/reload-status.json'
;
164
$reloadStatus = array(
165
'timestamp'
=> time(),
166
'stoppedServices'
=> $stoppedServices,
167
'failedServices'
=> $failedServices,
168
'success'
=> empty($failedServices)
169
);
170
171
// Atomic write: write to temp file, then rename to final path to prevent partial reads
172
$reloadStatusTmpFile = $reloadStatusFile .
'.tmp'
;
173
file_put_contents($reloadStatusTmpFile, json_encode($reloadStatus, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), LOCK_EX);
174
rename($reloadStatusTmpFile, $reloadStatusFile);
175
176
// Stop loading process
177
Util::stopLoading
();
178
}
179
}
180
}
181
$bearsamppBins
global $bearsamppBins
Definition
ajax.apache.php:16
$bearsamppRoot
global $bearsamppRoot
Definition
ajax.apache.php:16
$bearsamppCore
global $bearsamppCore
Definition
ajax.latestversion.php:24
ActionReload
Definition
class.action.reload.php:18
ActionReload\__construct
__construct($args)
Definition
class.action.reload.php:31
BinMariadb\SERVICE_NAME
const SERVICE_NAME
Definition
class.bin.mariadb.php:18
BinMysql\SERVICE_NAME
const SERVICE_NAME
Definition
class.bin.mysql.php:18
BinPostgresql\SERVICE_NAME
const SERVICE_NAME
Definition
class.bin.postgresql.php:18
Config\CFG_HOSTNAME
const CFG_HOSTNAME
Definition
class.config.php:30
Config\DISABLED
const DISABLED
Definition
class.config.php:36
Config\CFG_BROWSER
const CFG_BROWSER
Definition
class.config.php:31
Config\ENABLED
const ENABLED
Definition
class.config.php:35
Config\CFG_LAUNCH_STARTUP
const CFG_LAUNCH_STARTUP
Definition
class.config.php:33
Log\info
static info($data, $file=null)
Definition
class.log.php:627
Log\error
static error($data, $file=null)
Definition
class.log.php:650
Path\getLogsPath
static getLogsPath($aetrayPath=false)
Definition
class.path.php:626
Path\changePath
static changePath($filesToScan, $rootPath=null)
Definition
class.path.php:201
Path\getModuleRootPath
static getModuleRootPath($module)
Definition
class.path.php:32
Path\getIniFilePath
static getIniFilePath($aetrayPath=false)
Definition
class.path.php:538
TplApp\process
static process()
Definition
class.tpl.app.php:43
TplPowerShell\process
static process()
Definition
class.tpl.powershell.php:50
Util\isLaunchStartup
static isLaunchStartup()
Definition
class.util.php:385
Util\utf8ToCp1252
static utf8ToCp1252($data)
Definition
class.util.php:510
Util\getFolderList
static getFolderList($path)
Definition
class.util.php:1482
Util\startLoading
static startLoading()
Definition
class.util.php:530
Util\getFilesToScan
static getFilesToScan($path=null, $useCache=true, $forceRefresh=false)
Definition
class.util.php:602
Util\stopLoading
static stopLoading()
Definition
class.util.php:550
Win32Native\getDefaultBrowser
static getDefaultBrowser()
Definition
class.win32native.php:1094
$bearsamppConfig
global $bearsamppConfig
Definition
homepage.php:41
$bearsamppHomepage
global $bearsamppHomepage
Definition
homepage.php:41
sandbox
core
classes
actions
class.action.reload.php
Generated by
1.17.0