mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2026-04-01 09:10:08 +02:00
🔥 Additional cleanup
This commit is contained in:
@@ -14,16 +14,14 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
|
||||
// MARK: - Variables
|
||||
|
||||
let sharedShell : Shell
|
||||
let state : App
|
||||
let statusItem = NSStatusBar.system.statusItem(withLength: 32)
|
||||
var timer: Timer?
|
||||
var version: PhpVersion? = nil
|
||||
var availablePhpVersions : [String] = []
|
||||
var busy: Bool = false
|
||||
var log: String = ""
|
||||
var windowController: NSWindowController? = nil
|
||||
|
||||
// MARK: - Initializer
|
||||
|
||||
override init() {
|
||||
self.sharedShell = Shell.shared
|
||||
self.state = App.shared
|
||||
super.init()
|
||||
}
|
||||
|
||||
@@ -34,12 +32,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
|
||||
self.setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!)
|
||||
// Perform environment boot checks
|
||||
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
|
||||
BootChecks.perform()
|
||||
self.availablePhpVersions = Services.detectPhpVersions()
|
||||
Startup.checkEnvironment()
|
||||
App.shared.availablePhpVersions = Actions.detectPhpVersions()
|
||||
self.updatePhpVersionInStatusBar()
|
||||
// Schedule a request to fetch the PHP version every 60 seconds
|
||||
DispatchQueue.main.async {
|
||||
self.timer = Timer.scheduledTimer(
|
||||
App.shared.timer = Timer.scheduledTimer(
|
||||
timeInterval: 60,
|
||||
target: self,
|
||||
selector: #selector(self.updatePhpVersionInStatusBar),
|
||||
@@ -57,7 +55,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
|
||||
// MARK: - UI related
|
||||
|
||||
func setStatusBarImage(version: String) {
|
||||
self.setStatusBar(image: ImageGenerator.generateImageForStatusBar(width: 32.0, text: version))
|
||||
self.setStatusBar(image: MenuBarImageGenerator.textToImage(width: 32.0, text: version))
|
||||
}
|
||||
|
||||
func setStatusBar(image: NSImage) {
|
||||
@@ -71,29 +69,29 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
|
||||
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
|
||||
let menu = NSMenu()
|
||||
var string = "We are not sure what version of PHP you are running."
|
||||
if (self.version != nil) {
|
||||
string = "You are running PHP \(self.version!.long)"
|
||||
if (App.shared.currentVersion != nil) {
|
||||
string = "You are running PHP \(App.shared.currentVersion!.long)"
|
||||
}
|
||||
menu.addItem(NSMenuItem(title: string, action: nil, keyEquivalent: ""))
|
||||
if (self.version != nil) {
|
||||
if (App.shared.currentVersion != nil) {
|
||||
// Actions
|
||||
menu.addItem(NSMenuItem.separator())
|
||||
menu.addItem(NSMenuItem(title: "PHP configuration file (php.ini)", action: #selector(self.openActiveConfigFolder), keyEquivalent: ""))
|
||||
}
|
||||
menu.addItem(NSMenuItem.separator())
|
||||
if (self.availablePhpVersions.count > 0 && !self.busy) {
|
||||
if (App.shared.availablePhpVersions.count > 0 && !App.shared.busy) {
|
||||
var shortcutKey = 1
|
||||
for index in (0..<self.availablePhpVersions.count).reversed() {
|
||||
let version = self.availablePhpVersions[index]
|
||||
for index in (0..<App.shared.availablePhpVersions.count).reversed() {
|
||||
let version = App.shared.availablePhpVersions[index]
|
||||
let action = #selector(self.switchToPhpVersion(sender:))
|
||||
let menuItem = NSMenuItem(title: "Switch to PHP \(version)", action: (version == self.version?.short) ? nil : action, keyEquivalent: "\(shortcutKey)")
|
||||
let menuItem = NSMenuItem(title: "Switch to PHP \(version)", action: (version == App.shared.currentVersion?.short) ? nil : action, keyEquivalent: "\(shortcutKey)")
|
||||
menuItem.tag = index
|
||||
shortcutKey = shortcutKey + 1
|
||||
menu.addItem(menuItem)
|
||||
}
|
||||
menu.addItem(NSMenuItem.separator())
|
||||
}
|
||||
if (self.busy) {
|
||||
if (App.shared.busy) {
|
||||
menu.addItem(NSMenuItem(title: "Switching PHP versions...", action: nil, keyEquivalent: ""))
|
||||
menu.addItem(NSMenuItem.separator())
|
||||
}
|
||||
@@ -109,27 +107,27 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
|
||||
// MARK: - Callable via Obj-C (#selector)
|
||||
|
||||
@objc func openOutput() {
|
||||
if (self.windowController == nil) {
|
||||
if (App.shared.windowController == nil) {
|
||||
let vc = NSStoryboard(name: "Main", bundle: nil).instantiateController(withIdentifier: "logWindow") as! LogViewController
|
||||
Shell.shared.delegate = vc
|
||||
let window = NSWindow(contentViewController: vc)
|
||||
window.title = "Terminal Output"
|
||||
window.delegate = self
|
||||
self.windowController = NSWindowController(window: window)
|
||||
App.shared.windowController = NSWindowController(window: window)
|
||||
}
|
||||
self.windowController!.showWindow(self)
|
||||
App.shared.windowController!.showWindow(self)
|
||||
NSApp.activate(ignoringOtherApps: true)
|
||||
}
|
||||
|
||||
@objc func updatePhpVersionInStatusBar() {
|
||||
self.version = PhpVersion()
|
||||
if (self.busy) {
|
||||
App.shared.currentVersion = PhpVersion()
|
||||
if (App.shared.busy) {
|
||||
DispatchQueue.main.async {
|
||||
self.setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!)
|
||||
}
|
||||
} else {
|
||||
DispatchQueue.main.async {
|
||||
self.setStatusBarImage(version: self.version!.short)
|
||||
self.setStatusBarImage(version: App.shared.currentVersion!.short)
|
||||
}
|
||||
}
|
||||
self.updateMenu()
|
||||
@@ -141,27 +139,23 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
|
||||
}
|
||||
|
||||
@objc public func openActiveConfigFolder() {
|
||||
Services.openPhpConfigFolder(version: self.version!.short)
|
||||
}
|
||||
|
||||
@objc public func restartPhp() {
|
||||
Services.restartPhp(version: self.version!.short)
|
||||
Actions.openPhpConfigFolder(version: App.shared.currentVersion!.short)
|
||||
}
|
||||
|
||||
@objc public func switchToPhpVersion(sender: AnyObject) {
|
||||
self.setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!)
|
||||
let index = sender.tag!
|
||||
let version = self.availablePhpVersions[index]
|
||||
self.busy = true
|
||||
let version = App.shared.availablePhpVersions[index]
|
||||
App.shared.busy = true
|
||||
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
|
||||
// Update the PHP version in the status bar
|
||||
self.updatePhpVersionInStatusBar()
|
||||
// Update the menu
|
||||
self.updateMenu()
|
||||
// Switch the PHP version
|
||||
Services.switchToPhpVersion(version: version, availableVersions: self.availablePhpVersions)
|
||||
Actions.switchToPhpVersion(version: version, availableVersions: App.shared.availablePhpVersions)
|
||||
// Mark as no longer busy
|
||||
self.busy = false
|
||||
App.shared.busy = false
|
||||
// Perform UI updates on main thread
|
||||
DispatchQueue.main.async {
|
||||
self.updatePhpVersionInStatusBar()
|
||||
@@ -171,7 +165,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
|
||||
}
|
||||
|
||||
func windowWillClose(_ notification: Notification) {
|
||||
self.windowController = nil
|
||||
App.shared.windowController = nil
|
||||
Shell.shared.delegate = nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,16 +9,7 @@
|
||||
import Foundation
|
||||
import AppKit
|
||||
|
||||
class Services {
|
||||
public static func mysqlIsRunning() -> Bool {
|
||||
let running = Shell.shared.pipe("launchctl list | grep homebrew.mxcl.mysql")
|
||||
return (running != "")
|
||||
}
|
||||
|
||||
public static func nginxIsRunning() -> Bool {
|
||||
let running = Shell.shared.pipe("launchctl list | grep homebrew.mxcl.nginx")
|
||||
return (running != "")
|
||||
}
|
||||
class Actions {
|
||||
|
||||
public static func detectPhpVersions() -> [String] {
|
||||
let files = Shell.shared.pipe("ls /usr/local/opt | grep php@")
|
||||
@@ -49,10 +40,6 @@ class Services {
|
||||
}
|
||||
}
|
||||
|
||||
public static func restartPhp(version: String) {
|
||||
Shell.shared.run("brew services restart php@\(version)")
|
||||
}
|
||||
|
||||
public static func openPhpConfigFolder(version: String) {
|
||||
let files = [NSURL(fileURLWithPath: "/usr/local/etc/php/\(version)/php.ini")];
|
||||
NSWorkspace.shared.activateFileViewerSelecting(files as [URL]);
|
||||
@@ -8,9 +8,9 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
class BootChecks {
|
||||
class Startup {
|
||||
|
||||
public static func perform()
|
||||
public static func checkEnvironment()
|
||||
{
|
||||
self.presentAlertOnMainThreadIf(
|
||||
!Shell.shared.pipe("which php").contains("/usr/local/bin/php"),
|
||||
@@ -8,9 +8,12 @@
|
||||
|
||||
import Cocoa
|
||||
|
||||
class ImageGenerator {
|
||||
class MenuBarImageGenerator {
|
||||
|
||||
public static func generateImageForStatusBar(width: CGFloat = 30.0, height: CGFloat = 20.0, text: String) -> NSImage {
|
||||
/**
|
||||
Takes a string and converts it to an image that can be displayed in the menu bar.
|
||||
*/
|
||||
public static func textToImage(width: CGFloat = 30.0, height: CGFloat = 20.0, text: String) -> NSImage {
|
||||
let image = NSImage(size: NSMakeSize(width, height))
|
||||
let font = NSFont.systemFont(ofSize: 14)
|
||||
|
||||
@@ -22,5 +22,5 @@ class Constants {
|
||||
This is usually the latest PHP version.
|
||||
*/
|
||||
static let LatestPhpVersion = "7.3"
|
||||
|
||||
|
||||
}
|
||||
|
||||
40
phpmon/Singletons/App.swift
Normal file
40
phpmon/Singletons/App.swift
Normal file
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// StateManager.swift
|
||||
// phpmon
|
||||
//
|
||||
// Created by Nico Verbruggen on 11/07/2019.
|
||||
// Copyright © 2019 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
|
||||
class App {
|
||||
|
||||
static let shared = App()
|
||||
|
||||
/**
|
||||
Whether the application is busy switching versions.
|
||||
*/
|
||||
var busy: Bool = false
|
||||
|
||||
/**
|
||||
The currently active version of PHP.
|
||||
*/
|
||||
var currentVersion: PhpVersion? = nil
|
||||
|
||||
/**
|
||||
All available versions of PHP.
|
||||
*/
|
||||
var availablePhpVersions : [String] = []
|
||||
|
||||
/**
|
||||
The timer that will periodically fetch the PHP version that is currently active.
|
||||
*/
|
||||
var timer: Timer?
|
||||
|
||||
/**
|
||||
The window controller that will show the log.
|
||||
*/
|
||||
var windowController: NSWindowController? = nil
|
||||
|
||||
}
|
||||
@@ -62,7 +62,6 @@ class Shell {
|
||||
history.append(historyItem)
|
||||
// Keep the last 100 items
|
||||
history = history.suffix(100)
|
||||
print(history.count)
|
||||
delegate?.didCompleteCommand(historyItem: historyItem)
|
||||
|
||||
return output
|
||||
Reference in New Issue
Block a user