mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-11-09 05:40:07 +01:00
♻️ Cleanup, updated target for tests
This commit is contained in:
44
phpmon/Domain/Core/AppDelegate+Notifications.swift
Normal file
44
phpmon/Domain/Core/AppDelegate+Notifications.swift
Normal file
@@ -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])
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -108,7 +108,7 @@ class StatusMenu : NSMenu {
|
||||
return
|
||||
}
|
||||
|
||||
let stats = App.phpInstall!.configuration
|
||||
let stats = App.phpInstall!.limits
|
||||
|
||||
// Stats
|
||||
self.addItem(NSMenuItem.separator())
|
||||
|
||||
@@ -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 = "???"
|
||||
|
||||
@@ -44,6 +44,6 @@ class SiteListCell: NSTableCellView
|
||||
: NSColor.red
|
||||
|
||||
// Show the current driver
|
||||
labelDriver.stringValue = site.driver
|
||||
labelDriver.stringValue = site.driver ?? "???"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user