From 92a6d506dca9756391c2b0126bb842e83f159c06 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Mon, 6 Dec 2021 17:19:08 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Cleanup,=20updated=20targe?= =?UTF-8?q?t=20for=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHP Monitor.xcodeproj/project.pbxproj | 10 ++++- .../Core/AppDelegate+Notifications.swift | 44 +++++++++++++++++++ phpmon/Domain/Core/AppDelegate.swift | 19 ++------ phpmon/Domain/Helpers/LocalNotification.swift | 14 ------ phpmon/Domain/Integrations/Valet/Valet.swift | 23 +++++++++- phpmon/Domain/Menu/StatusMenu.swift | 2 +- phpmon/Domain/PHP/ActivePhpInstallation.swift | 20 ++++++--- phpmon/Domain/SiteList/SiteListCell.swift | 2 +- phpmon/Domain/SiteList/SiteListVC.swift | 5 +++ phpmon/Domain/SiteList/SiteListWC.swift | 1 - 10 files changed, 99 insertions(+), 41 deletions(-) create mode 100644 phpmon/Domain/Core/AppDelegate+Notifications.swift diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index 06a2ddd..4244974 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -25,6 +25,8 @@ C4232EE52612526500158FC6 /* Credits.html in Resources */ = {isa = PBXBuildFile; fileRef = C4232EE42612526500158FC6 /* Credits.html */; }; C42759672627662800093CAE /* NSMenuExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C42759662627662800093CAE /* NSMenuExtension.swift */; }; C42759682627662800093CAE /* NSMenuExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C42759662627662800093CAE /* NSMenuExtension.swift */; }; + C43603A0275E67610028EFC6 /* AppDelegate+Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = C436039F275E67610028EFC6 /* AppDelegate+Notifications.swift */; }; + C43603A1275E67610028EFC6 /* AppDelegate+Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = C436039F275E67610028EFC6 /* AppDelegate+Notifications.swift */; }; C43A8A1A25D9CD1000591B77 /* Utility.swift in Sources */ = {isa = PBXBuildFile; fileRef = C43A8A1925D9CD1000591B77 /* Utility.swift */; }; C43A8A2025D9D1D700591B77 /* brew.json in Resources */ = {isa = PBXBuildFile; fileRef = C43A8A1F25D9D1D700591B77 /* brew.json */; }; C43A8A2425D9D20D00591B77 /* BrewJsonParserTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C43A8A2325D9D20D00591B77 /* BrewJsonParserTest.swift */; }; @@ -131,6 +133,7 @@ C42295DC2358D02000E263B2 /* Command.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Command.swift; sourceTree = ""; }; C4232EE42612526500158FC6 /* Credits.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = Credits.html; sourceTree = ""; }; C42759662627662800093CAE /* NSMenuExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSMenuExtension.swift; sourceTree = ""; }; + C436039F275E67610028EFC6 /* AppDelegate+Notifications.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppDelegate+Notifications.swift"; sourceTree = ""; }; C43A8A1925D9CD1000591B77 /* Utility.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utility.swift; sourceTree = ""; }; C43A8A1F25D9D1D700591B77 /* brew.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = brew.json; sourceTree = ""; }; C43A8A2325D9D20D00591B77 /* BrewJsonParserTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrewJsonParserTest.swift; sourceTree = ""; }; @@ -352,6 +355,7 @@ children = ( C41C1B3622B0097F00E7CF16 /* AppDelegate.swift */, C4B97B74275CF08C003F3378 /* AppDelegate+MenuOutlets.swift */, + C436039F275E67610028EFC6 /* AppDelegate+Notifications.swift */, C41C1B3C22B0098000E7CF16 /* Main.storyboard */, C4811D2322D70A4700B5F6B3 /* App.swift */, C4B97B77275CF1B5003F3378 /* App+ActivationPolicy.swift */, @@ -523,6 +527,7 @@ C4F8C0A422D4F12C002EFE61 /* DateExtension.swift in Sources */, C4AF9F7A2754499000D44ED0 /* Valet.swift in Sources */, 5420395926135DC100FB00FA /* PrefsVC.swift in Sources */, + C43603A0275E67610028EFC6 /* AppDelegate+Notifications.swift in Sources */, C41C1B4722B009A400E7CF16 /* Shell.swift in Sources */, C4F2E43A2752F7D00020E974 /* PhpInstallation.swift in Sources */, C41C1B4D22B0215A00E7CF16 /* Actions.swift in Sources */, @@ -575,6 +580,7 @@ C4F2E4382752F08D0020E974 /* HomebrewDiagnostics.swift in Sources */, C4F780AE25D80B37000DBC97 /* ExtensionParserTest.swift in Sources */, C4F780C725D80B75000DBC97 /* StatusMenu.swift in Sources */, + C43603A1275E67610028EFC6 /* AppDelegate+Notifications.swift in Sources */, C42759682627662800093CAE /* NSMenuExtension.swift in Sources */, C4B97B76275CF08C003F3378 /* AppDelegate+MenuOutlets.swift in Sources */, C4F780CD25D80B75000DBC97 /* Alert.swift in Sources */, @@ -808,7 +814,7 @@ "@executable_path/../Frameworks", "@loader_path/../Frameworks", ); - MACOSX_DEPLOYMENT_TARGET = 10.14; + MACOSX_DEPLOYMENT_TARGET = 11.0; PRODUCT_BUNDLE_IDENTIFIER = "com.nicoverbruggen.phpmon-tests"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; @@ -829,7 +835,7 @@ "@executable_path/../Frameworks", "@loader_path/../Frameworks", ); - MACOSX_DEPLOYMENT_TARGET = 10.14; + MACOSX_DEPLOYMENT_TARGET = 11.0; PRODUCT_BUNDLE_IDENTIFIER = "com.nicoverbruggen.phpmon-tests"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; diff --git a/phpmon/Domain/Core/AppDelegate+Notifications.swift b/phpmon/Domain/Core/AppDelegate+Notifications.swift new file mode 100644 index 0000000..f0af76c --- /dev/null +++ b/phpmon/Domain/Core/AppDelegate+Notifications.swift @@ -0,0 +1,44 @@ +// +// AppDelegate+Notifications.swift +// PHP Monitor +// +// Created by Nico Verbruggen on 06/12/2021. +// Copyright © 2021 Nico Verbruggen. All rights reserved. +// + +import Foundation +import UserNotifications + +extension AppDelegate { + + // MARK: - Notifications + + public func setupNotifications() { + let notificationCenter = UNUserNotificationCenter.current() + notificationCenter.delegate = self + notificationCenter.requestAuthorization(options: [.alert], completionHandler: { granted, error in + if granted { + print("PHP Monitor has permission to show notifications.") + } else { + print("PHP Monitor does not have permission to show notifications.") + } + if let error = error { + print("PHP Monitor encounted an error determining notification permissions:") + print(error) + } + }) + } + + /** + Ensure that the application displays notifications even when the app is active. + */ + func userNotificationCenter( + _ center: UNUserNotificationCenter, + willPresent notification: UNNotification, + withCompletionHandler completionHandler: + @escaping (UNNotificationPresentationOptions) -> Void + ) { + completionHandler([.banner]) + } + +} diff --git a/phpmon/Domain/Core/AppDelegate.swift b/phpmon/Domain/Core/AppDelegate.swift index c91b718..a963b6e 100644 --- a/phpmon/Domain/Core/AppDelegate.swift +++ b/phpmon/Domain/Core/AppDelegate.swift @@ -70,21 +70,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele startup procedure. */ func applicationDidFinishLaunching(_ aNotification: Notification) { - LocalNotification.askForPermission() - UNUserNotificationCenter.current().delegate = self - self.menu.startup() - } - - /** - Ensure that the application displays notifications even when the app is active. - */ - func userNotificationCenter( - _ center: UNUserNotificationCenter, - willPresent notification: UNNotification, - withCompletionHandler completionHandler: - @escaping (UNNotificationPresentationOptions) -> Void - ) { - completionHandler([.banner]) + // Make sure notifications will work + setupNotifications() + // Make sure the menu performs its initial checks + menu.startup() } } diff --git a/phpmon/Domain/Helpers/LocalNotification.swift b/phpmon/Domain/Helpers/LocalNotification.swift index e835773..8bf0da2 100644 --- a/phpmon/Domain/Helpers/LocalNotification.swift +++ b/phpmon/Domain/Helpers/LocalNotification.swift @@ -10,20 +10,6 @@ import UserNotifications class LocalNotification { - public static func askForPermission() { - UNUserNotificationCenter.current().requestAuthorization(options: [.alert], completionHandler: { granted, error in - if granted { - print("PHP Monitor has permission to show notifications.") - } else { - print("PHP Monitor does not have permission to show notifications.") - } - if let error = error { - print("PHP Monitor encounted an error determining notification permissions:") - print(error) - } - }) - } - public static func send(title: String, subtitle: String) { let content = UNMutableNotificationContent() content.title = title diff --git a/phpmon/Domain/Integrations/Valet/Valet.swift b/phpmon/Domain/Integrations/Valet/Valet.swift index 5039a97..553c4ca 100644 --- a/phpmon/Domain/Integrations/Valet/Valet.swift +++ b/phpmon/Domain/Integrations/Valet/Valet.swift @@ -12,9 +12,13 @@ class Valet { static let shared = Valet() + /// The version of Valet that was detected. var version: String + + /// The Valet configuration file. var config: Valet.Configuration + /// A cached list of sites that were detected after analyzing the paths set up for Valet. var sites: [Site] = [] init() { @@ -70,13 +74,20 @@ class Valet { // MARK: - Structs class Site { + /// Name of the site. Does not include the TLD. var name: String! + /// The absolute path to the directory that is served. var absolutePath: String! + + /// Location of the alias. If set, this is a linked domain. var aliasPath: String? + /// Whether the site has been secured. var secured: Bool! - var driver: String = "???" + + /// What driver is currently in use. If not detected, defaults to nil. + var driver: String? = nil init() {} @@ -110,15 +121,23 @@ class Valet { .replacingOccurrences(of: "This site is served by [", with: "") .replacingOccurrences(of: "ValetDriver].\n", with: "") } else { - self.driver = "???" + self.driver = nil } } } struct Configuration: Decodable { + /// Top level domain suffix. Usually "test" but can be set to something else. + /// - Important: Does not include the actual dot. ("test", not ".test"!) let tld: String + + /// The paths that need to be checked. let paths: [String] + + /// The loopback address. let loopback: String + + /// The default site that is served if the domain is not found. Optional. let defaultSite: String? private enum CodingKeys: String, CodingKey { diff --git a/phpmon/Domain/Menu/StatusMenu.swift b/phpmon/Domain/Menu/StatusMenu.swift index f1f3def..84b2f9e 100644 --- a/phpmon/Domain/Menu/StatusMenu.swift +++ b/phpmon/Domain/Menu/StatusMenu.swift @@ -108,7 +108,7 @@ class StatusMenu : NSMenu { return } - let stats = App.phpInstall!.configuration + let stats = App.phpInstall!.limits // Stats self.addItem(NSMenuItem.separator()) diff --git a/phpmon/Domain/PHP/ActivePhpInstallation.swift b/phpmon/Domain/PHP/ActivePhpInstallation.swift index 08aa4b5..2ab3579 100644 --- a/phpmon/Domain/PHP/ActivePhpInstallation.swift +++ b/phpmon/Domain/PHP/ActivePhpInstallation.swift @@ -19,7 +19,7 @@ import Foundation class ActivePhpInstallation { var version: Version! - var configuration: Configuration! + var limits: Limits! var extensions: [PhpExtension]! // MARK: - Computed @@ -36,7 +36,7 @@ class ActivePhpInstallation { // If an error occurred, exit early if (version.error) { - configuration = Configuration() + limits = Limits() extensions = [] return } @@ -46,7 +46,7 @@ class ActivePhpInstallation { extensions = PhpExtension.load(from: path) // Get configuration values - configuration = Configuration( + limits = Limits( memory_limit: getByteCount(key: "memory_limit"), upload_max_filesize: getByteCount(key: "upload_max_filesize"), post_max_size: getByteCount(key: "post_max_size") @@ -161,14 +161,24 @@ class ActivePhpInstallation { } // MARK: - Structs - + + /** + Struct containing information about the version number of the current PHP installation. + Also includes information about whether the install is considered "broken" or not. + If an error was found in the terminal output, `error` is set to `true` and the installation + can be considered broken. (The app will display this as well.) + */ struct Version { var short = "???" var long = "???" var error = false } - struct Configuration { + /** + Struct containing information about the limits of the current PHP installation. + Includes: memory limit, max upload size and max post size. + */ + struct Limits { var memory_limit = "???" var upload_max_filesize = "???" var post_max_size = "???" diff --git a/phpmon/Domain/SiteList/SiteListCell.swift b/phpmon/Domain/SiteList/SiteListCell.swift index 32f3464..91efeb4 100644 --- a/phpmon/Domain/SiteList/SiteListCell.swift +++ b/phpmon/Domain/SiteList/SiteListCell.swift @@ -44,6 +44,6 @@ class SiteListCell: NSTableCellView : NSColor.red // Show the current driver - labelDriver.stringValue = site.driver + labelDriver.stringValue = site.driver ?? "???" } } diff --git a/phpmon/Domain/SiteList/SiteListVC.swift b/phpmon/Domain/SiteList/SiteListVC.swift index 2405add..83af04d 100644 --- a/phpmon/Domain/SiteList/SiteListVC.swift +++ b/phpmon/Domain/SiteList/SiteListVC.swift @@ -19,8 +19,13 @@ class SiteListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource { // MARK: - Variables + /// List of sites that will be displayed in this view. Originates from the `Valet` object. var sites: [Valet.Site] = [] + + /// Array that contains various editors that might open a particular site directory. var editorAvailability: [String] = [] + + /// String that was last searched for. Empty by default. var lastSearchedFor = "" // MARK: - Helper Variables diff --git a/phpmon/Domain/SiteList/SiteListWC.swift b/phpmon/Domain/SiteList/SiteListWC.swift index 509e862..d652dad 100644 --- a/phpmon/Domain/SiteList/SiteListWC.swift +++ b/phpmon/Domain/SiteList/SiteListWC.swift @@ -36,7 +36,6 @@ class SiteListWC: PMWindowController, NSSearchFieldDelegate, NSToolbarDelegate { func controlTextDidChange(_ notification: Notification) { guard let searchField = notification.object as? NSSearchField else { - print("Unexpected control in update notification") return }