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

🚀 Version 2.3

Merge branch 'develop' into main

* develop:
  📝 Updated README, SECURITY docs
  🍱 Updated screenshot to Big Sur
  ♻️ Cleanup
  🚧 WIP: Notify the user about PHP switch completion (#15)
This commit is contained in:
2020-08-16 16:06:55 +02:00
14 changed files with 231 additions and 94 deletions

View File

@ -20,6 +20,7 @@
C46FA23F246C358E00944F05 /* StringExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C46FA23E246C358E00944F05 /* StringExtension.swift */; };
C473319F2470923A009A0597 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = C473319E2470923A009A0597 /* Localizable.strings */; };
C47331A2247093B7009A0597 /* StatusMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = C47331A1247093B7009A0597 /* StatusMenu.swift */; };
C474B00624C0E98C00066A22 /* LocalNotification.swift in Sources */ = {isa = PBXBuildFile; fileRef = C474B00524C0E98C00066A22 /* LocalNotification.swift */; };
C476FF9822B0DD830098105B /* Alert.swift in Sources */ = {isa = PBXBuildFile; fileRef = C476FF9722B0DD830098105B /* Alert.swift */; };
C4811D2422D70A4700B5F6B3 /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4811D2322D70A4700B5F6B3 /* App.swift */; };
C4811D2A22D70F9A00B5F6B3 /* MainMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4811D2922D70F9A00B5F6B3 /* MainMenu.swift */; };
@ -45,6 +46,7 @@
C46FA23E246C358E00944F05 /* StringExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringExtension.swift; sourceTree = "<group>"; };
C473319E2470923A009A0597 /* Localizable.strings */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; path = Localizable.strings; sourceTree = "<group>"; };
C47331A1247093B7009A0597 /* StatusMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusMenu.swift; sourceTree = "<group>"; };
C474B00524C0E98C00066A22 /* LocalNotification.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalNotification.swift; sourceTree = "<group>"; };
C476FF9722B0DD830098105B /* Alert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Alert.swift; sourceTree = "<group>"; };
C4811D2322D70A4700B5F6B3 /* App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = "<group>"; };
C4811D2922D70F9A00B5F6B3 /* MainMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainMenu.swift; sourceTree = "<group>"; };
@ -161,6 +163,7 @@
C476FF9722B0DD830098105B /* Alert.swift */,
C41C1B4A22B019FF00E7CF16 /* PhpVersion.swift */,
C41C1B4822B00A9800E7CF16 /* MenuBarImageGenerator.swift */,
C474B00524C0E98C00066A22 /* LocalNotification.swift */,
);
path = Helpers;
sourceTree = "<group>";
@ -258,6 +261,7 @@
C41C1B3722B0097F00E7CF16 /* AppDelegate.swift in Sources */,
C41C1B4B22B019FF00E7CF16 /* PhpVersion.swift in Sources */,
C476FF9822B0DD830098105B /* Alert.swift in Sources */,
C474B00624C0E98C00066A22 /* LocalNotification.swift in Sources */,
C47331A2247093B7009A0597 /* StatusMenu.swift in Sources */,
C46FA23F246C358E00944F05 /* StringExtension.swift in Sources */,
C4EE188422D3386B00E126E5 /* Constants.swift in Sources */,
@ -401,7 +405,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 24;
CURRENT_PROJECT_VERSION = 25;
DEVELOPMENT_TEAM = 8M54J5J787;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = phpmon/Info.plist;
@ -409,7 +413,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 2.2;
MARKETING_VERSION = 2.3;
PRODUCT_BUNDLE_IDENTIFIER = com.nicoverbruggen.phpmon;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
@ -425,7 +429,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 24;
CURRENT_PROJECT_VERSION = 25;
DEVELOPMENT_TEAM = 8M54J5J787;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = phpmon/Info.plist;
@ -433,7 +437,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 2.2;
MARKETING_VERSION = 2.3;
PRODUCT_BUNDLE_IDENTIFIER = com.nicoverbruggen.phpmon;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";

View File

@ -39,6 +39,7 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
enableGPUValidationMode = "1"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">

View File

