From 510257c4363bfa4ea63baae85fffe2cd0a1885ac Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Fri, 28 Jan 2022 22:05:53 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8C=20Complete=20work=20on=20inter=20a?= =?UTF-8?q?pp=20handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allowed commands: phpmon://list phpmon://services/stop phpmon://services/restart/all phpmon://services/restart/nginx phpmon://services/restart/php phpmon://services/restart/dnsmasq phpmon://locate/config phpmon://locate/composer phpmon://locate/valet phpmon://phpinfo phpmon://switch/php/{version} --- PHP Monitor.xcodeproj/project.pbxproj | 6 ++ phpmon/Domain/Core/AppDelegate+InterApp.swift | 46 ++++--------- phpmon/Domain/Core/InterAppHandler.swift | 64 +++++++++++++++++++ 3 files changed, 82 insertions(+), 34 deletions(-) create mode 100644 phpmon/Domain/Core/InterAppHandler.swift diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index bbdb5ee..1a3a75f 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -161,6 +161,8 @@ C4EE55AC27708B9E001DF387 /* Preview.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EE55A727708B9E001DF387 /* Preview.swift */; }; C4EE55AD27708B9E001DF387 /* PMStatsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EE55A827708B9E001DF387 /* PMStatsView.swift */; }; C4EE55AE27708B9E001DF387 /* PMStatsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EE55A827708B9E001DF387 /* PMStatsView.swift */; }; + C4EED88927A48778006D7272 /* InterAppHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EED88827A48778006D7272 /* InterAppHandler.swift */; }; + C4EED88A27A48778006D7272 /* InterAppHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EED88827A48778006D7272 /* InterAppHandler.swift */; }; C4F2E4372752F0870020E974 /* HomebrewDiagnostics.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4F2E4362752F0870020E974 /* HomebrewDiagnostics.swift */; }; C4F2E4382752F08D0020E974 /* HomebrewDiagnostics.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4F2E4362752F0870020E974 /* HomebrewDiagnostics.swift */; }; C4F2E43A2752F7D00020E974 /* PhpInstallation.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4F2E4392752F7D00020E974 /* PhpInstallation.swift */; }; @@ -311,6 +313,7 @@ C4EE55A627708B9E001DF387 /* PMHeaderView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PMHeaderView.swift; sourceTree = ""; }; C4EE55A727708B9E001DF387 /* Preview.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Preview.swift; sourceTree = ""; }; C4EE55A827708B9E001DF387 /* PMStatsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PMStatsView.swift; sourceTree = ""; }; + C4EED88827A48778006D7272 /* InterAppHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InterAppHandler.swift; sourceTree = ""; }; C4F2E4362752F0870020E974 /* HomebrewDiagnostics.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomebrewDiagnostics.swift; sourceTree = ""; }; C4F2E4392752F7D00020E974 /* PhpInstallation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhpInstallation.swift; sourceTree = ""; }; C4F30B02278E16BA00755FCE /* HomebrewService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomebrewService.swift; sourceTree = ""; }; @@ -600,6 +603,7 @@ C4B97B77275CF1B5003F3378 /* App+ActivationPolicy.swift */, C4B97B7A275CF20A003F3378 /* App+GlobalHotkey.swift */, C4D8016522B1584700C6DA1B /* Startup.swift */, + C4EED88827A48778006D7272 /* InterAppHandler.swift */, ); path = Core; sourceTree = ""; @@ -929,6 +933,7 @@ C41C1B4B22B019FF00E7CF16 /* ActivePhpInstallation.swift in Sources */, C4188989275FE8CB001EF227 /* Filesystem.swift in Sources */, C4B97B7B275CF20A003F3378 /* App+GlobalHotkey.swift in Sources */, + C4EED88927A48778006D7272 /* InterAppHandler.swift in Sources */, C40C7F1E2772136000DDDCDC /* PhpEnv.swift in Sources */, C476FF9822B0DD830098105B /* Alert.swift in Sources */, C474B00624C0E98C00066A22 /* LocalNotification.swift in Sources */, @@ -980,6 +985,7 @@ C4F2E4382752F08D0020E974 /* HomebrewDiagnostics.swift in Sources */, C4F780AE25D80B37000DBC97 /* ExtensionParserTest.swift in Sources */, C4C8E819276F54D8003AC782 /* App+ConfigWatch.swift in Sources */, + C4EED88A27A48778006D7272 /* InterAppHandler.swift in Sources */, C48D6C75279CD3E400F26D7E /* PhpVersionNumberTest.swift in Sources */, C43603A1275E67610028EFC6 /* AppDelegate+Notifications.swift in Sources */, C42759682627662800093CAE /* NSMenuExtension.swift in Sources */, diff --git a/phpmon/Domain/Core/AppDelegate+InterApp.swift b/phpmon/Domain/Core/AppDelegate+InterApp.swift index d2de011..fa7dc4a 100644 --- a/phpmon/Domain/Core/AppDelegate+InterApp.swift +++ b/phpmon/Domain/Core/AppDelegate+InterApp.swift @@ -21,44 +21,22 @@ extension AppDelegate { Please note that PHP Monitor needs to be running in the background for this to work. */ func application(_ application: NSApplication, open urls: [URL]) { - // Only ever interpret the first URL - if let url = urls.first { - self.interpretCommand( - url.absoluteString.replacingOccurrences(of: "phpmon://", with: "") - ) - } + + guard let url = urls.first else { return } + + self.interpretCommand( + url.absoluteString.replacingOccurrences(of: "phpmon://", with: ""), + commands: InterApp.getCommands() + ) } - private func interpretCommand(_ command: String) { - switch (command) { - case "list": - SiteListVC.show() - break - default: - break - } - - if command.starts(with: "callback/switch") { - // TODO: Send XML for suggestions? - // I looked at the output of Dash (which is my example of an amazing app here) - // and it works like this: open -g "dash-workflow-callback://{query}" - // which returns XML. I'm not sure how to do that here, but if I could - // dynamically return a list of valid PHP versions, it'd be easy mode. - } - - if command.starts(with: "switch/php/") { - // See if the PHP version we're attempting to switch to is valid - let version = String(command.split(separator: "/").last!) - if PhpEnv.shared.availablePhpVersions.contains(version) { - MainMenu.shared.switchToPhpVersion(version) - } else { - Alert.notify( - message: "Unsupported version", - info: "PHP Monitor can't switch to PHP \(version), as it may not be installed or available." - ) + private func interpretCommand(_ command: String, commands: [InterApp.Action]) { + commands.forEach { action in + if command.starts(with: action.command) { + let lastElement = String(command.split(separator: "/").last!) + action.action(lastElement) } } } - } diff --git a/phpmon/Domain/Core/InterAppHandler.swift b/phpmon/Domain/Core/InterAppHandler.swift new file mode 100644 index 0000000..cc8576e --- /dev/null +++ b/phpmon/Domain/Core/InterAppHandler.swift @@ -0,0 +1,64 @@ +// +// InterAppHandler.swift +// PHP Monitor +// +// Created by Nico Verbruggen on 28/01/2022. +// Copyright © 2022 Nico Verbruggen. All rights reserved. +// + +import Foundation + +class InterApp { + + public static var bindings: [Action] = [] + + public static func register(_ action: Action) { + self.bindings.append(action) + } + + public struct Action { + let command: String + let action: (String) -> Void + } + + static func getCommands() -> [InterApp.Action] { return [ + InterApp.Action(command: "list", action: { _ in + SiteListVC.show() + }), + InterApp.Action(command: "services/stop", action: { _ in + MainMenu.shared.stopAllServices() + }), + InterApp.Action(command: "services/restart/all", action: { _ in + MainMenu.shared.restartAllServices() + }), + InterApp.Action(command: "services/restart/nginx", action: { _ in + MainMenu.shared.restartNginx() + }), + InterApp.Action(command: "services/restart/php", action: { _ in + MainMenu.shared.restartPhpFpm() + }), + InterApp.Action(command: "services/restart/dnsmasq", action: { _ in + MainMenu.shared.restartDnsMasq() + }), + InterApp.Action(command: "locate/config", action: { _ in + MainMenu.shared.openActiveConfigFolder() + }), + InterApp.Action(command: "locate/composer", action: { _ in + MainMenu.shared.openGlobalComposerFolder() + }), + InterApp.Action(command: "locate/valet", action: { _ in + MainMenu.shared.openValetConfigFolder() + }), + InterApp.Action(command: "phpinfo", action: { _ in + MainMenu.shared.openPhpInfo() + }), + InterApp.Action(command: "switch/php/", action: { version in + if PhpEnv.shared.availablePhpVersions.contains(version) { + MainMenu.shared.switchToPhpVersion(version) + } else { + Alert.notify(message: "Unsupported version", info: "PHP Monitor can't switch to PHP \(version), as it may not be installed or available.") + } + }), + ]} + +}