diff --git a/phpmon/Classes/Commands/Startup.swift b/phpmon/Classes/Commands/Startup.swift index e4f20a5..fa8e3d5 100644 --- a/phpmon/Classes/Commands/Startup.swift +++ b/phpmon/Classes/Commands/Startup.swift @@ -10,62 +10,102 @@ import Foundation class Startup { - public static func checkEnvironment() + public var failed : Bool = false + public var failureCallback = {} + + /** + Checks the user's environment and checks if PHP Monitor can be used properly. + This checks if PHP is installed, Valet is running, the appropriate permissions are set, and more. + + - Parameter success: Callback that is fired if the application can proceed with launch + - Parameter failure: Callback that is fired if the application must retry launch + */ + public func checkEnvironment(success: () -> Void, failure: @escaping () -> Void) { - self.presentAlertOnMainThreadIf( + self.failureCallback = failure + + self.performEnvironmentCheck( !Shell.user.pipe("which php").contains("/usr/local/bin/php"), messageText: "PHP is not correctly installed", - informativeText: "You must install PHP via brew. Try running `which php` in Terminal, it should return `/usr/local/bin/php`. The app will not work correctly until you resolve this issue. (Usually `brew link php` resolves this issue.)" + informativeText: "You must install PHP via brew. Try running `which php` in Terminal, it should return `/usr/local/bin/php`. The app will not work correctly until you resolve this issue. (Usually `brew link php` resolves this issue.)", + breaking: true ) - self.presentAlertOnMainThreadIf( + self.performEnvironmentCheck( !Shell.user.pipe("ls /usr/local/opt | grep php@7.4").contains("php@7.4"), messageText: "PHP 7.4 is not correctly installed", - informativeText: "PHP 7.4 alias was not found in `/usr/local/opt`. The app will not work correctly until you resolve this issue. If you already have the `php` formula installed, you may need to run `brew install php@7.4` in order for PHP Monitor to detect this installation." + informativeText: "PHP 7.4 alias was not found in `/usr/local/opt`. The app will not work correctly until you resolve this issue. If you already have the `php` formula installed, you may need to run `brew install php@7.4` in order for PHP Monitor to detect this installation.", + breaking: true ) - self.presentAlertOnMainThreadIf( + self.performEnvironmentCheck( !Shell.user.pipe("which valet").contains("/usr/local/bin/valet"), messageText: "Laravel Valet is not correctly installed", - informativeText: "You must install Valet with composer. Try running `which valet` in Terminal, it should return `/usr/local/bin/valet`. The app will not work correctly until you resolve this issue." + informativeText: "You must install Valet with composer. Try running `which valet` in Terminal, it should return `/usr/local/bin/valet`. The app will not work correctly until you resolve this issue.", + breaking: true ) - self.presentAlertOnMainThreadIf( + self.performEnvironmentCheck( !Shell.user.pipe("cat /private/etc/sudoers.d/brew").contains("/usr/local/bin/brew"), messageText: "Brew has not been added to sudoers.d", - informativeText: "You must run `sudo valet trust` to ensure Valet can start and stop services without having to use sudo every time. The app will not work correctly until you resolve this issue." + informativeText: "You must run `sudo valet trust` to ensure Valet can start and stop services without having to use sudo every time. The app will not work correctly until you resolve this issue.", + breaking: true ) - self.presentAlertOnMainThreadIf( + self.performEnvironmentCheck( !Shell.user.pipe("cat /private/etc/sudoers.d/valet").contains("/usr/local/bin/valet"), messageText: "Valet has not been added to sudoers.d", - informativeText: "You must run `sudo valet trust` to ensure Valet can start and stop services without having to use sudo every time. The app will not work correctly until you resolve this issue." + informativeText: "You must run `sudo valet trust` to ensure Valet can start and stop services without having to use sudo every time. The app will not work correctly until you resolve this issue.", + breaking: true ) let services = Shell.user.pipe("brew services list | grep php") - self.presentAlertOnMainThreadIf( + self.performEnvironmentCheck( (services.countInstances(of: "started") > 1), messageText: "Multiple PHP services are active", informativeText: "This can cause php-fpm to serve a more recent version of PHP than the one you'd like to see active. Please terminate all extra PHP processes." + "\n\nThe easiest solution is to choose the option 'Force load latest PHP version' in the menu bar." + "\n\nAlternatively, you can fix this manually. You can do this by running `brew services list` and running `sudo brew services stop php@7.3` (and use the version that applies)." + "\n\nPHP Monitor usually handles the starting and stopping of these services, so once the correct version is the only PHP version running you should not have any issues. It is recommended to restart PHP Monitor once you have resolved this issue." + - "\n\nFor more information about this issue, please see the README.md file in the repository on GitHub." + "\n\nFor more information about this issue, please see the README.md file in the repository on GitHub.", + breaking: false ) + + if (!self.failed) { + success() + } } - private static func presentAlertOnMainThreadIf( + /** + * Perform an environment check. Will cause the application to terminate, if `breaking` is set to true. + * + * - Parameter condition: Condition to check for + * - Parameter messageText: Short description of what is wrong + * - Parameter informativeText: Expanded description of the environment check that failed + * - Parameter breaking: If the application should terminate afterwards + */ + private func performEnvironmentCheck( _ condition: Bool, messageText: String, - informativeText: String + informativeText: String, + breaking: Bool ) { if (condition) { + // Only breaking issues will cause the notification + if (breaking) { + self.failed = true + } DispatchQueue.main.async { - Alert.present( + // Present the information to the user + _ = Alert.present( messageText: messageText, informativeText: informativeText ) + // Only breaking issues will throw the extra retry modal + if (breaking) { + self.failureCallback() + } } } } diff --git a/phpmon/Classes/Helpers/Alert.swift b/phpmon/Classes/Helpers/Alert.swift index 149221f..1162223 100644 --- a/phpmon/Classes/Helpers/Alert.swift +++ b/phpmon/Classes/Helpers/Alert.swift @@ -12,12 +12,16 @@ class Alert { public static func present( messageText: String, informativeText: String, - buttonTitle: String = "OK" - ) { + buttonTitle: String = "OK", + secondButtonTitle: String = "" + ) -> Bool { let alert = NSAlert.init() alert.messageText = messageText alert.informativeText = informativeText alert.addButton(withTitle: buttonTitle) - alert.runModal() + if (!secondButtonTitle.isEmpty) { + alert.addButton(withTitle: secondButtonTitle) + } + return alert.runModal() == .alertFirstButtonReturn } } diff --git a/phpmon/Singletons/MainMenu.swift b/phpmon/Singletons/MainMenu.swift index 16d9cbb..f24db59 100644 --- a/phpmon/Singletons/MainMenu.swift +++ b/phpmon/Singletons/MainMenu.swift @@ -21,18 +21,41 @@ class MainMenu: NSObject, NSWindowDelegate { self.setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!) // Perform environment boot checks DispatchQueue.global(qos: .userInitiated).async { [unowned self] in - Startup.checkEnvironment() - App.shared.availablePhpVersions = Actions.detectPhpVersions() - self.updatePhpVersionInStatusBar() - // Schedule a request to fetch the PHP version every 60 seconds - DispatchQueue.main.async { - App.shared.timer = Timer.scheduledTimer( - timeInterval: 60, - target: self, - selector: #selector(self.updatePhpVersionInStatusBar), - userInfo: nil, - repeats: true - ) + Startup().checkEnvironment(success: { + self.onEnvironmentPass() + }, failure: { + self.onEnvironmentFail() + }) + } + } + + private func onEnvironmentPass() { + App.shared.availablePhpVersions = Actions.detectPhpVersions() + self.updatePhpVersionInStatusBar() + // Schedule a request to fetch the PHP version every 60 seconds + DispatchQueue.main.async { + App.shared.timer = Timer.scheduledTimer( + timeInterval: 60, + target: self, + selector: #selector(self.updatePhpVersionInStatusBar), + userInfo: nil, + repeats: true + ) + } + } + + private func onEnvironmentFail() { + DispatchQueue.main.async { + let close = Alert.present( + messageText: "PHP Monitor cannot start", + informativeText: "The issue you were just notified about is keeping PHP Monitor from functioning correctly. Please fix the issue and restart PHP Monitor. After clicking on OK, PHP Monitor will close.\n\nIf you have fixed the issue (or don't remember what the exact issue is) you can click on Retry, which will have PHP Monitor to retry the startup checks.", + buttonTitle: "Close", + secondButtonTitle: "Retry" + ) + if (!close) { + self.startup() + } else { + exit(1) } } } @@ -140,12 +163,12 @@ class MainMenu: NSObject, NSWindowDelegate { } @objc public func forceRestartLatestPhp() { - Alert.present( + _ = Alert.present( messageText: "alert.force_reload.title".localized, informativeText: "alert.force_reload.info".localized ) self.waitAndExecute({ Actions.fixMyPhp() }, { - Alert.present( + _ = Alert.present( messageText: "alert.force_reload_done.title".localized, informativeText: "alert.force_reload_done.info".localized )