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

Add site isolation to ValetInteractor

This commit is contained in:
2022-12-14 20:18:06 +01:00
parent 4b04f70638
commit d6554ceea9
6 changed files with 78 additions and 26 deletions

View File

@ -91,7 +91,7 @@
</CommandLineArgument>
<CommandLineArgument
argument = "--configuration:~/.phpmon_fconf_working.json"
isEnabled = "YES">
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "--configuration:~/.phpmon_fconf_broken.json"

View File

@ -18,7 +18,7 @@ class ServicesManager: ObservableObject {
@Published var services: [String: ServiceWrapper] = [:]
init() {
Log.info("Initializing ServicesManager")
Log.info("Initializing ServicesManager...")
formulae = [
Homebrew.Formulae.php,

View File

@ -110,32 +110,41 @@ extension DomainListVC {
}
}
#warning("ValetInteractor needs to be used here instead of directly issuing commands to valet")
@objc func isolateSite(sender: PhpMenuItem) {
let command = "sudo \(Paths.valet) isolate php@\(sender.version) --site '\(self.selectedSite!.name)' && exit;"
guard let site = selectedSite else {
return
}
self.performAction(command: command) {
self.selectedSite!.determineIsolated()
self.selectedSite!.determineComposerPhpVersion()
if self.selectedSite!.isolatedPhpVersion == nil {
BetterAlert()
.withInformation(
title: "domain_list.alerts_isolation_failed.title".localized,
subtitle: "domain_list.alerts_isolation_failed.subtitle".localized,
description: "domain_list.alerts_isolation_failed.desc".localized(command)
)
.withPrimary(text: "OK")
.show()
waitAndExecute {
do {
// Instruct Valet to isolate a given PHP version
try await site.isolate(version: sender.version)
// Reload the UI
self.reloadSelectedRow()
} catch {
// Notify the user about a failed command
let error = error as! ValetInteractionError
self.notifyAboutFailedSiteIsolation(command: error.command)
}
}
}
#warning("ValetInteractor needs to be used here instead of directly issuing commands to valet")
@objc func removeIsolatedSite() {
self.performAction(command: "sudo \(Paths.valet) unisolate --site '\(self.selectedSite!.name)' && exit;") {
self.selectedSite!.isolatedPhpVersion = nil
self.selectedSite!.determineComposerPhpVersion()
guard let site = selectedSite else {
return
}
waitAndExecute {
do {
// Instruct Valet to remove isolation
try await site.unisolate()
// Reload the UI
self.reloadSelectedRow()
} catch {
// Notify the user about a failed command
let error = error as! ValetInteractionError
self.notifyAboutFailedSiteIsolation(command: error.command)
}
}
}
@ -214,4 +223,15 @@ extension DomainListVC {
.withPrimary(text: "OK")
.show()
}
private func notifyAboutFailedSiteIsolation(command: String) {
BetterAlert()
.withInformation(
title: "domain_list.alerts_isolation_failed.title".localized,
subtitle: "domain_list.alerts_isolation_failed.subtitle".localized,
description: "domain_list.alerts_isolation_failed.desc".localized(command)
)
.withPrimary(text: "OK")
.show()
}
}

View File

@ -69,8 +69,36 @@ class ValetInteractor {
await Actions.restartNginx()
}
public func isolate(site: ValetSite, version: PhpVersionNumber) async throws {
// TODO
public func isolate(site: ValetSite, version: String) async throws {
let command = "sudo \(Paths.valet) isolate php@\(version) --site '\(site.name)'"
// Run the command
await Shell.quiet(command)
// Check if the secured status has actually changed
site.determineIsolated()
site.determineComposerPhpVersion()
// If the version is not isolated, this failed
if site.isolatedPhpVersion == nil {
throw ValetInteractionError(command: command)
}
}
public func unisolate(site: ValetSite) async throws {
let command = "sudo \(Paths.valet) unisolate --site '\(site.name)'"
// Run the command
await Shell.quiet(command)
// Check if the secured status has actually changed
site.determineIsolated()
site.determineComposerPhpVersion()
// If the version is somehow still isolated, this failed
if site.isolatedPhpVersion != nil {
throw ValetInteractionError(command: command)
}
}
public func unlink(site: ValetSite) async throws {

View File

@ -269,10 +269,14 @@ class ValetSite: ValetListable {
try await ValetInteractor.shared.toggleSecure(site: self)
}
func isolate(version: PhpVersionNumber) async throws {
func isolate(version: String) async throws {
try await ValetInteractor.shared.isolate(site: self, version: version)
}
func unisolate() async throws {
try await ValetInteractor.shared.unisolate(site: self)
}
func unlink() async {
try! await ValetInteractor.shared.unlink(site: self)
}

View File

@ -112,8 +112,8 @@
"domain_list.tooltips.isolated" = "This domain is isolated and using PHP %@ instead of the globally linked PHP.";
"domain_list.tooltips.checkmark" = "This domain is being served with a version of PHP that is compatible with this requirement (PHP %@). Click on the PHP version next to this checkmark to find out more information about how this requirement was determined.";
"domain_list.alerts_isolation_failed.title" = "Oops! Site Not Isolated";
"domain_list.alerts_isolation_failed.subtitle" = "Something went wrong trying to isolate this site. If this is your default site but it is not linked, I recommend manually linking the site prior to setting up isolation.";
"domain_list.alerts_isolation_failed.title" = "Oops! Site Isolation Not Applied";
"domain_list.alerts_isolation_failed.subtitle" = "Something went wrong trying to change the isolation status for this site. If this is your default site but it is not linked, I recommend manually linking the site prior to setting up isolation.";
"domain_list.alerts_isolation_failed.desc" = "To find out what goes wrong, you can try running the command in your terminal manually: %@";
"domain_list.alerts_status_not_changed.title" = "Oops! SSL Status Not Changed";