Skip to content

Commit

Permalink
✨ Switch Xdebug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoverbruggen committed May 18, 2022
1 parent 990152d commit e7f80eb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
6 changes: 5 additions & 1 deletion phpmon/Common/PHP/Extensions/Xdebug.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ class Xdebug {
}

public static var mode: String {
return Command.execute(path: Paths.php, arguments: ["-r", "echo ini_get('xdebug.mode');"])
guard let file = PhpEnv.shared.getConfigFile(forKey: "xdebug.mode") else {
return ""
}

return file.get(for: "xdebug.mode") ?? ""
}

public static var modes: [String] {
Expand Down
10 changes: 10 additions & 0 deletions phpmon/Common/PHP/PHP Version/PhpEnv.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,14 @@ class PhpEnv {

return false
}

/**
Returns the configuration file instance that is used for a specific config value.
You can then use the configuration file instance to change values.
*/
public func getConfigFile(forKey key: String) -> PhpConfigurationFile? {
return PhpEnv.phpInstall.iniFiles
.reversed()
.first(where: { $0.has(key: key) })
}
}
17 changes: 17 additions & 0 deletions phpmon/Domain/Menu/MainMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,23 @@ class MainMenu: NSObject, NSWindowDelegate, NSMenuDelegate, PhpSwitcherDelegate

@objc func toggleXdebugMode(sender: XdebugMenuItem) {
Log.info("Switching Xdebug to mode: \(sender.mode)")

guard let file = PhpEnv.shared.getConfigFile(forKey: "xdebug.mode") else {
Log.info("xdebug.mode could not be found in any .ini file, aborting.")
return
}

do {
// Replace the xdebug mode
try file.replace(key: "xdebug.mode", value: sender.mode)
// Refresh the menu
Log.perf("Refreshing menu...")
MainMenu.shared.rebuild()
// Restart PHP-FPM
restartPhpFpm()
} catch {
Log.err("There was an issue replacing `xdebug.mode` in \(file.filePath)")
}
}

@objc func toggleExtension(sender: ExtensionMenuItem) {
Expand Down
2 changes: 1 addition & 1 deletion phpmon/Domain/Menu/StatusMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class StatusMenu: NSMenu {

self.addItem(NSMenuItem.separator())

// self.addXdebugMenuItem()
self.addXdebugMenuItem()

self.addFirstAidAndServicesMenuItems()
}
Expand Down
8 changes: 4 additions & 4 deletions phpmon/Domain/Watcher/App+ConfigWatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import Foundation
extension App {

func startWatcher(_ url: URL) {
Log.info("No watcher currently active...")
Log.perf("No watcher currently active...")
self.watcher = PhpConfigWatcher(for: url)

self.watcher.didChange = { url in
Log.info("Something has changed in: \(url)")
Log.perf("Something has changed in: \(url)")

// Check if the watcher has last updated the menu less than 0.75s ago
let distance = self.watcher.lastUpdate?.distance(to: Date().timeIntervalSince1970)
if distance == nil || distance != nil && distance! > 0.75 {
Log.info("Refreshing menu...")
Log.perf("Refreshing menu...")
MainMenu.shared.reloadPhpMonitorMenuInBackground()
self.watcher.lastUpdate = Date().timeIntervalSince1970
}
Expand All @@ -43,7 +43,7 @@ extension App {
if self.watcher.url != url || forceReload {
self.watcher.disable()
self.watcher = nil
Log.info("Watcher has stopped watching files. Starting new one...")
Log.perf("Watcher has stopped watching files. Starting new one...")
self.startWatcher(url)
}
}
Expand Down

0 comments on commit e7f80eb

Please sign in to comment.