@ -6,11 +6,11 @@ PHP Monitor (or phpmon) is a lightweight macOS utility app that runs on your Mac
It also gives you quick access to various useful functionality (like switching PHP versions, restarting services, accessing configuration files, and more).
<img src="./docs/screenshot.png" width="278px" alt="phpmon screenshot"/>
<img src="./docs/screenshot.png" width="362px" alt="phpmon screenshot"/>
For me, it comes in handy when running multiple versions of PHP with Homebrew. If you wish to be able to see at a glance which version is currently linked & active with Laravel Valet, PHP Monitor is your new best friend.
It's also super convenient to and switch between versions.
It's also super convenient to switch between different versions of PHP, or to find your currently active .ini file!
## 🖥 System requirements
@ -22,12 +22,12 @@ _Please note that future versions of PHP will not work automatically, minor chan
## 🚀 How to install
You can install via Homebrew, or may download the latest [release](https://github.com/nicoverbruggen/phpmon/releases).
You can install via Homebrew, or may download the latest [release][1].
To install via Homebrew, run:
brew tap nicoverbruggen/homebrew-cask
brew cask install phpmon
brew tap nicoverbruggen/homebrew-cask
brew cask install phpmon
## 👨‍💻 Why I built this
@ -54,7 +54,7 @@ This means:
The utility runs the following commands:
- Unlink all detected PHP versions
- Switch to PHP 7.4 (this is done in order to ensure that Valet works, even when attempting to use PHP 5.6)
- Switch to PHP 7.4 (this is done to ensure that Valet works, even when attempting to use PHP 5.6)
- Stop all php-fpm service instances
- Link the desired version of PHP
- Start the correct php-fpm service for the desired PHP version
@ -82,8 +82,11 @@ Follow instructions as specified in the alert in order to resolve any issues.
## 📝 Additional information
Please consult the [additional information](docs/ADDITIONAL.md) file that contains more information.
Please consult the [additional information][2] file that contains more information.
## ⭐️ Is this helpful?
If this software has been useful to you, star the repository so I know that the software is being used. I did not include any tracking or analytics software, so if you encounter issues, let me know via an issue.
If this software has been useful to you, star the repository, so I know that the software is being used. I did not include any tracking or analytics software, so if you encounter issues, let me know via an issue.
[1]: https://github.com/nicoverbruggen/phpmon/releases
[2]: docs/ADDITIONAL.md

14
SECURITY.md Normal file
View File

@ -0,0 +1,14 @@
# Security Policy
## Supported Versions
The following versions of PHP Monitor are supported:
| Version | Supported | Runs on macOS |
| ------- | ------------------ | ----- |
| 2.1 | ✅ | Catalina (10.15), Big Sur (11.0) |
| < 2.1 | | Catalina (10.15) |
## Reporting a Vulnerability
Contact Nico Verbruggen at the email address used for the commits in the repository. Please include "PHP Monitor" in the subject.

View File

@ -1,6 +1,6 @@
# Release Procedure
1. Merge into `master`
1. Merge into `main`
2. Create tag
3. Add changes to changelog
4. Archive
@ -10,4 +10,4 @@
8. Calculate SHA256: `openssl dgst -sha256 phpmon-2.x.zip`
9. Upload to GitHub
10. Update Cask
11. Check new version can be installed via Cask
11. Check new version can be installed via Cask

Binary file not shown.

Before

Width:  |  Height:  |  Size: 615 KiB

After

Width:  |  Height:  |  Size: 295 KiB

View File

@ -7,18 +7,37 @@
//
import Cocoa
import UserNotifications
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {
// MARK: - Variables
/**
The Shell singleton that keeps track of the history of all
(invoked by PHP Monitor) shell commands. It is used to
invoke all commands in this application.
*/
let sharedShell : Shell
/**
The App singleton contains information about the state of
the application and global variables.
*/
let state : App
/**
The MainMenu singleton is responsible for rendering the
menu bar item and its menu, as well as its actions.
*/
let menu : MainMenu
// MARK: - Initializer
/**
When the application initializes, create all singletons.
*/
override init() {
self.sharedShell = Shell.user
self.state = App.shared
@ -27,13 +46,29 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
// MARK: - Lifecycle
/**
When the application has finished launching, we'll want to set up
the user notification center delegate, and kickoff the menu
startup procedure.
*/
func applicationDidFinishLaunching(_ aNotification: Notification) {
NSUserNotificationCenter.default.delegate = self
self.menu.startup()
}
func applicationWillTerminate(_ aNotification: Notification) {
self.state.windowController = nil
// MARK: - NSUserNotificationCenterDelegate
/**
When a notification is sent, the delegate of the notification center
is asked whether the notification should be presented or not. Since
the user can now disable notifications per application since macOS
Catalina, any and all notifications should be displayed.
*/
func userNotificationCenter(
_ center: NSUserNotificationCenter,
shouldPresent notification: NSUserNotification
) -> Bool {
return true
}
}

View File

@ -0,0 +1,19 @@
//
// LocalNotification.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 16/07/2020.
// Copyright © 2020 Nico Verbruggen. All rights reserved.
//
import Foundation
class LocalNotification {
public static func send(title: String, subtitle: String)
{
let notification = NSUserNotification()
notification.title = title
notification.subtitle = subtitle
NSUserNotificationCenter.default.deliver(notification)
}
}

View File

@ -12,17 +12,18 @@ class StatusMenu : NSMenu {
public func addPhpVersionMenuItems()
{
var string = "We are not sure what version of PHP you are running."
var string = "mi_unsure".localized
if (App.shared.currentVersion != nil) {
if (!App.shared.currentVersion!.error) {
string = "You are running PHP \(App.shared.currentVersion!.long)"
// in case the php version loaded without issue
string = "\("mi_php_version".localized) \(App.shared.currentVersion!.long)"
self.addItem(NSMenuItem(title: string, action: nil, keyEquivalent: ""))
} else {
// in case of an error show the error message
self.addItem(NSMenuItem(title: "Oof! It appears your PHP installation is broken...", action: nil, keyEquivalent: ""))
self.addItem(NSMenuItem(title: "Try running `php -v` in your terminal.", action: nil, keyEquivalent: ""))
self.addItem(NSMenuItem(title: "You could also try switching to another version.", action: nil, keyEquivalent: ""))
self.addItem(NSMenuItem(title: "Running `brew reinstall php` (or for the equivalent version) might help.", action: nil, keyEquivalent: ""))
["mi_php_broken_1", "mi_php_broken_2",
"mi_php_broken_3", "mi_php_broken_4"].forEach { (message) in
self.addItem(NSMenuItem(title: message.localized, action: nil, keyEquivalent: ""))
}
}
}
}
@ -34,30 +35,30 @@ class StatusMenu : NSMenu {
for index in (0..<App.shared.availablePhpVersions.count).reversed() {
let version = App.shared.availablePhpVersions[index]
let action = #selector(MainMenu.switchToPhpVersion(sender:))
let menuItem = NSMenuItem(title: "Switch to PHP \(version)", action: (version == App.shared.currentVersion?.short) ? nil : action, keyEquivalent: "\(shortcutKey)")
let menuItem = NSMenuItem(title: "\("mi_php_switch".localized) \(version)", action: (version == App.shared.currentVersion?.short) ? nil : action, keyEquivalent: "\(shortcutKey)")
menuItem.tag = index
shortcutKey = shortcutKey + 1
self.addItem(menuItem)
}
self.addItem(NSMenuItem.separator())
self.addItem(NSMenuItem(title: "Active Services", action: nil, keyEquivalent: ""))
self.addItem(NSMenuItem(title: "Restart php-fpm service", action: #selector(MainMenu.restartPhpFpm), keyEquivalent: "f"))
self.addItem(NSMenuItem(title: "Restart nginx service", action: #selector(MainMenu.restartNginx), keyEquivalent: "n"))
self.addItem(NSMenuItem(title: "Force load latest PHP version", action: #selector(MainMenu.forceRestartLatestPhp), keyEquivalent: ""))
self.addItem(NSMenuItem(title: "mi_active_services".localized, action: nil, keyEquivalent: ""))
self.addItem(NSMenuItem(title: "mi_restart_php_fpm".localized, action: #selector(MainMenu.restartPhpFpm), keyEquivalent: "f"))
self.addItem(NSMenuItem(title: "mi_restart_nginx".localized, action: #selector(MainMenu.restartNginx), keyEquivalent: "n"))
self.addItem(NSMenuItem(title: "mi_force_load_latest".localized, action: #selector(MainMenu.forceRestartLatestPhp), keyEquivalent: ""))
}
if (App.shared.busy) {
self.addItem(NSMenuItem(title: "PHP Monitor is busy...", action: nil, keyEquivalent: ""))
self.addItem(NSMenuItem(title: "mi_busy".localized, action: nil, keyEquivalent: ""))
}
}
public func addPhpConfigurationMenuItems()
{
if (App.shared.currentVersion != nil) {
self.addItem(NSMenuItem(title: "Configuration", action: nil, keyEquivalent: ""))
self.addItem(NSMenuItem(title: "Valet configuration (.config/valet)", action: #selector(MainMenu.openValetConfigFolder), keyEquivalent: "v"))
self.addItem(NSMenuItem(title: "PHP configuration file (php.ini)", action: #selector(MainMenu.openActiveConfigFolder), keyEquivalent: "c"))
self.addItem(NSMenuItem(title: "mi_configuration".localized, action: nil, keyEquivalent: ""))
self.addItem(NSMenuItem(title: "mi_valet_config".localized, action: #selector(MainMenu.openValetConfigFolder), keyEquivalent: "v"))
self.addItem(NSMenuItem(title: "mi_php_config".localized, action: #selector(MainMenu.openActiveConfigFolder), keyEquivalent: "c"))
self.addItem(NSMenuItem.separator())
self.addItem(NSMenuItem(title: "Enabled Extensions", action: nil, keyEquivalent: ""))
self.addItem(NSMenuItem(title: "mi_enabled_extensions".localized, action: nil, keyEquivalent: ""))
self.addXdebugMenuItem()
}
}
@ -68,7 +69,7 @@ class StatusMenu : NSMenu {
if (xdebugFound) {
let xdebugOn = App.shared.currentVersion!.xdebugEnabled
let xdebugToggleMenuItem = NSMenuItem(
title: "Xdebug",
title: "mi_xdebug".localized,
action: #selector(MainMenu.toggleXdebug), keyEquivalent: "x"
)
if (xdebugOn) {
@ -77,7 +78,7 @@ class StatusMenu : NSMenu {
self.addItem(xdebugToggleMenuItem)
} else {
let disabledItem = NSMenuItem(
title: "xdebug.so missing",
title: "mi_xdebug_missing".localized,
action: nil, keyEquivalent: "x"
)
disabledItem.isEnabled = false

View File

@ -6,6 +6,38 @@
Copyright © 2020 Nico Verbruggen. All rights reserved.
*/
// MENU ITEMS (MI)
"mi_busy" = "PHP Monitor is busy...";
"mi_unsure" = "We are not sure what version of PHP you are running.";
"mi_php_version" = "You are running PHP";
"mi_php_switch" = "Switch to PHP";
"mi_php_broken_1" = "Oof! It appears your PHP installation is broken...";
"mi_php_broken_2" = "Try running `php -v` in your terminal.";
"mi_php_broken_3" = "You could also try switching to another version.";
"mi_php_broken_4" = "Running `brew reinstall php` (or for the equivalent version) might help.";
"mi_active_services" = "Active Services";
"mi_restart_php_fpm" = "Restart php-fpm service";
"mi_restart_nginx" = "Restart nginx service";
"mi_force_load_latest" = "Force load latest PHP version";
"mi_configuration" = "Configuration";
"mi_valet_config" = "Valet configuration (.config/valet)";
"mi_php_config" = "PHP Configuration file (php.ini)";
"mi_enabled_extensions" = "Enabled Extensions";
"mi_xdebug" = "Xdebug";
"mi_xdebug_missing" = "xdebug.so missing";
"mi_quit" = "Quit PHP Monitor";
"mi_about" = "About PHP Monitor";
// NOTIFICATIONS
"notification.version_changed_title" = "PHP %@ now active";
"notification.version_changed_desc" = "PHP Monitor has finished the switch to PHP %@.";
// ALERTS
// Force Reload Started
@ -15,3 +47,9 @@
// Force Reload Done
"alert.force_reload_done.title" = "PHP has been force reloaded";
"alert.force_reload_done.info" = "All appropriate services have been restarted, and the latest version of PHP is now active. You can now try switching to another version of PHP.";
// PHP Monitor Cannot Start
"alert.cannot_start.title" = "PHP Monitor cannot start";
"alert.cannot_start.info" = "The issue you were just notified about is keeping PHP Monitor from functioning correctly. Please fix the issue and restart PHP Monitor. After clicking on OK, PHP Monitor will close.\n\nIf you have fixed the issue (or don't remember what the exact issue is) you can click on Retry, which will have PHP Monitor retry the startup checks.";
"alert.cannot_start.close" = "Close";
"alert.cannot_start.retry" = "Retry";

View File

@ -32,9 +32,4 @@ class App {
*/
var timer: Timer?
/**
The window controller that will show the log.
*/
var windowController: NSWindowController? = nil
}

View File

@ -9,8 +9,13 @@
import Cocoa
class Command {
/// Immediately executes a command.
/**
Immediately executes a command.
- Parameter path: The path of the command or program to invoke.
- Parameter arguments: A list of arguments that are passed on.
*/
public static func execute(path: String, arguments: [String]) -> String {
let task = Process()
task.launchPath = path

View File

@ -12,10 +12,18 @@ class MainMenu: NSObject, NSWindowDelegate {
static let shared = MainMenu()
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
/**
The status bar item with variable length.
*/
let statusItem = NSStatusBar.system.statusItem(
withLength: NSStatusItem.variableLength
)
// MARK: - UI related
/**
Kick off the startup of the rendering of the main menu.
*/
public func startup() {
// Start with the icon
self.setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!)
@ -29,6 +37,9 @@ class MainMenu: NSObject, NSWindowDelegate {
}
}
/**
When the environment is all clear and the app can run, let's go.
*/
private func onEnvironmentPass() {
App.shared.availablePhpVersions = Actions.detectPhpVersions()
self.updatePhpVersionInStatusBar()
@ -44,13 +55,16 @@ class MainMenu: NSObject, NSWindowDelegate {
}
}
/**
When the environment is not OK, present an alert to inform the user.
*/
private func onEnvironmentFail() {
DispatchQueue.main.async {
let close = Alert.present(
messageText: "PHP Monitor cannot start",
informativeText: "The issue you were just notified about is keeping PHP Monitor from functioning correctly. Please fix the issue and restart PHP Monitor. After clicking on OK, PHP Monitor will close.\n\nIf you have fixed the issue (or don't remember what the exact issue is) you can click on Retry, which will have PHP Monitor retry the startup checks.",
buttonTitle: "Close",
secondButtonTitle: "Retry"
messageText: "alert.cannot_start.title".localized,
informativeText: "alert.cannot_start.info".localized,
buttonTitle: "alert.cannot_start.close".localized,
secondButtonTitle: "alert.cannot_start.retry".localized
)
if (!close) {
self.startup()
@ -60,6 +74,9 @@ class MainMenu: NSObject, NSWindowDelegate {
}
}
/**
Update the menu's contents, based on what's going on.
*/
public func update() {
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
// Create a new menu
@ -78,8 +95,8 @@ class MainMenu: NSObject, NSWindowDelegate {
menu.addItem(NSMenuItem.separator())
// Add about & quit menu items
menu.addItem(NSMenuItem(title: "About PHP Monitor", action: #selector(self.openAbout), keyEquivalent: ""))
menu.addItem(NSMenuItem(title: "Quit PHP Monitor", action: #selector(self.terminateApp), keyEquivalent: "q"))
menu.addItem(NSMenuItem(title: "mi_about".localized, action: #selector(self.openAbout), keyEquivalent: ""))
menu.addItem(NSMenuItem(title: "mi_quit".localized, action: #selector(self.terminateApp), keyEquivalent: "q"))
// Make sure every item can be interacted with
menu.items.forEach({ (item) in
@ -93,10 +110,19 @@ class MainMenu: NSObject, NSWindowDelegate {
}
}
/**
Sets the status bar image based on a version string.
*/
func setStatusBarImage(version: String) {
self.setStatusBar(image: MenuBarImageGenerator.textToImage(text: version))
self.setStatusBar(
image: MenuBarImageGenerator.textToImage(text: version)
)
}
/**
Sets the status bar image, based on the provided NSImage.
The image will be used as a template image.
*/
func setStatusBar(image: NSImage) {
if let button = statusItem.button {
image.isTemplate = true
@ -106,6 +132,14 @@ class MainMenu: NSObject, NSWindowDelegate {
// MARK: - Nicer callbacks
/**
Executes a specific callback and fires the completion callback,
while updating the UI as required. As long as the completion callback
does not fire, the app is presumed to be busy and the UI reflects this.
- Parameter execute: Escaping callback of the work that needs to happen.
- Parameter completion: Callback that is fired when the work is done.
*/
private func waitAndExecute(_ execute: @escaping () -> Void, _ completion: @escaping () -> Void = {})
{
App.shared.busy = true
@ -122,7 +156,7 @@ class MainMenu: NSObject, NSWindowDelegate {
}
}
// MARK: - Actions
// MARK: - User Interface
@objc func updatePhpVersionInStatusBar() {
App.shared.currentVersion = PhpVersion()
@ -144,6 +178,8 @@ class MainMenu: NSObject, NSWindowDelegate {
}
}
// MARK: - Actions
@objc public func restartPhpFpm() {
self.waitAndExecute({
Actions.restartPhpFpm()
@ -163,10 +199,12 @@ class MainMenu: NSObject, NSWindowDelegate {
}
@objc public func forceRestartLatestPhp() {
// Tell the user the switch is about to occur
_ = Alert.present(
messageText: "alert.force_reload.title".localized,
informativeText: "alert.force_reload.info".localized
)
// Start switching
self.waitAndExecute({ Actions.fixMyPhp() }, {
_ = Alert.present(
messageText: "alert.force_reload_done.title".localized,
@ -192,6 +230,7 @@ class MainMenu: NSObject, NSWindowDelegate {
@objc public func switchToPhpVersion(sender: AnyObject) {
self.setBusyImage()
// TODO: A wise man once said: using tags is not good. Fix this.
let index = sender.tag!
let version = App.shared.availablePhpVersions[index]
App.shared.busy = true
@ -211,6 +250,11 @@ class MainMenu: NSObject, NSWindowDelegate {
DispatchQueue.main.async {
self.updatePhpVersionInStatusBar()
self.update()
// Send a notification that the switch has been completed
LocalNotification.send(
title: String(format: "notification.version_changed_title".localized, version),
subtitle: String(format: "notification.version_changed_desc".localized, version)
)
}
}
}
@ -223,11 +267,4 @@ class MainMenu: NSObject, NSWindowDelegate {
@objc public func terminateApp() {
NSApplication.shared.terminate(nil)
}
// MARK: - Cleanup when window closes
func windowWillClose(_ notification: Notification) {
App.shared.windowController = nil
Shell.user.delegate = nil
}
}

View File

@ -8,38 +8,30 @@
import Cocoa
protocol ShellDelegate: class {
func didCompleteCommand(historyItem: ShellHistoryItem)
}
class ShellHistoryItem {
var command: String
var output: String
var date: Date
init(command: String, output: String) {
self.command = command
self.output = output
self.date = Date()
}
}
class Shell {
// Singleton to access a user shell (with --login)
/**
Singleton to access a user shell (with --login)
*/
static let user = Shell()
var history : [ShellHistoryItem] = []
var delegate : ShellDelegate?
/// Runs a shell command without using the description.
/**
Runs a shell command without using the output.
Uses the default shell.
- Parameter command: The command to run
*/
public func run(_ command: String) {
// Equivalent of piping to /dev/null; don't do anything with the string
_ = self.pipe(command)
}
/// Runs a shell command and returns the output.
/**
Runs a shell command and returns the output.
- Parameter command: The command to run
- Parameter shell: Path to the shell to invoke
*/
public func pipe(_ command: String, shell: String = "/bin/sh") -> String {
let task = Process()
task.launchPath = shell
@ -51,17 +43,10 @@ class Shell {
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
let historyItem = ShellHistoryItem(command: command, output: output)
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
self.history.append(historyItem)
// Keep the last 100 items
self.history = self.history.suffix(100)
}
delegate?.didCompleteCommand(historyItem: historyItem)
let output: String = NSString(
data: data,
encoding: String.Encoding.utf8.rawValue
)! as String
return output
}