From 48b4f9b160cc264e638908aceb0d1747769fdaa9 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Mon, 14 Mar 2022 19:13:38 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=20Added=20initial=20TODO=20items?= =?UTF-8?q?=20for=20#148?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PHP/Switcher/InternalSwitcher.swift | 2 + phpmon/Domain/Integrations/Valet/Valet.swift | 6 ++- .../Domain/Integrations/Valet/ValetSite.swift | 41 ++++++++++++------- phpmon/Domain/SiteList/SiteListCell.swift | 4 +- .../Domain/SiteList/SiteListVC+Actions.swift | 16 ++++---- 5 files changed, 44 insertions(+), 25 deletions(-) diff --git a/phpmon/Common/PHP/Switcher/InternalSwitcher.swift b/phpmon/Common/PHP/Switcher/InternalSwitcher.swift index 4c313ad..14f8096 100644 --- a/phpmon/Common/PHP/Switcher/InternalSwitcher.swift +++ b/phpmon/Common/PHP/Switcher/InternalSwitcher.swift @@ -34,6 +34,8 @@ class InternalSwitcher: PhpSwitcher { ? "php" : "php@\(available)" brew("unlink \(formula)") + + // TODO: (ISOLATION) Only stop formulae that are not used for isolation brew("services stop \(formula)", sudo: true) Log.perf("Unlinked and stopped services for \(formula)") diff --git a/phpmon/Domain/Integrations/Valet/Valet.swift b/phpmon/Domain/Integrations/Valet/Valet.swift index 1315029..ab561a6 100644 --- a/phpmon/Domain/Integrations/Valet/Valet.swift +++ b/phpmon/Domain/Integrations/Valet/Valet.swift @@ -41,7 +41,7 @@ class Valet { let file = FileManager.default.homeDirectoryForCurrentUser .appendingPathComponent(".config/valet/config.json") - // TODO: (5.1) Fix loading of invalid JSON: do not crash the app + // TODO: (5.2) Fix loading of invalid JSON: do not crash the app config = try! JSONDecoder().decode( Valet.Configuration.self, from: try! String(contentsOf: file, encoding: .utf8).data(using: .utf8)! @@ -83,6 +83,10 @@ class Valet { installed is not recent enough. */ public func validateVersion() -> Void { + if version.versionCompare("3.0") == .orderedAscending { + Log.warn("This version of Valet does not support isolation yet. Disabling isolation checks.") + } + if version.versionCompare(Constants.MinimumRecommendedValetVersion) == .orderedAscending { let version = version Log.warn("Valet version \(version!) is too old! (recommended: \(Constants.MinimumRecommendedValetVersion))") diff --git a/phpmon/Domain/Integrations/Valet/ValetSite.swift b/phpmon/Domain/Integrations/Valet/ValetSite.swift index 5e8be82..a4abf76 100644 --- a/phpmon/Domain/Integrations/Valet/ValetSite.swift +++ b/phpmon/Domain/Integrations/Valet/ValetSite.swift @@ -11,10 +11,10 @@ import Foundation class ValetSite { /// Name of the site. Does not include the TLD. - var name: String! + var name: String /// The absolute path to the directory that is served. - var absolutePath: String! + var absolutePath: String /// The absolute path to the directory that is served, /// replacing the user's home folder with ~. @@ -23,6 +23,12 @@ class ValetSite { .replacingOccurrences(of: "/Users/\(Paths.whoami)", with: "~") }() + /// The TLD used to locate this site. + var tld: String = "test" + + /// The PHP version that is being used to serve this site specifically (if not global). + var isolatedPhpVersion: PhpInstallation? + /// Location of the alias. If set, this is a linked domain. var aliasPath: String? @@ -54,34 +60,41 @@ class ValetSite { case valetphprc = "valetphprc" } - init() {} - - convenience init(absolutePath: String, tld: String) { - self.init() + init(absolutePath: String, tld: String) { self.absolutePath = absolutePath + self.tld = tld self.name = URL(fileURLWithPath: absolutePath).lastPathComponent self.aliasPath = nil - determineSecured(tld) + + determineSecured() determineComposerPhpVersion() determineDriver() } convenience init(aliasPath: String, tld: String) { - self.init() - self.absolutePath = try! FileManager.default.destinationOfSymbolicLink(atPath: aliasPath) + let absolutePath = try! FileManager.default.destinationOfSymbolicLink(atPath: aliasPath) + + self.init(absolutePath: absolutePath, tld: tld) self.name = URL(fileURLWithPath: aliasPath).lastPathComponent self.aliasPath = aliasPath - determineSecured(tld) + + determineSecured() determineComposerPhpVersion() determineDriver() } + public func determineIsolated() { + // TODO: (ISOLATION) Determine whether the domain is isolated by checking for `# Valet isolated PHP version` + // This needs to be checked in: "~/.config/valet/Nginx/\(self.name).\(self.tld)" + self.isolatedPhpVersion = nil + } + /** Checks if a certificate file can be found in the `valet/Certificates` directory. - Note: The file is not validated, only its presence is checked. */ - public func determineSecured(_ tld: String) { - secured = Filesystem.fileExists("~/.config/valet/Certificates/\(self.name!).\(tld).key") + public func determineSecured() { + secured = Filesystem.fileExists("~/.config/valet/Certificates/\(self.name).\(self.tld).key") } /** @@ -147,7 +160,7 @@ class ValetSite { as well as the requested PHP version. If no composer.json file is found, nothing happens. */ private func determineComposerInformation() { - let path = "\(absolutePath!)/composer.json" + let path = "\(absolutePath)/composer.json" do { if Filesystem.fileExists(path) { @@ -168,7 +181,7 @@ class ValetSite { Checks the contents of the .valetphprc file and determine the version, if possible. */ private func determineValetPhpFileInfo() { - let path = "\(absolutePath!)/.valetphprc" + let path = "\(absolutePath)/.valetphprc" do { if Filesystem.fileExists(path) { diff --git a/phpmon/Domain/SiteList/SiteListCell.swift b/phpmon/Domain/SiteList/SiteListCell.swift index 824ca24..4840fa5 100644 --- a/phpmon/Domain/SiteList/SiteListCell.swift +++ b/phpmon/Domain/SiteList/SiteListCell.swift @@ -33,7 +33,7 @@ class SiteListCell: NSTableCellView self.site = site // Make sure to show the TLD - labelSiteName.stringValue = "\(site.name!).\(Valet.shared.config.tld)" + labelSiteName.stringValue = "\(site.name).\(Valet.shared.config.tld)" // Show the absolute path, except make sure to replace the /Users/username segment with ~ for readability labelPathName.stringValue = site.absolutePathRelative @@ -79,7 +79,7 @@ class SiteListCell: NSTableCellView alert.alertStyle = .informational alert.messageText = "alert.composer_php_requirement.title" - .localized("\(site.name!).\(Valet.shared.config.tld)", site.composerPhp) + .localized("\(site.name).\(Valet.shared.config.tld)", site.composerPhp) alert.informativeText = "alert.composer_php_requirement.type.\(site.composerPhpSource.rawValue)" .localized diff --git a/phpmon/Domain/SiteList/SiteListVC+Actions.swift b/phpmon/Domain/SiteList/SiteListVC+Actions.swift index d36a86b..7842387 100644 --- a/phpmon/Domain/SiteList/SiteListVC+Actions.swift +++ b/phpmon/Domain/SiteList/SiteListVC+Actions.swift @@ -16,12 +16,12 @@ extension SiteListVC { let originalSecureStatus = selectedSite!.secured let action = selectedSite!.secured ? "unsecure" : "secure" let selectedSite = selectedSite! - let command = "cd '\(selectedSite.absolutePath!)' && sudo \(Paths.valet) \(action) && exit;" + let command = "cd '\(selectedSite.absolutePath)' && sudo \(Paths.valet) \(action) && exit;" waitAndExecute { Shell.run(command, requiresPath: true) } completion: { [self] in - selectedSite.determineSecured(Valet.shared.config.tld) + selectedSite.determineSecured() if selectedSite.secured == originalSecureStatus { BetterAlert() .withInformation( @@ -36,7 +36,7 @@ extension SiteListVC { title: "site_list.alerts_status_changed.title".localized, subtitle: "site_list.alerts_status_changed.desc" .localized( - "\(selectedSite.name!).\(Valet.shared.config.tld)", + "\(selectedSite.name).\(Valet.shared.config.tld)", newState ) ) @@ -50,7 +50,7 @@ extension SiteListVC { @objc func openInBrowser() { let prefix = selectedSite!.secured ? "https://" : "http://" - let url = URL(string: "\(prefix)\(selectedSite!.name!).\(Valet.shared.config.tld)") + let url = URL(string: "\(prefix)\(selectedSite!.name).\(Valet.shared.config.tld)") if url != nil { NSWorkspace.shared.open(url!) } else { @@ -65,16 +65,16 @@ extension SiteListVC { } @objc func openInFinder() { - Shell.run("open '\(selectedSite!.absolutePath!)'") + Shell.run("open '\(selectedSite!.absolutePath)'") } @objc func openInTerminal() { - Shell.run("open -b com.apple.terminal '\(selectedSite!.absolutePath!)'") + Shell.run("open -b com.apple.terminal '\(selectedSite!.absolutePath)'") } @objc func openWithEditor(sender: EditorMenuItem) { guard let editor = sender.editor else { return } - editor.openDirectory(file: selectedSite!.absolutePath!) + editor.openDirectory(file: selectedSite!.absolutePath) } @objc func unlinkSite() { @@ -94,7 +94,7 @@ extension SiteListVC { secondButtonTitle: "Cancel", style: .critical, onFirstButtonPressed: { - Shell.run("valet unlink '\(site.name!)'", requiresPath: true) + Shell.run("valet unlink '\(site.name)'", requiresPath: true) self.reloadSites() } )