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

♻️ Separate some of the PHP config logic from the app

This commit is contained in:
2021-12-21 15:30:50 +01:00
parent 2dbf775ad6
commit a6387e96e7
18 changed files with 211 additions and 153 deletions

View File

@ -1,6 +1,6 @@
//
// Command.swift
// PMCommon
// phpmon-common
//
// Copyright © 2021 Nico Verbruggen. All rights reserved.
//

View File

@ -1,38 +1,24 @@
//
// Paths.swift
// PMCommon
// phpmon-common
//
// Copyright © 2021 Nico Verbruggen. All rights reserved.
//
import Foundation
public enum HomebrewDir: String {
case opt = "/opt/homebrew"
case usr = "/usr/local"
}
/**
The `Paths` class is used to locate various binaries on the system,
and provides a full
*/
public class Paths {
public static let shared = Paths()
var baseDir : HomebrewDir
private var baseDir : Paths.HomebrewDir
init() {
let optBrewFound = Shell.fileExists("\(HomebrewDir.opt.rawValue)/bin/brew")
let usrBrewFound = Shell.fileExists("\(HomebrewDir.usr.rawValue)/bin/brew")
if (optBrewFound) {
// This is usually the case with Homebrew installed on Apple Silicon
baseDir = .opt
} else if (usrBrewFound) {
// This is usually the case with Homebrew installed on Intel (or Rosetta 2)
baseDir = .usr
} else {
// Falling back to default "legacy" Homebrew location (for Intel)
print("Seems like we couldn't determine the Homebrew directory.")
print("This usually means we're in trouble... (no Homebrew?)")
baseDir = .usr
}
baseDir = Shell.fileExists("\(HomebrewDir.opt.rawValue)/bin/brew") ? .opt : .usr
}
// - MARK: Binaries
@ -71,4 +57,11 @@ public class Paths {
return "\(shared.baseDir.rawValue)/etc"
}
// MARK: - Enum
public enum HomebrewDir: String {
case opt = "/opt/homebrew"
case usr = "/usr/local"
}
}

View File

@ -0,0 +1,60 @@
//
// PhpSwitcher.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 21/12/2021.
// Copyright © 2021 Nico Verbruggen. All rights reserved.
//
import Foundation
protocol PhpSwitcherDelegate: AnyObject {
func switcherDidStartSwitching()
func switcherDidCompleteSwitch()
}
class PhpSwitcher {
init() {
self.currentInstall = ActivePhpInstallation()
}
/** The delegate that is informed of updates. */
weak var delegate: PhpSwitcherDelegate?
/** The static app instance. Accessible at any time. */
static let shared = PhpSwitcher()
/** Whether the switcher is busy performing any actions. */
var isBusy: Bool = false
/** All available versions of PHP. */
var availablePhpVersions: [String] = []
/** Cached information about the PHP installations. */
var cachedPhpInstallations: [String: PhpInstallation] = [:]
/** Static accessor for `PhpSwitcher.shared.currentInstall`. */
static var phpInstall: ActivePhpInstallation {
return Self.shared.currentInstall
}
/** Information about the currently linked PHP installation. */
var currentInstall: ActivePhpInstallation
/**
The version that the `php` formula via Brew is aliased to on the current system.
If you're up to date, `php` will be aliased to the latest version,
but that might not be the case.
*/
var brewPhpVersion: String {
return homebrewPackage.version
}
/**
Information we were able to discern from the Homebrew info command.
*/
var homebrewPackage: HomebrewPackage! = nil
}

View File

@ -1,6 +1,6 @@
//
// Shell.swift
// PMCommon
// phpmon-common
//
// Copyright © 2021 Nico Verbruggen. All rights reserved.
//
@ -80,7 +80,7 @@ public class Shell {
public func executeSynchronously(
_ command: String,
requiresPath: Bool = false
) -> ShellOutput {
) -> Shell.Output {
let outputPipe = Pipe()
let errorPipe = Pipe()
@ -91,7 +91,7 @@ public class Shell {
task.launch()
task.waitUntilExit()
return ShellOutput(
return Shell.Output(
standardOutput: String(
data: outputPipe.fileHandleForReading.readDataToEndOfFile(),
encoding: .utf8
@ -162,18 +162,18 @@ public class Shell {
NotificationCenter.default.removeObserver(pipe.fileHandleForReading)
}
}
}
public class ShellOutput {
public let standardOutput: String
public let errorOutput: String
public let task: Process
init(standardOutput: String,
errorOutput: String,
task: Process) {
self.standardOutput = standardOutput
self.errorOutput = errorOutput
self.task = task
public class Output {
public let standardOutput: String
public let errorOutput: String
public let task: Process
init(standardOutput: String,
errorOutput: String,
task: Process) {
self.standardOutput = standardOutput
self.errorOutput = errorOutput
self.task = task
}
}
}