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

👌 Code cleanup

This commit is contained in:
2022-05-15 15:15:49 +02:00
parent 1392b6e4a0
commit b0c62e226a
10 changed files with 87 additions and 44 deletions

View File

@@ -52,11 +52,9 @@ class ActivePhpInstallation {
// Load extension information
let mainConfigurationFileUrl = URL(fileURLWithPath: "\(Paths.etcPath)/php/\(version.short)/php.ini")
iniFiles.append(
PhpConfigurationFile(fileUrl: mainConfigurationFileUrl)
)
// extensions.append(contentsOf: PhpExtension.load(from: mainConfigurationFileUrl))
if let file = PhpConfigurationFile.from(filePath: mainConfigurationFileUrl.path) {
iniFiles.append(file)
}
// Get configuration values
limits = Limits(
@@ -73,11 +71,9 @@ class ActivePhpInstallation {
// See if any extensions are present in said .ini files
paths.forEach { (iniFilePath) in
let fileUrl = URL(fileURLWithPath: iniFilePath)
iniFiles.append(
PhpConfigurationFile(fileUrl: fileUrl)
)
if let file = PhpConfigurationFile.from(filePath: iniFilePath) {
iniFiles.append(file)
}
}
}

View File

@@ -8,7 +8,7 @@
import Foundation
class PhpConfigurationFile {
class PhpConfigurationFile: CreatedFromFile {
typealias Section = [String: String]
typealias Config = [String: Section]
@@ -22,14 +22,31 @@ class PhpConfigurationFile {
/// The actual content of the configuration file.
var content: Config
init(fileUrl: URL) {
self.file = fileUrl.path
static func from(filePath: String) -> Self? {
let path = filePath.replacingOccurrences(
of: "~",
with: "/Users/\(Paths.whoami)"
)
let rawString = (try? String(contentsOf: fileUrl, encoding: .utf8)) ?? ""
do {
let fileContents = try String(contentsOfFile: path)
self.extensions = PhpExtension.load(from: fileUrl)
return Self.init(
path: path,
contents: fileContents
)
} catch {
Log.warn("Could not read the PHP configuration file at: `\(filePath)`")
return nil
}
}
self.content = Self.parseConfig(from: rawString.components(separatedBy: "\n"))
required init(path: String, contents: String) {
self.file = path
self.extensions = PhpExtension.load(from: URL(string: path)!)
self.content = Self.parseConfig(from: contents.components(separatedBy: "\n"))
dump(self)
}

View File

@@ -0,0 +1,15 @@
//
// CreatedFromFile.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 15/05/2022.
// Copyright © 2022 Nico Verbruggen. All rights reserved.
//
import Foundation
protocol CreatedFromFile {
static func from(filePath: String) -> Self?
}

View File

@@ -1,5 +1,5 @@
//
// NginxConfiguration.swift
// NginxConfigurationFile.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 15/03/2022.
@@ -8,7 +8,7 @@
import Foundation
class NginxConfiguration {
class NginxConfigurationFile: CreatedFromFile {
/** Contents of the Nginx file in question, as a string. */
var contents: String!
@@ -19,7 +19,7 @@ class NginxConfiguration {
/** The TLD of the domain, usually derived from the name of the file. */
var tld: String
static func from(filePath: String) -> NginxConfiguration? {
static func from(filePath: String) -> Self? {
let path = filePath.replacingOccurrences(
of: "~",
with: "/Users/\(Paths.whoami)"
@@ -27,7 +27,8 @@ class NginxConfiguration {
do {
let fileContents = try String(contentsOfFile: path)
return NginxConfiguration.init(
return Self.init(
path: path,
contents: fileContents
)
@@ -37,7 +38,7 @@ class NginxConfiguration {
}
}
init(path: String, contents: String) {
required init(path: String, contents: String) {
let domain = String(path.split(separator: "/").last!)
let tld = String(domain.split(separator: ".").last!)

View File

@@ -14,7 +14,7 @@ class ValetProxyScanner: ProxyScanner {
.default
.contentsOfDirectory(atPath: directoryPath)
.compactMap {
return NginxConfiguration.from(filePath: "\(directoryPath)/\($0)")
return NginxConfigurationFile.from(filePath: "\(directoryPath)/\($0)")
}
.filter {
return $0.proxy != nil

View File

@@ -14,7 +14,7 @@ class ValetProxy: DomainListable {
var target: String
var secured: Bool = false
init(_ configuration: NginxConfiguration) {
init(_ configuration: NginxConfigurationFile) {
self.domain = configuration.domain
self.tld = configuration.tld
self.target = configuration.proxy!

View File

@@ -225,7 +225,7 @@ class ValetSite: DomainListable {
public static func isolatedVersion(_ filePath: String) -> String? {
if Filesystem.fileExists(filePath) {
return NginxConfiguration
return NginxConfigurationFile
.from(filePath: filePath)?
.isolatedVersion ?? nil
}