1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-11-09 13:10:24 +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?
}