1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-07 20:10:08 +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 {
didSet {
self.brewPhpVersion = self.brewPhpPackage!.version
brewPhpVersion = brewPhpPackage!.version
}
}

View File

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

View File

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

View File

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

View File

@ -15,18 +15,18 @@ class StatusMenu : NSMenu {
if App.phpInstall!.version.error {
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
}
let phpVersionText = "\("mi_php_version".localized) \(App.phpInstall!.version.long)"
self.addItem(HeaderView.asMenuItem(text: phpVersionText))
addItem(HeaderView.asMenuItem(text: phpVersionText))
}
func addPhpActionMenuItems() {
if App.busy {
self.addItem(NSMenuItem(title: "mi_busy".localized, action: nil, keyEquivalent: ""))
addItem(NSMenuItem(title: "mi_busy".localized, action: nil, keyEquivalent: ""))
return
}

View File

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

View File

@ -23,41 +23,41 @@ class Paths {
if (optBrewFound) {
// This is usually the case with Homebrew installed on Apple Silicon
self.baseDir = .opt
baseDir = .opt
} else if (usrBrewFound) {
// This is usually the case with Homebrew installed on Intel (or Rosetta 2)
self.baseDir = .usr
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?)")
self.baseDir = .usr
baseDir = .usr
}
print("Homebrew directory: \(self.baseDir)")
print("Homebrew directory: \(baseDir)")
}
// - MARK: Binaries
public static var brew: String {
return "\(self.binPath)/brew"
return "\(binPath)/brew"
}
public static var php: String {
return "\(self.binPath)/php"
return "\(binPath)/php"
}
// - MARK: Paths
public static var binPath: String {
return "\(self.shared.baseDir.rawValue)/bin"
return "\(shared.baseDir.rawValue)/bin"
}
public static var optPath: String {
return "\(self.shared.baseDir.rawValue)/opt"
return "\(shared.baseDir.rawValue)/opt"
}
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))
// 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"
: "Detected older macOS (< 10.15): so defaulting to /bin/bash")
}
@ -47,7 +47,7 @@ class Shell {
*/
func run(_ command: 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)
{
self.failureCallback = failure
failureCallback = failure
self.performEnvironmentCheck(
performEnvironmentCheck(
!Shell.fileExists("\(Paths.binPath)/php"),
messageText: "startup.errors.php_binary.title".localized,
informativeText: "startup.errors.php_binary_desc".localized,
breaking: true
)
self.performEnvironmentCheck(
performEnvironmentCheck(
!Shell.pipe("ls \(Paths.optPath) | grep php").contains("php"),
messageText: "startup.errors.php_opt.title".localized,
informativeText: "startup.errors.php_opt.desc".localized,
breaking: true
)
self.performEnvironmentCheck(
performEnvironmentCheck(
// 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")),
messageText: "startup.errors.valet_executable.title".localized,
@ -45,14 +45,14 @@ class Startup {
breaking: true
)
self.performEnvironmentCheck(
performEnvironmentCheck(
!Shell.pipe("cat /private/etc/sudoers.d/brew").contains("\(Paths.binPath)/brew"),
messageText: "startup.errors.sudoers_brew.title".localized,
informativeText: "startup.errors.sudoers_brew.desc".localized,
breaking: true
)
self.performEnvironmentCheck(
performEnvironmentCheck(
// 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("/opt/homebrew/bin/valet")),
@ -62,15 +62,15 @@ class Startup {
)
let services = Shell.pipe("\(Paths.brew) services list | grep php")
self.performEnvironmentCheck(
performEnvironmentCheck(
(services.countInstances(of: "started") > 1),
messageText: "startup.errors.services.title".localized,
informativeText: "startup.errors.services.desc".localized,
breaking: false
)
if (!self.failed) {
self.determineBrewAliasVersion()
if (!failed) {
determineBrewAliasVersion()
success()
}
}
@ -110,13 +110,13 @@ class Startup {
) {
if (!condition) { return }
self.failed = breaking
failed = breaking
DispatchQueue.main.async {
DispatchQueue.main.async { [self] in
// Present the information to the user
Alert.notify(message: messageText, info: informativeText)
// Only breaking issues will throw the extra retry modal
breaking ? self.failureCallback() : ()
breaking ? failureCallback() : ()
}
}
}