1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2026-03-30 08:20:09 +02:00

👌 Avoid self

This commit is contained in:
2021-03-19 16:15:05 +01:00
parent 3cbc2a0367
commit 0cfb7c65bb
9 changed files with 43 additions and 41 deletions

View File

@@ -43,7 +43,7 @@ class App {
*/ */
var brewPhpPackage: HomebrewPackage? = nil { var brewPhpPackage: HomebrewPackage? = nil {
didSet { didSet {
self.brewPhpVersion = self.brewPhpPackage!.version brewPhpVersion = brewPhpPackage!.version
} }
} }

View File

@@ -62,11 +62,11 @@ class PhpExtension {
*/ */
func toggle() { func toggle() {
Actions.sed( Actions.sed(
file: self.file, file: file,
original: self.line, original: line,
replacement: self.enabled ? "; \(self.line)" : self.line.replacingOccurrences(of: "; ", with: "") replacement: enabled ? "; \(line)" : line.replacingOccurrences(of: "; ", with: "")
) )
self.enabled = !self.enabled enabled = !enabled
} }
// MARK: - Static Methods // MARK: - Static Methods

View File

@@ -16,28 +16,29 @@ class PhpInstallation {
// MARK: - Computed // MARK: - Computed
var formula: String { var formula: String {
return (self.version.short == App.shared.brewPhpVersion) ? "php" : "php@\(self.version.short)" return (version.short == App.shared.brewPhpVersion) ? "php" : "php@\(version.short)"
} }
// MARK: - Initializer // MARK: - Initializer
init() { init() {
// Show information about the current version // Show information about the current version
self.version = Self.getVersion() version = Self.getVersion()
// If an error occurred, exit early // If an error occurred, exit early
if (self.version.error) { if (version.error) {
self.configuration = Configuration() configuration = Configuration()
self.extensions = [] extensions = []
return return
} }
// Load extension information // Load extension information
let path = URL(fileURLWithPath: "\(Paths.etcPath)/php/\(self.version.short)/php.ini") let path = URL(fileURLWithPath: "\(Paths.etcPath)/php/\(version.short)/php.ini")
self.extensions = PhpExtension.load(from: path)
extensions = PhpExtension.load(from: path)
// Get configuration values // Get configuration values
self.configuration = Configuration( configuration = Configuration(
memory_limit: Self.getByteCount(key: "memory_limit"), memory_limit: Self.getByteCount(key: "memory_limit"),
upload_max_filesize: Self.getByteCount(key: "upload_max_filesize"), upload_max_filesize: Self.getByteCount(key: "upload_max_filesize"),
post_max_size: Self.getByteCount(key: "post_max_size") post_max_size: Self.getByteCount(key: "post_max_size")

View File

@@ -161,7 +161,7 @@ class MainMenu: NSObject, NSWindowDelegate {
App.shared.currentInstall = PhpInstallation() App.shared.currentInstall = PhpInstallation()
DispatchQueue.main.async { [self] in DispatchQueue.main.async { [self] in
if (App.shared.busy) { if (App.busy) {
setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!) setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!)
} else { } else {
setStatusBarImage(version: App.phpInstall!.version.short) setStatusBarImage(version: App.phpInstall!.version.short)

View File

@@ -15,18 +15,18 @@ class StatusMenu : NSMenu {
if App.phpInstall!.version.error { if App.phpInstall!.version.error {
for message in ["mi_php_broken_1", "mi_php_broken_2", "mi_php_broken_3", "mi_php_broken_4"] { for message in ["mi_php_broken_1", "mi_php_broken_2", "mi_php_broken_3", "mi_php_broken_4"] {
self.addItem(NSMenuItem(title: message.localized, action: nil, keyEquivalent: "")) addItem(NSMenuItem(title: message.localized, action: nil, keyEquivalent: ""))
} }
return return
} }
let phpVersionText = "\("mi_php_version".localized) \(App.phpInstall!.version.long)" let phpVersionText = "\("mi_php_version".localized) \(App.phpInstall!.version.long)"
self.addItem(HeaderView.asMenuItem(text: phpVersionText)) addItem(HeaderView.asMenuItem(text: phpVersionText))
} }
func addPhpActionMenuItems() { func addPhpActionMenuItems() {
if App.busy { if App.busy {
self.addItem(NSMenuItem(title: "mi_busy".localized, action: nil, keyEquivalent: "")) addItem(NSMenuItem(title: "mi_busy".localized, action: nil, keyEquivalent: ""))
return return
} }

View File

@@ -75,6 +75,7 @@ class Actions {
} }
let formula = (version == App.shared.brewPhpVersion) ? "php" : "php@\(version)" let formula = (version == App.shared.brewPhpVersion) ? "php" : "php@\(version)"
brew("link \(formula) --overwrite --force") brew("link \(formula) --overwrite --force")
brew("services start \(formula)", sudo: true) brew("services start \(formula)", sudo: true)
} }
@@ -110,7 +111,7 @@ class Actions {
{ {
brew("services restart dnsmasq", sudo: true) brew("services restart dnsmasq", sudo: true)
self.detectPhpVersions().forEach { (version) in detectPhpVersions().forEach { (version) in
let formula = (version == App.shared.brewPhpVersion) ? "php" : "php@\(version)" let formula = (version == App.shared.brewPhpVersion) ? "php" : "php@\(version)"
brew("unlink php@\(version)") brew("unlink php@\(version)")
brew("services stop \(formula)") brew("services stop \(formula)")

View File

@@ -23,41 +23,41 @@ class Paths {
if (optBrewFound) { if (optBrewFound) {
// This is usually the case with Homebrew installed on Apple Silicon // This is usually the case with Homebrew installed on Apple Silicon
self.baseDir = .opt baseDir = .opt
} else if (usrBrewFound) { } else if (usrBrewFound) {
// This is usually the case with Homebrew installed on Intel (or Rosetta 2) // This is usually the case with Homebrew installed on Intel (or Rosetta 2)
self.baseDir = .usr baseDir = .usr
} else { } else {
// Falling back to default "legacy" Homebrew location (for Intel) // Falling back to default "legacy" Homebrew location (for Intel)
print("Seems like we couldn't determine the Homebrew directory.") print("Seems like we couldn't determine the Homebrew directory.")
print("This usually means we're in trouble... (no Homebrew?)") print("This usually means we're in trouble... (no Homebrew?)")
self.baseDir = .usr baseDir = .usr
} }
print("Homebrew directory: \(self.baseDir)") print("Homebrew directory: \(baseDir)")
} }
// - MARK: Binaries // - MARK: Binaries
public static var brew: String { public static var brew: String {
return "\(self.binPath)/brew" return "\(binPath)/brew"
} }
public static var php: String { public static var php: String {
return "\(self.binPath)/php" return "\(binPath)/php"
} }
// - MARK: Paths // - MARK: Paths
public static var binPath: String { public static var binPath: String {
return "\(self.shared.baseDir.rawValue)/bin" return "\(shared.baseDir.rawValue)/bin"
} }
public static var optPath: String { public static var optPath: String {
return "\(self.shared.baseDir.rawValue)/opt" return "\(shared.baseDir.rawValue)/opt"
} }
public static var etcPath: String { public static var etcPath: String {
return "\(self.shared.baseDir.rawValue)/etc" return "\(shared.baseDir.rawValue)/etc"
} }
} }

View File

@@ -29,7 +29,7 @@ class Shell {
.init(majorVersion: 10, minorVersion: 15, patchVersion: 0)) .init(majorVersion: 10, minorVersion: 15, patchVersion: 0))
// If macOS Mojave is being used, we'll default to /bin/bash // If macOS Mojave is being used, we'll default to /bin/bash
self.shell = at_least_10_15 ? "/bin/sh" : "/bin/bash" shell = at_least_10_15 ? "/bin/sh" : "/bin/bash"
print(at_least_10_15 ? "Detected recent macOS (> 10.15): defaulting to /bin/sh" print(at_least_10_15 ? "Detected recent macOS (> 10.15): defaulting to /bin/sh"
: "Detected older macOS (< 10.15): so defaulting to /bin/bash") : "Detected older macOS (< 10.15): so defaulting to /bin/bash")
} }
@@ -47,7 +47,7 @@ class Shell {
*/ */
func run(_ command: String) { func run(_ command: String) {
// Equivalent of piping to /dev/null; don't do anything with the string // Equivalent of piping to /dev/null; don't do anything with the string
_ = self.pipe(command) _ = pipe(command)
} }
/** /**

View File

@@ -21,23 +21,23 @@ class Startup {
*/ */
func checkEnvironment(success: () -> Void, failure: @escaping () -> Void) func checkEnvironment(success: () -> Void, failure: @escaping () -> Void)
{ {
self.failureCallback = failure failureCallback = failure
self.performEnvironmentCheck( performEnvironmentCheck(
!Shell.fileExists("\(Paths.binPath)/php"), !Shell.fileExists("\(Paths.binPath)/php"),
messageText: "startup.errors.php_binary.title".localized, messageText: "startup.errors.php_binary.title".localized,
informativeText: "startup.errors.php_binary_desc".localized, informativeText: "startup.errors.php_binary_desc".localized,
breaking: true breaking: true
) )
self.performEnvironmentCheck( performEnvironmentCheck(
!Shell.pipe("ls \(Paths.optPath) | grep php").contains("php"), !Shell.pipe("ls \(Paths.optPath) | grep php").contains("php"),
messageText: "startup.errors.php_opt.title".localized, messageText: "startup.errors.php_opt.title".localized,
informativeText: "startup.errors.php_opt.desc".localized, informativeText: "startup.errors.php_opt.desc".localized,
breaking: true breaking: true
) )
self.performEnvironmentCheck( performEnvironmentCheck(
// Older versions of Valet might be located in `/usr/local/bin` regardless of Homebrew prefix // Older versions of Valet might be located in `/usr/local/bin` regardless of Homebrew prefix
!(Shell.fileExists("/usr/local/bin/valet") || Shell.fileExists("/opt/homebrew/bin/valet")), !(Shell.fileExists("/usr/local/bin/valet") || Shell.fileExists("/opt/homebrew/bin/valet")),
messageText: "startup.errors.valet_executable.title".localized, messageText: "startup.errors.valet_executable.title".localized,
@@ -45,14 +45,14 @@ class Startup {
breaking: true breaking: true
) )
self.performEnvironmentCheck( performEnvironmentCheck(
!Shell.pipe("cat /private/etc/sudoers.d/brew").contains("\(Paths.binPath)/brew"), !Shell.pipe("cat /private/etc/sudoers.d/brew").contains("\(Paths.binPath)/brew"),
messageText: "startup.errors.sudoers_brew.title".localized, messageText: "startup.errors.sudoers_brew.title".localized,
informativeText: "startup.errors.sudoers_brew.desc".localized, informativeText: "startup.errors.sudoers_brew.desc".localized,
breaking: true breaking: true
) )
self.performEnvironmentCheck( performEnvironmentCheck(
// Older versions of Valet might be located in `/usr/local/bin` regardless of Homebrew prefix // Older versions of Valet might be located in `/usr/local/bin` regardless of Homebrew prefix
!(Shell.pipe("cat /private/etc/sudoers.d/valet").contains("/usr/local/bin/valet") !(Shell.pipe("cat /private/etc/sudoers.d/valet").contains("/usr/local/bin/valet")
|| Shell.pipe("cat /private/etc/sudoers.d/valet").contains("/opt/homebrew/bin/valet")), || Shell.pipe("cat /private/etc/sudoers.d/valet").contains("/opt/homebrew/bin/valet")),
@@ -62,15 +62,15 @@ class Startup {
) )
let services = Shell.pipe("\(Paths.brew) services list | grep php") let services = Shell.pipe("\(Paths.brew) services list | grep php")
self.performEnvironmentCheck( performEnvironmentCheck(
(services.countInstances(of: "started") > 1), (services.countInstances(of: "started") > 1),
messageText: "startup.errors.services.title".localized, messageText: "startup.errors.services.title".localized,
informativeText: "startup.errors.services.desc".localized, informativeText: "startup.errors.services.desc".localized,
breaking: false breaking: false
) )
if (!self.failed) { if (!failed) {
self.determineBrewAliasVersion() determineBrewAliasVersion()
success() success()
} }
} }
@@ -110,13 +110,13 @@ class Startup {
) { ) {
if (!condition) { return } if (!condition) { return }
self.failed = breaking failed = breaking
DispatchQueue.main.async { DispatchQueue.main.async { [self] in
// Present the information to the user // Present the information to the user
Alert.notify(message: messageText, info: informativeText) Alert.notify(message: messageText, info: informativeText)
// Only breaking issues will throw the extra retry modal // Only breaking issues will throw the extra retry modal
breaking ? self.failureCallback() : () breaking ? failureCallback() : ()
} }
} }
} }