diff --git a/phpmon.xcodeproj/project.pbxproj b/phpmon.xcodeproj/project.pbxproj index f148295..d22cbe6 100644 --- a/phpmon.xcodeproj/project.pbxproj +++ b/phpmon.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ C41C1B3E22B0098000E7CF16 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C41C1B3C22B0098000E7CF16 /* Main.storyboard */; }; C41C1B4722B009A400E7CF16 /* Shell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41C1B4622B009A400E7CF16 /* Shell.swift */; }; C41C1B4922B00A9800E7CF16 /* ImageGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41C1B4822B00A9800E7CF16 /* ImageGenerator.swift */; }; + C41C1B4B22B019FF00E7CF16 /* PhpVersionExtractor.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41C1B4A22B019FF00E7CF16 /* PhpVersionExtractor.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -25,6 +26,7 @@ C41C1B4022B0098000E7CF16 /* phpmon.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = phpmon.entitlements; sourceTree = ""; }; C41C1B4622B009A400E7CF16 /* Shell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Shell.swift; sourceTree = ""; }; C41C1B4822B00A9800E7CF16 /* ImageGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageGenerator.swift; sourceTree = ""; }; + C41C1B4A22B019FF00E7CF16 /* PhpVersionExtractor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhpVersionExtractor.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -65,6 +67,7 @@ C41C1B4022B0098000E7CF16 /* phpmon.entitlements */, C41C1B4622B009A400E7CF16 /* Shell.swift */, C41C1B4822B00A9800E7CF16 /* ImageGenerator.swift */, + C41C1B4A22B019FF00E7CF16 /* PhpVersionExtractor.swift */, ); path = phpmon; sourceTree = ""; @@ -143,6 +146,7 @@ C41C1B4922B00A9800E7CF16 /* ImageGenerator.swift in Sources */, C41C1B3922B0097F00E7CF16 /* ViewController.swift in Sources */, C41C1B3722B0097F00E7CF16 /* AppDelegate.swift in Sources */, + C41C1B4B22B019FF00E7CF16 /* PhpVersionExtractor.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/phpmon/AppDelegate.swift b/phpmon/AppDelegate.swift index 2c1f294..1fb4b3b 100644 --- a/phpmon/AppDelegate.swift +++ b/phpmon/AppDelegate.swift @@ -12,6 +12,7 @@ import Cocoa class AppDelegate: NSObject, NSApplicationDelegate { var timer: Timer? + var version: PhpVersionExtractor? = nil let statusItem = NSStatusBar.system.statusItem( withLength: 40 @@ -37,8 +38,21 @@ class AppDelegate: NSObject, NSApplicationDelegate { } @objc func updatePhpVersionInStatusBar() { - let version = Shell.extractPhpVersion() - self.setStatusBarImage(version: version) + self.version = PhpVersionExtractor() + self.setStatusBarImage(version: self.version!.short) + self.updateMenu() + } + + func updateMenu() { + let menu = NSMenu() + var string = "We are not sure what version of PHP you are running." + if (version != nil) { + string = "You are running PHP \(version!.long)" + } + menu.addItem(NSMenuItem(title: string, action: nil, keyEquivalent: "")) + menu.addItem(NSMenuItem.separator()) + menu.addItem(NSMenuItem(title: "Quit phpmon", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")) + statusItem.menu = menu } } diff --git a/phpmon/PhpVersionExtractor.swift b/phpmon/PhpVersionExtractor.swift new file mode 100644 index 0000000..5ef81d7 --- /dev/null +++ b/phpmon/PhpVersionExtractor.swift @@ -0,0 +1,29 @@ +// +// PhpVersionExtractor.swift +// phpmon +// +// Created by Nico Verbruggen on 11/06/2019. +// Copyright © 2019 Nico Verbruggen. All rights reserved. +// + +import Foundation + +class PhpVersionExtractor { + + var short : String = "???" + var long : String = "???" + + init() { + // Get the info about the PHP installation + let output = Shell.execute(command: "php -v") + // Get everything before "(cli)" (PHP X.X.X (cli) ...) + var version = output!.components(separatedBy: " (cli)")[0] + // Strip away the text before the version number + version = version.components(separatedBy: "PHP ")[1] + self.long = version + // Next up, let's strip away the minor version number + let segments = version.components(separatedBy: ".") + // Get the first two elements + self.short = segments[0...1].joined(separator: ".") + } +} diff --git a/phpmon/Shell.swift b/phpmon/Shell.swift index 4eebf92..1ab90eb 100644 --- a/phpmon/Shell.swift +++ b/phpmon/Shell.swift @@ -24,18 +24,4 @@ class Shell { return output } - - public static func extractPhpVersion() -> String - { - // Get the info about the PHP installation - let output = self.execute(command: "php -v") - // Get everything before "(cli)" (PHP X.X.X (cli) ...) - var version = output!.components(separatedBy: " (cli)")[0] - // Strip away the text before the version number - version = version.components(separatedBy: "PHP ")[1] - // Next up, let's strip away the minor version number - let segments = version.components(separatedBy: ".") - // Get the first two elements - return segments[0...1].joined(separator: ".") - } }