diff --git a/phpmon/Domain/Menu/MainMenu.swift b/phpmon/Domain/Menu/MainMenu.swift index 9e03c8c..35697f0 100644 --- a/phpmon/Domain/Menu/MainMenu.swift +++ b/phpmon/Domain/Menu/MainMenu.swift @@ -218,6 +218,12 @@ class MainMenu: NSObject, NSWindowDelegate, NSMenuDelegate { } } + @objc func stopAllServices() { + waitAndExecute { + Actions.stopAllServices() + } + } + @objc func restartNginx() { waitAndExecute { Actions.restartNginx() @@ -289,26 +295,29 @@ class MainMenu: NSObject, NSWindowDelegate, NSMenuDelegate { // Update the menu update() + let completion = { + // Mark as no longer busy + App.shared.busy = false + + // Perform UI updates on main thread + DispatchQueue.main.async { [self] in + updatePhpVersionInStatusBar() + update() + + // Send a notification that the switch has been completed + LocalNotification.send( + title: String(format: "notification.version_changed_title".localized, sender.version), + subtitle: String(format: "notification.version_changed_desc".localized, sender.version) + ) + } + } + // Switch the PHP version Actions.switchToPhpVersion( version: sender.version, - availableVersions: App.shared.availablePhpVersions + availableVersions: App.shared.availablePhpVersions, + completed: completion ) - - // Mark as no longer busy - App.shared.busy = false - - // Perform UI updates on main thread - DispatchQueue.main.async { [self] in - updatePhpVersionInStatusBar() - update() - - // Send a notification that the switch has been completed - LocalNotification.send( - title: String(format: "notification.version_changed_title".localized, sender.version), - subtitle: String(format: "notification.version_changed_desc".localized, sender.version) - ) - } } } diff --git a/phpmon/Domain/Terminal/Actions.swift b/phpmon/Domain/Terminal/Actions.swift index 8fb4a6c..dee5f79 100644 --- a/phpmon/Domain/Terminal/Actions.swift +++ b/phpmon/Domain/Terminal/Actions.swift @@ -78,6 +78,13 @@ class Actions { brew("services restart dnsmasq", sudo: true) } + public static func stopAllServices() + { + brew("services stop \(App.phpInstall!.formula)", sudo: true) + brew("services stop nginx", sudo: true) + brew("services stop dnsmasq", sudo: true) + } + /** Switching to a new PHP version involves: - unlinking the current version @@ -87,18 +94,42 @@ class Actions { Please note that depending on which version is installed, the version that is switched to may or may not be identical to `php` (without @version). */ - public static func switchToPhpVersion(version: String, availableVersions: [String]) - { + public static func switchToPhpVersion( + version: String, + availableVersions: [String], + completed: @escaping () -> Void + ) { + print("Switching to \(version)") + let group = DispatchGroup() + group.enter() + + var versionsDisabled: [String: Bool] = [:] availableVersions.forEach { (available) in - let formula = (available == App.shared.brewPhpVersion) ? "php" : "php@\(available)" - brew("unlink \(formula)") - brew("services stop \(formula)", sudo: true) + versionsDisabled[available] = false } - let formula = (version == App.shared.brewPhpVersion) ? "php" : "php@\(version)" + availableVersions.forEach { (available) in + DispatchQueue.global(qos: .userInitiated).async { + let formula = (available == App.shared.brewPhpVersion) + ? "php" : "php@\(available)" + + brew("unlink \(formula)") + brew("services stop \(formula)", sudo: true) + + versionsDisabled[available] = true + if !versionsDisabled.values.contains(false) { + group.leave() + } + } + } - brew("link \(formula) --overwrite --force") - brew("services start \(formula)", sudo: true) + group.notify(queue: .global(qos: .userInitiated)) { + let formula = (version == App.shared.brewPhpVersion) ? "php" : "php@\(version)" + brew("link \(formula) --overwrite --force") + brew("services start \(formula)", sudo: true) + + completed() + } } // MARK: - Finding Config Files diff --git a/phpmon/Localizable.strings b/phpmon/Localizable.strings index c26d952..70e6987 100644 --- a/phpmon/Localizable.strings +++ b/phpmon/Localizable.strings @@ -24,6 +24,7 @@ "mi_restart_dnsmasq" = "Restart service: dnsmasq"; "mi_restart_specific" = "Restart specific service"; "mi_restart_all_services" = "Restart all services"; +"mi_stop_all_services" = "Stop all services"; "mi_force_load_latest" = "Force load latest PHP version"; "mi_php_refresh" = "Refresh information";