Bearsampp 2026.7.28
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
18{
31 public function __construct($args)
32 {
34
35 // If the executable file exists, return early.
36 if (file_exists($bearsamppCore->getExec())) {
37 return;
38 }
39
40 // Start loading process
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(
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
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
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)) {
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
122
123 // Check and update the browser setting in the configuration
124 $currentBrowser = $bearsamppConfig->getBrowser();
125 if (empty($currentBrowser) || !file_exists($currentBrowser)) {
127 }
128
129 // Process and update the bearsampp.ini file
131
132 // Process and update the PowerShell configuration
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
178 }
179 }
180}
181
global $bearsamppBins
global $bearsamppRoot
global $bearsamppCore
const SERVICE_NAME
const CFG_HOSTNAME
const DISABLED
const CFG_BROWSER
const ENABLED
const CFG_LAUNCH_STARTUP
static info($data, $file=null)
static error($data, $file=null)
static getLogsPath($aetrayPath=false)
static changePath($filesToScan, $rootPath=null)
static getModuleRootPath($module)
static getIniFilePath($aetrayPath=false)
static process()
static isLaunchStartup()
static utf8ToCp1252($data)
static getFolderList($path)
static startLoading()
static getFilesToScan($path=null, $useCache=true, $forceRefresh=false)
static stopLoading()
static getDefaultBrowser()
global $bearsamppConfig
Definition homepage.php:41
global $bearsamppHomepage
Definition homepage.php:41