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

🔀 Merge branch 'develop'

* develop:
  🔥 Remove broken log
  🔧 Version bump
  🚧 WIP: Adds support to PHP 7.4
  ️ Use Command.execute to retrieve PHP version
  🚧 WIP: Add toggle to enable or disable Xdebug
  📝 Update readme
This commit is contained in:
2019-12-20 11:59:04 +01:00
11 changed files with 181 additions and 109 deletions

View File

@ -8,13 +8,13 @@
/* Begin PBXBuildFile section */
C41C1B3722B0097F00E7CF16 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41C1B3622B0097F00E7CF16 /* AppDelegate.swift */; };
C41C1B3922B0097F00E7CF16 /* LogViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41C1B3822B0097F00E7CF16 /* LogViewController.swift */; };
C41C1B3B22B0098000E7CF16 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C41C1B3A22B0098000E7CF16 /* Assets.xcassets */; };
C41C1B3E22B0098000E7CF16 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C41C1B3C22B0098000E7CF16 /* Main.storyboard */; };
C41C1B4722B009A400E7CF16 /* Shell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41C1B4622B009A400E7CF16 /* Shell.swift */; };
C41C1B4922B00A9800E7CF16 /* MenuBarImageGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41C1B4822B00A9800E7CF16 /* MenuBarImageGenerator.swift */; };
C41C1B4B22B019FF00E7CF16 /* PhpVersion.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41C1B4A22B019FF00E7CF16 /* PhpVersion.swift */; };
C41C1B4D22B0215A00E7CF16 /* Actions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41C1B4C22B0215A00E7CF16 /* Actions.swift */; };
C42295DD2358D02000E263B2 /* Command.swift in Sources */ = {isa = PBXBuildFile; fileRef = C42295DC2358D02000E263B2 /* Command.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 */; };
@ -26,7 +26,6 @@
/* Begin PBXFileReference section */
C41C1B3322B0097F00E7CF16 /* PHP Monitor.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "PHP Monitor.app"; sourceTree = BUILT_PRODUCTS_DIR; };
C41C1B3622B0097F00E7CF16 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
C41C1B3822B0097F00E7CF16 /* LogViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogViewController.swift; sourceTree = "<group>"; };
C41C1B3A22B0098000E7CF16 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
C41C1B3D22B0098000E7CF16 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
C41C1B3F22B0098000E7CF16 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@ -35,6 +34,7 @@
C41C1B4822B00A9800E7CF16 /* MenuBarImageGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuBarImageGenerator.swift; sourceTree = "<group>"; };
C41C1B4A22B019FF00E7CF16 /* PhpVersion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhpVersion.swift; sourceTree = "<group>"; };
C41C1B4C22B0215A00E7CF16 /* Actions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Actions.swift; sourceTree = "<group>"; };
C42295DC2358D02000E263B2 /* Command.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Command.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>"; };
@ -101,7 +101,6 @@
isa = PBXGroup;
children = (
C41C1B3C22B0098000E7CF16 /* Main.storyboard */,
C41C1B3822B0097F00E7CF16 /* LogViewController.swift */,
);
path = "View Controllers";
sourceTree = "<group>";
@ -112,6 +111,7 @@
C41C1B4622B009A400E7CF16 /* Shell.swift */,
C4811D2322D70A4700B5F6B3 /* App.swift */,
C4811D2922D70F9A00B5F6B3 /* MainMenu.swift */,
C42295DC2358D02000E263B2 /* Command.swift */,
);
path = Singletons;
sourceTree = "<group>";
@ -170,7 +170,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1020;
LastUpgradeCheck = 1020;
LastUpgradeCheck = 1110;
ORGANIZATIONNAME = "Nico Verbruggen";
TargetAttributes = {
C41C1B3222B0097F00E7CF16 = {
@ -217,10 +217,10 @@
C4F8C0A422D4F12C002EFE61 /* DateExtension.swift in Sources */,
C41C1B4722B009A400E7CF16 /* Shell.swift in Sources */,
C41C1B4D22B0215A00E7CF16 /* Actions.swift in Sources */,
C42295DD2358D02000E263B2 /* Command.swift in Sources */,
C4811D2422D70A4700B5F6B3 /* App.swift in Sources */,
C41C1B4922B00A9800E7CF16 /* MenuBarImageGenerator.swift in Sources */,
C4811D2A22D70F9A00B5F6B3 /* MainMenu.swift in Sources */,
C41C1B3922B0097F00E7CF16 /* LogViewController.swift in Sources */,
C41C1B3722B0097F00E7CF16 /* AppDelegate.swift in Sources */,
C41C1B4B22B019FF00E7CF16 /* PhpVersion.swift in Sources */,
C476FF9822B0DD830098105B /* Alert.swift in Sources */,
@ -362,15 +362,17 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = phpmon/phpmon.entitlements;
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 12;
CURRENT_PROJECT_VERSION = 16;
DEVELOPMENT_TEAM = 8M54J5J787;
INFOPLIST_FILE = phpmon/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.6;
PRODUCT_BUNDLE_IDENTIFIER = com.nicoverbruggen.phpmon;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
@ -382,15 +384,17 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = phpmon/phpmon.entitlements;
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 12;
CURRENT_PROJECT_VERSION = 16;
DEVELOPMENT_TEAM = 8M54J5J787;
INFOPLIST_FILE = phpmon/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.6;
PRODUCT_BUNDLE_IDENTIFIER = com.nicoverbruggen.phpmon;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;

View File

@ -8,18 +8,16 @@ For me, it comes in handy when running multiple versions of PHP with Homebrew an
## System requirements
Minimal system requirements are:
**Minimal system requirements**
* macOS 10.14 or higher
* PHP 7.3 installed via Homebrew
* PHP 7.4 installed via Homebrew
* Laravel Valet 2.3 or higher installed
## Recommended setup
This means that this configuration is recommended and supported:
**Recommended system**
* macOS 10.15 Catalina
* PHP 7.3.x installed with Homebrew 2
* PHP 7.4 installed with Homebrew 2.2
- other versions of PHP are optional
- includes support for PHP 5.6 and PHP 7.0 [as well](https://github.com/eXolnet/homebrew-deprecated)
* Laravel Valet 2.5.x installed
@ -42,14 +40,14 @@ This utility will detect which PHP versions you have installed via Homebrew, and
This means:
- You have at least the latest version of PHP installed (`php@7.3`)
- You have at least the latest version of PHP installed (`php@7.4`)
- You have installed Laravel Valet (`which valet` returns `/usr/local/bin/valet`)
- You ran `valet trust`, which means Valet commands can be run without using sudo
The utility runs the following commands:
- Unlink all detected PHP versions
- Switch to PHP 7.3 (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 in order to ensure that Valet works, even when attempting to use PHP 5.6)
- Tell Valet to switch to a specific PHP version
- Link the desired version of PHP
@ -58,3 +56,27 @@ The utility runs the following commands:
If you want to know more about how this works, I recommend you check out the source code.
This app isn't very complicated after all. In the end, this just (conveniently) executes some shell commands.
## Troubleshooting
### Reasons for alerts at startup
PHP Monitor performs some integrity checks to ensure a good experience when using the app. You'll get a message telling you that PHP Monitor won't work correctly in the following scenarios:
- The PHP binary is not located in `/usr/local/bin/php`
- PHP 7.4 is missing in `/usr/local/opt`
- Laravel Valet is missing in `/usr/local/bin/valet`
- Brew has not been added to sudoers in `/private/etc/sudoers.d/brew`
- Valet has not been added to sudoers in `/private/etc/sudoers.d/valet`
Follow instructions as specified in the alert in order to resolve any issues.
### Still seeing another PHP version (from before switching versions)?
If you're still seeing an old version of PHP in your scripts — e.g. when running `phpinfo()` — I recommend you shut down the PHP service by running:
sudo brew services stop php
Please note that PHP Monitor will not be able to stop this service (it doesn't run as an administrator), so you'll need to handle this yourself.
You should only have to do this **once**, and then PHP Monitor should work as usual.

View File

@ -30,8 +30,8 @@ class Actions {
availableVersions.forEach { (version) in
Shell.user.run("brew unlink php@\(version)")
}
if (availableVersions.contains("7.3")) {
Shell.user.run("brew link php@7.3")
if (availableVersions.contains("7.4")) {
Shell.user.run("brew link php@7.4")
if (version == Constants.LatestPhpVersion) {
Shell.user.run("valet use php")
} else {
@ -42,6 +42,35 @@ class Actions {
public static func openPhpConfigFolder(version: String) {
let files = [NSURL(fileURLWithPath: "/usr/local/etc/php/\(version)/php.ini")];
NSWorkspace.shared.activateFileViewerSelecting(files as [URL]);
NSWorkspace.shared.activateFileViewerSelecting(files as [URL])
}
public static func XdebugFound(_ version: String) -> Bool {
let command = """
grep -q 'zend_extension="xdebug.so"' /usr/local/etc/php/\(version)/php.ini; [ $? -eq 0 ] && echo "YES" || echo "NO"
"""
let output = Shell.user.pipe(command).trimmingCharacters(in: .whitespacesAndNewlines)
return (output == "YES")
}
public static func XdebugEnabled(_ version: String) -> Bool {
let command = """
grep -q '; zend_extension="xdebug.so"' /usr/local/etc/php/\(version)/php.ini; [ $? -eq 0 ] && echo "YES" || echo "NO"
"""
let output = Shell.user.pipe(command).trimmingCharacters(in: .whitespacesAndNewlines)
return (output == "NO")
}
public static func toggleXdebug() {
let version = App.shared.currentVersion?.short
var command = """
sed -i '' 's/; zend_extension="xdebug.so"/zend_extension="xdebug.so"/g' /usr/local/etc/php/\(version!)/php.ini
"""
if (self.XdebugEnabled(version!)) {
command = """
sed -i '' 's/zend_extension="xdebug.so"/; zend_extension="xdebug.so"/g' /usr/local/etc/php/\(version!)/php.ini
"""
}
Shell.user.run(command)
}
}

View File

@ -19,9 +19,9 @@ class Startup {
)
self.presentAlertOnMainThreadIf(
!Shell.user.pipe("ls /usr/local/opt | grep php@7.3").contains("php@7.3"),
messageText: "PHP 7.3 is not correctly installed",
informativeText: "PHP 7.3 alias was not found in `/usr/local/opt`. The app will not work correctly until you resolve this issue."
!Shell.user.pipe("ls /usr/local/opt | grep php@7.4").contains("php@7.4"),
messageText: "PHP 7.4 is not correctly installed",
informativeText: "PHP 7.4 alias was not found in `/usr/local/opt`. The app will not work correctly until you resolve this issue."
)
self.presentAlertOnMainThreadIf(

View File

@ -13,17 +13,25 @@ class PhpVersion {
var short : String = "???"
var long : String = "???"
var xdebugFound: Bool = false
var xdebugEnabled : Bool = false
init() {
let version = Shell.user
// Get the version directly from PHP
.pipe("php -r 'print phpversion();'")
let version = Command.execute(path: "/usr/local/bin/php", arguments: ["-r", "print phpversion();"])
// That's the long version
self.long = version
// Next up, let's strip away the minor version number
let segments = long.components(separatedBy: ".")
// Get the first two elements
self.short = segments[0...1].joined(separator: ".")
// Load xdebug support
self.xdebugFound = Actions.XdebugFound(self.short)
if (self.xdebugFound) {
self.xdebugEnabled = Actions.XdebugEnabled(self.short)
}
}
}

View File

@ -14,13 +14,13 @@ class Constants {
* The PHP versions supported by this application.
*/
static let SupportedPhpVersions = [
"5.6", "7.0", "7.1", "7.2", "7.3"
"5.6", "7.0", "7.1", "7.2", "7.3", "7.4"
]
/**
Which php version is aliased as `php` to brew?
This is usually the latest PHP version.
*/
static let LatestPhpVersion = "7.3"
static let LatestPhpVersion = "7.4"
}

View File

@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.4</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>LSApplicationCategoryType</key>

View File

@ -0,0 +1,50 @@
//
// Command.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 17/10/2019.
// Copyright © 2019 Nico Verbruggen. All rights reserved.
//
import Cocoa
class Command {
public static func execute(path: String, arguments: [String]) -> String {
let task = Process()
task.launchPath = path
task.arguments = arguments
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = String.init(data: data, encoding: String.Encoding.utf8)!
return output;
}
public static func experiment() {
/*
print("Running '/usr/local/bin/php -v' directly...")
print("========================================")
var start = DispatchTime.now()
print(Command.execute(path: "/usr/local/bin/php", arguments: ["-v"]))
var end = DispatchTime.now()
var nanoTime = end.uptimeNanoseconds - start.uptimeNanoseconds
var timeInterval = Double(nanoTime) / 1_000_000_000
print("Time to run command directly: \(timeInterval) seconds")
print("")
print("Running 'bash -> php -v'...")
print("========================================")
start = DispatchTime.now()
print(Shell.user.pipe("php -v"))
end = DispatchTime.now()
nanoTime = end.uptimeNanoseconds - start.uptimeNanoseconds
timeInterval = Double(nanoTime) / 1_000_000_000
print("Time to run command via bash: \(timeInterval) seconds")
*/
}
}

View File

@ -63,9 +63,27 @@ class MainMenu: NSObject, NSWindowDelegate {
menu.addItem(NSMenuItem.separator())
}
if (App.shared.currentVersion != nil) {
menu.addItem(NSMenuItem(title: "PHP configuration file (php.ini)", action: #selector(self.openActiveConfigFolder), keyEquivalent: ""))
menu.addItem(NSMenuItem(title: "PHP configuration file (php.ini)", action: #selector(self.openActiveConfigFolder), keyEquivalent: "c"))
let xdebugFound = App.shared.currentVersion!.xdebugFound
if (xdebugFound) {
let xdebugOn = App.shared.currentVersion!.xdebugEnabled
let xdebugToggleMenuItem = NSMenuItem(
title: "Xdebug",
action: #selector(self.toggleXdebug), keyEquivalent: "x"
)
if (xdebugOn) {
xdebugToggleMenuItem.state = .on
}
menu.addItem(xdebugToggleMenuItem)
} else {
let disabledItem = NSMenuItem(
title: "xdebug.so missing",
action: nil, keyEquivalent: "x"
)
disabledItem.isEnabled = false
menu.addItem(disabledItem)
}
}
menu.addItem(NSMenuItem(title: "View shell output", action: #selector(self.openOutput), keyEquivalent: ""))
menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem(title: "About PHP Monitor", action: #selector(self.openAbout), keyEquivalent: ""))
menu.items.forEach({ (item) in
@ -95,10 +113,6 @@ class MainMenu: NSObject, NSWindowDelegate {
// MARK: - Callable via Obj-C (#selector)
@objc func openOutput() {
LogViewController.show(delegate: self)
}
@objc func updatePhpVersionInStatusBar() {
App.shared.currentVersion = PhpVersion()
if (App.shared.busy) {
@ -147,6 +161,19 @@ class MainMenu: NSObject, NSWindowDelegate {
}
}
@objc public func toggleXdebug() {
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
DispatchQueue.main.async {
self.setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!)
}
Actions.toggleXdebug()
DispatchQueue.main.async {
self.updatePhpVersionInStatusBar()
self.update()
}
}
}
func windowWillClose(_ notification: Notification) {
App.shared.windowController = nil
Shell.user.delegate = nil

View File

@ -60,11 +60,15 @@ class Shell {
) as String
let historyItem = ShellHistoryItem(command: command, output: output)
history.append(historyItem)
// Keep the last 100 items
history = history.suffix(100)
delegate?.didCompleteCommand(historyItem: historyItem)
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)
return output
}
}

View File

@ -1,72 +0,0 @@
//
// ViewController.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 11/06/2019.
// Copyright © 2019 Nico Verbruggen. All rights reserved.
//
import Cocoa
class LogViewController: NSViewController, ShellDelegate {
public static func show(delegate: NSWindowDelegate? = nil) {
if (App.shared.windowController == nil) {
let vc = NSStoryboard(name: "Main", bundle: nil).instantiateController(withIdentifier: "logWindow") as! LogViewController
Shell.user.delegate = vc
let window = NSWindow(contentViewController: vc)
window.title = "Shell output (/bin/bash --login)"
window.delegate = delegate
App.shared.windowController = NSWindowController(window: window)
}
App.shared.windowController!.showWindow(self)
NSApp.activate(ignoringOtherApps: true)
}
@IBOutlet var textView: NSTextView!
public func appendHistoryItem(_ historyItem: ShellHistoryItem) {
self.append(
"""
======
@ \(historyItem.date.toString())
------
$ \(historyItem.command)
------
> \(historyItem.output)
"""
)
}
public func append(_ text : String) {
self.textView.textStorage?.append(
NSAttributedString(
string: text,
attributes: [
NSAttributedString.Key.font: NSFont(name: "Menlo", size: 12.0)!
]
)
)
self.textView.scrollToEndOfDocument(nil)
}
override func viewDidLoad() {
self.textView.isEditable = false
for entry in Shell.user.history {
self.appendHistoryItem(entry)
}
}
func didCompleteCommand(historyItem: ShellHistoryItem) {
self.appendHistoryItem(historyItem)
}
@IBAction func pressed(_ sender: Any) {
self.view.window?.windowController?.close()
}
deinit {
print("VC deallocated")
}
}