1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-11-07 05:10:06 +01:00

♻️ Cleanup

This commit is contained in:
2023-01-26 20:29:56 +01:00
parent 66393094b0
commit f60ecb877c
8 changed files with 89 additions and 77 deletions

View File

@@ -28,7 +28,7 @@ class DomainListPhpCell: NSTableCellView, DomainListCellProtocol {
imageViewPhpVersionOK.toolTip = nil
imageViewPhpVersionOK.contentTintColor = site.composerPhpCompatibleWithLinked
imageViewPhpVersionOK.contentTintColor = site.isCompatibleWithPreferredPhpVersion
? NSColor(named: "IconColorGreen")
: NSColor(named: "IconColorRed")
@@ -37,9 +37,9 @@ class DomainListPhpCell: NSTableCellView, DomainListCellProtocol {
imageViewPhpVersionOK.image = NSImage(named: "Isolated")
imageViewPhpVersionOK.toolTip = "domain_list.tooltips.isolated".localized(site.servingPhpVersion)
} else {
imageViewPhpVersionOK.isHidden = (site.composerPhp == "???" || !site.composerPhpCompatibleWithLinked)
imageViewPhpVersionOK.isHidden = (site.preferredPhpVersion == "???" || !site.isCompatibleWithPreferredPhpVersion)
imageViewPhpVersionOK.image = NSImage(named: "Checkmark")
imageViewPhpVersionOK.toolTip = "domain_list.tooltips.checkmark".localized(site.composerPhp)
imageViewPhpVersionOK.toolTip = "domain_list.tooltips.checkmark".localized(site.preferredPhpVersion)
}
}
@@ -58,7 +58,7 @@ class DomainListPhpCell: NSTableCellView, DomainListCellProtocol {
return []
}
return PhpEnv.shared.validVersions(for: site.composerPhp).filter({ version in
return PhpEnv.shared.validVersions(for: site.preferredPhpVersion).filter({ version in
version.short != PhpEnv.phpInstall.version.short
})
}

View File

@@ -25,7 +25,7 @@ class DomainListTypeCell: NSTableCellView, DomainListCellProtocol {
}
// PHP version
labelPhpVersion.stringValue = site.composerPhp == "???" ? "Any PHP" : "PHP \(site.composerPhp)"
labelPhpVersion.stringValue = site.preferredPhpVersion == "???" ? "Any PHP" : "PHP \(site.preferredPhpVersion)"
}
func populateCell(with proxy: ValetProxy) {

View File

@@ -40,7 +40,7 @@ struct ComposerJson: Decodable {
Checks what the PHP version constraint is.
Returns a tuple (constraint, location of constraint).
*/
public func getPhpVersion() -> (String, ValetSite.VersionSource) {
public func getPhpVersion() -> (String, PhpVersionSource) {
// Check if in platform
if configuration?.platform?.php != nil {
return (configuration!.platform!.php!, .platform)

View File

@@ -19,10 +19,17 @@ class FakeValetSite: ValetSite {
constraint: String = "^8.1",
isolated: String? = nil
) {
self.init(name: name, tld: tld, absolutePath: path, aliasPath: nil, makeDeterminations: false)
self.init(
name: name,
tld: tld,
absolutePath: path,
aliasPath: nil,
makeDeterminations: false
)
self.secured = secure
self.composerPhp = constraint
self.composerPhpSource = constraint != "" ? .require : .unknown
self.preferredPhpVersion = constraint
self.preferredPhpVersionSource = constraint != "" ? .require : .unknown
self.driver = driver
self.driverDeterminedByComposer = true

View File

@@ -0,0 +1,17 @@
//
// VersionSource.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 25/01/2023.
// Copyright © 2023 Nico Verbruggen. All rights reserved.
//
import Foundation
enum PhpVersionSource: String {
case unknown
case require
case platform
case valetphprc
case valetrc
}

View File

@@ -44,14 +44,15 @@ class ValetSite: ValetListable {
/// A list of notable Composer dependencies.
var notableComposerDependencies: [String: String] = [:]
/// The PHP version as discovered in `composer.json` or in .valetphprc.
var composerPhp: String = "???"
/// The PHP version as discovered in `composer.json` or in .valetphprc/.valetrc.
/// This is the preferred version needed to correctly run the domain or site.
var preferredPhpVersion: String = "???"
/// Check whether the PHP version is valid for the currently linked version.
var composerPhpCompatibleWithLinked: Bool = false
var isCompatibleWithPreferredPhpVersion: Bool = false
/// How the PHP version was determined.
var composerPhpSource: VersionSource = .unknown
var preferredPhpVersionSource: PhpVersionSource = .unknown
/// Which version of PHP is actually used to serve this site.
var servingPhpVersion: String {
@@ -59,14 +60,6 @@ class ValetSite: ValetListable {
?? PhpEnv.phpInstall.version.short
}
enum VersionSource: String {
case unknown
case require
case platform
case valetphprc
case valetrc
}
init(
name: String,
tld: String,
@@ -139,22 +132,6 @@ class ValetSite: ValetListable {
self.evaluateCompatibility()
}
public func evaluateCompatibility() {
if self.composerPhp == "???" {
return
}
// Split the composer list (on "|") to evaluate multiple constraints
// For example, for Laravel 8 projects the value is "^7.3|^8.0"
self.composerPhpCompatibleWithLinked = self.composerPhp.split(separator: "|")
.map { string in
let origin = self.isolatedPhpVersion?.versionNumber.short ?? PhpEnv.phpInstall.version.long
return !PhpVersionNumberCollection.make(from: [origin])
.matching(constraint: string.trimmingCharacters(in: .whitespacesAndNewlines))
.isEmpty
}.contains(true)
}
/**
Determine the driver to be displayed in the list of sites. In v5.0, this has been changed
to load the "framework" or "project type" instead.
@@ -195,10 +172,14 @@ class ValetSite: ValetListable {
if FileSystem.fileExists(path) {
let decoded = try JSONDecoder().decode(
ComposerJson.self,
from: String(contentsOf: URL(fileURLWithPath: path), encoding: .utf8).data(using: .utf8)!
from: String(
contentsOf: URL(fileURLWithPath: path),
encoding: .utf8
).data(using: .utf8)!
)
(self.composerPhp, self.composerPhpSource) = decoded.getPhpVersion()
(self.preferredPhpVersion,
self.preferredPhpVersionSource) = decoded.getPhpVersion()
self.notableComposerDependencies = decoded.getNotableDependencies()
}
} catch {
@@ -212,8 +193,8 @@ class ValetSite: ValetListable {
*/
private func determineValetPhpFileInfo() {
let files = [
(".valetrc", VersionSource.valetrc),
(".valetphprc", VersionSource.valetphprc)
(".valetrc", PhpVersionSource.valetrc),
(".valetphprc", PhpVersionSource.valetphprc)
]
for (suffix, source) in files {
@@ -231,40 +212,45 @@ class ValetSite: ValetListable {
/**
Parse a Valet file (either .valetphprc or .valetrc).
*/
private func handleValetFile(_ path: String, _ source: VersionSource) throws {
let contents = try String(contentsOf: URL(fileURLWithPath: path), encoding: .utf8)
private func handleValetFile(_ path: String, _ source: PhpVersionSource) throws {
var versionString = ""
switch source {
case .valetphprc:
if let version = VersionExtractor.from(contents) {
self.composerPhp = version
self.composerPhpSource = source
}
versionString = try String(contentsOf: URL(fileURLWithPath: path), encoding: .utf8)
case .valetrc:
self.parseValetRcFile(path, contents)
guard let valetRc = RCFile.fromPath(path) else { return }
guard let phpField = valetRc.fields["PHP"] else { return }
versionString = phpField
default:
return
}
}
/**
Specifically extract PHP information from a .valetrc file.
*/
private func parseValetRcFile(_ path: String, _ text: String) {
let valetRc = RCFile(path: path, contents: text)
guard let versionString = valetRc.fields["PHP"] else {
if valetRc.path != nil {
Log.perf("\(self.name)'s .valetrc file at '\(valetRc.path!)' lacks a 'PHP' entry.")
}
return
}
if let version = VersionExtractor.from(versionString) {
self.composerPhp = version
self.composerPhpSource = .valetrc
self.preferredPhpVersion = version
self.preferredPhpVersionSource = source
}
}
public func evaluateCompatibility() {
if self.preferredPhpVersion == "???" {
return
}
// Split the composer list (on "|") to evaluate multiple constraints
// For example, for Laravel 8 projects the value is "^7.3|^8.0"
self.isCompatibleWithPreferredPhpVersion = self.preferredPhpVersion.split(separator: "|").map { string in
let origin = self.isolatedPhpVersion?.versionNumber.short
?? PhpEnv.phpInstall.version.long
let normalizedPhpVersion = string.trimmingCharacters(in: .whitespacesAndNewlines)
return !PhpVersionNumberCollection.make(from: [origin])
.matching(constraint: normalizedPhpVersion)
.isEmpty
}.contains(true)
}
// MARK: - File Parsing
public static func isolatedVersion(_ filePath: String) -> String? {

View File

@@ -42,14 +42,14 @@ struct VersionPopoverView: View {
}.padding(EdgeInsets(top: 10, leading: 0, bottom: 0, trailing: 0))
}
} else {
if site.composerPhpSource == .unknown {
if site.preferredPhpVersionSource == .unknown {
// We don't know which PHP version is required
DisclaimerView(
iconName: "questionmark.circle.fill",
message: "alert.unable_to_determine_is_fine".localized
)
} else {
if site.composerPhpCompatibleWithLinked {
if site.isCompatibleWithPreferredPhpVersion {
DisclaimerView(
iconName: "checkmark.circle.fill",
message: "alert.php_version_ideal".localized,
@@ -73,7 +73,7 @@ struct VersionPopoverView: View {
}
func getTitleText() -> String {
if site.composerPhpSource == .unknown {
if site.preferredPhpVersionSource == .unknown {
return "alert.composer_php_requirement.unable_to_determine".localized
}
@@ -87,7 +87,7 @@ struct VersionPopoverView: View {
return "alert.composer_php_requirement.title".localized(
"\(site.name).\(suffix)",
site.composerPhp
site.preferredPhpVersion
)
}
@@ -102,7 +102,7 @@ struct VersionPopoverView: View {
information += "\n\n"
}
information += "alert.composer_php_requirement.type.\(site.composerPhpSource.rawValue)"
information += "alert.composer_php_requirement.type.\(site.preferredPhpVersionSource.rawValue)"
.localized
return information