1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-07 20:10:08 +02:00

👌 Avoid self. (and capture using [self] in)

This commit is contained in:
2021-03-19 16:01:03 +01:00
parent 7733c90206
commit 3cbc2a0367

View File

@ -25,12 +25,12 @@ class MainMenu: NSObject, NSWindowDelegate {
*/ */
func startup() { func startup() {
// Start with the icon // Start with the icon
self.setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!) setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!)
// Perform environment boot checks // Perform environment boot checks
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
Startup().checkEnvironment(success: { self.onEnvironmentPass() }, Startup().checkEnvironment(success: { onEnvironmentPass() },
failure: { self.onEnvironmentFail() } failure: { onEnvironmentFail() }
) )
} }
} }
@ -40,14 +40,14 @@ class MainMenu: NSObject, NSWindowDelegate {
*/ */
private func onEnvironmentPass() { private func onEnvironmentPass() {
App.shared.availablePhpVersions = Actions.detectPhpVersions() App.shared.availablePhpVersions = Actions.detectPhpVersions()
self.updatePhpVersionInStatusBar() updatePhpVersionInStatusBar()
// Schedule a request to fetch the PHP version every 60 seconds // Schedule a request to fetch the PHP version every 60 seconds
DispatchQueue.main.async { DispatchQueue.main.async { [self] in
App.shared.timer = Timer.scheduledTimer( App.shared.timer = Timer.scheduledTimer(
timeInterval: 60, timeInterval: 60,
target: self, target: self,
selector: #selector(self.updatePhpVersionInStatusBar), selector: #selector(updatePhpVersionInStatusBar),
userInfo: nil, userInfo: nil,
repeats: true repeats: true
) )
@ -58,7 +58,7 @@ class MainMenu: NSObject, NSWindowDelegate {
When the environment is not OK, present an alert to inform the user. When the environment is not OK, present an alert to inform the user.
*/ */
private func onEnvironmentFail() { private func onEnvironmentFail() {
DispatchQueue.main.async { DispatchQueue.main.async { [self] in
let close = Alert.present( let close = Alert.present(
messageText: "alert.cannot_start.title".localized, messageText: "alert.cannot_start.title".localized,
informativeText: "alert.cannot_start.info".localized, informativeText: "alert.cannot_start.info".localized,
@ -70,7 +70,7 @@ class MainMenu: NSObject, NSWindowDelegate {
exit(1) exit(1)
} }
self.startup() startup()
} }
} }
@ -79,7 +79,7 @@ class MainMenu: NSObject, NSWindowDelegate {
*/ */
func update() { func update() {
// Update the menu item on the main thread // Update the menu item on the main thread
DispatchQueue.main.async { DispatchQueue.main.async { [self] in
// Create a new menu // Create a new menu
let menu = StatusMenu() let menu = StatusMenu()
@ -96,15 +96,15 @@ class MainMenu: NSObject, NSWindowDelegate {
menu.addItem(NSMenuItem.separator()) menu.addItem(NSMenuItem.separator())
// Add about & quit menu items // Add about & quit menu items
menu.addItem(NSMenuItem(title: "mi_about".localized, action: #selector(self.openAbout), keyEquivalent: "")) menu.addItem(NSMenuItem(title: "mi_about".localized, action: #selector(openAbout), keyEquivalent: ""))
menu.addItem(NSMenuItem(title: "mi_quit".localized, action: #selector(self.terminateApp), keyEquivalent: "q")) menu.addItem(NSMenuItem(title: "mi_quit".localized, action: #selector(terminateApp), keyEquivalent: "q"))
// Make sure every item can be interacted with // Make sure every item can be interacted with
menu.items.forEach({ (item) in menu.items.forEach({ (item) in
item.target = self item.target = self
}) })
self.statusItem.menu = menu statusItem.menu = menu
} }
} }
@ -112,7 +112,7 @@ class MainMenu: NSObject, NSWindowDelegate {
Sets the status bar image based on a version string. Sets the status bar image based on a version string.
*/ */
func setStatusBarImage(version: String) { func setStatusBarImage(version: String) {
self.setStatusBar( setStatusBar(
image: MenuBarImageGenerator.textToImage(text: version) image: MenuBarImageGenerator.textToImage(text: version)
) )
} }
@ -141,15 +141,15 @@ class MainMenu: NSObject, NSWindowDelegate {
private func waitAndExecute(_ execute: @escaping () -> Void, completion: @escaping () -> Void = {}) private func waitAndExecute(_ execute: @escaping () -> Void, completion: @escaping () -> Void = {})
{ {
App.shared.busy = true App.shared.busy = true
self.setBusyImage() setBusyImage()
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
self.update() update()
execute() execute()
App.shared.busy = false App.shared.busy = false
DispatchQueue.main.async { DispatchQueue.main.async { [self] in
self.updatePhpVersionInStatusBar() updatePhpVersionInStatusBar()
self.update() update()
completion() completion()
} }
} }
@ -160,33 +160,33 @@ class MainMenu: NSObject, NSWindowDelegate {
@objc func updatePhpVersionInStatusBar() { @objc func updatePhpVersionInStatusBar() {
App.shared.currentInstall = PhpInstallation() App.shared.currentInstall = PhpInstallation()
DispatchQueue.main.async { DispatchQueue.main.async { [self] in
if (App.shared.busy) { if (App.shared.busy) {
self.setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!) setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!)
} else { } else {
self.setStatusBarImage(version: App.phpInstall!.version.short) setStatusBarImage(version: App.phpInstall!.version.short)
} }
} }
self.update() update()
} }
@objc func setBusyImage() { @objc func setBusyImage() {
DispatchQueue.main.async { DispatchQueue.main.async { [self] in
self.setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!) setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!)
} }
} }
// MARK: - Actions // MARK: - Actions
@objc func restartPhpFpm() { @objc func restartPhpFpm() {
self.waitAndExecute({ waitAndExecute({
Actions.restartPhpFpm() Actions.restartPhpFpm()
}) })
} }
@objc func restartAllServices() { @objc func restartAllServices() {
self.waitAndExecute { waitAndExecute {
Actions.restartDnsMasq() Actions.restartDnsMasq()
Actions.restartPhpFpm() Actions.restartPhpFpm()
Actions.restartNginx() Actions.restartNginx()
@ -194,25 +194,25 @@ class MainMenu: NSObject, NSWindowDelegate {
} }
@objc func restartNginx() { @objc func restartNginx() {
self.waitAndExecute { waitAndExecute {
Actions.restartNginx() Actions.restartNginx()
} }
} }
@objc func restartDnsMasq() { @objc func restartDnsMasq() {
self.waitAndExecute { waitAndExecute {
Actions.restartDnsMasq() Actions.restartDnsMasq()
} }
} }
@objc func toggleExtension(sender: ExtensionMenuItem) { @objc func toggleExtension(sender: ExtensionMenuItem) {
self.waitAndExecute { waitAndExecute {
sender.phpExtension?.toggle() sender.phpExtension?.toggle()
} }
} }
@objc func openPhpInfo() { @objc func openPhpInfo() {
self.waitAndExecute { waitAndExecute {
try! "<?php phpinfo();".write(toFile: "/tmp/phpmon_phpinfo.php", atomically: true, encoding: .utf8) try! "<?php phpinfo();".write(toFile: "/tmp/phpmon_phpinfo.php", atomically: true, encoding: .utf8)
Shell.run("\(Paths.binPath)/php-cgi -q /tmp/phpmon_phpinfo.php > /tmp/phpmon_phpinfo.html") Shell.run("\(Paths.binPath)/php-cgi -q /tmp/phpmon_phpinfo.php > /tmp/phpmon_phpinfo.html")
} completion: { } completion: {
@ -225,7 +225,7 @@ class MainMenu: NSObject, NSWindowDelegate {
Alert.notify(message: "alert.force_reload.title".localized, info: "alert.force_reload.info".localized) Alert.notify(message: "alert.force_reload.title".localized, info: "alert.force_reload.info".localized)
// Start switching // Start switching
self.waitAndExecute { waitAndExecute {
Actions.fixMyPhp() Actions.fixMyPhp()
} completion: { } completion: {
Alert.notify(message: "alert.force_reload_done.title".localized, info: "alert.force_reload_done.info".localized) Alert.notify(message: "alert.force_reload_done.title".localized, info: "alert.force_reload_done.info".localized)
@ -248,17 +248,17 @@ class MainMenu: NSObject, NSWindowDelegate {
} }
@objc func switchToPhpVersion(sender: PhpMenuItem) { @objc func switchToPhpVersion(sender: PhpMenuItem) {
print("Switching to: PHP \(sender.version)") // print("Switching to: PHP \(sender.version)")
self.setBusyImage() setBusyImage()
App.shared.busy = true App.shared.busy = true
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
// Update the PHP version in the status bar // Update the PHP version in the status bar
self.updatePhpVersionInStatusBar() updatePhpVersionInStatusBar()
// Update the menu // Update the menu
self.update() update()
// Switch the PHP version // Switch the PHP version
Actions.switchToPhpVersion( Actions.switchToPhpVersion(
@ -270,9 +270,9 @@ class MainMenu: NSObject, NSWindowDelegate {
App.shared.busy = false App.shared.busy = false
// Perform UI updates on main thread // Perform UI updates on main thread
DispatchQueue.main.async { DispatchQueue.main.async { [self] in
self.updatePhpVersionInStatusBar() updatePhpVersionInStatusBar()
self.update() update()
// Send a notification that the switch has been completed // Send a notification that the switch has been completed
LocalNotification.send( LocalNotification.send(