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

️ More efficient extension parsing

This commit is contained in:
2022-05-18 19:23:56 +02:00
parent f9df86851c
commit 0579ebb1c1
5 changed files with 54 additions and 31 deletions

View File

@ -15,9 +15,19 @@ class PhpConfigurationTest: XCTestCase {
}
func testCanLoadExtension() throws {
let iniFile = PhpConfigurationFile.from(filePath: Self.phpIniFileUrl.path)
let iniFile = PhpConfigurationFile.from(filePath: Self.phpIniFileUrl.path)!
XCTAssertNotNil(iniFile)
XCTAssertGreaterThan(iniFile.extensions.count, 0)
}
func testCanSwapConfigurationValue() throws {
let destination = Utility.copyToTemporaryFile(resourceName: "php", fileExtension: "ini")!
let configurationFile = PhpConfigurationFile.from(filePath: destination.path)
XCTAssertNotNil(configurationFile)
}
}

View File

@ -15,13 +15,13 @@ class PhpExtensionTest: XCTestCase {
}
func testCanLoadExtension() throws {
let extensions = PhpExtension.load(from: Self.phpIniFileUrl)
let extensions = PhpExtension.from(filePath: Self.phpIniFileUrl.path)
XCTAssertGreaterThan(extensions.count, 0)
}
func testExtensionNameIsCorrect() throws {
let extensions = PhpExtension.load(from: Self.phpIniFileUrl)
let extensions = PhpExtension.from(filePath: Self.phpIniFileUrl.path)
let extensionNames = extensions.map { (ext) -> String in
return ext.name
@ -40,7 +40,7 @@ class PhpExtensionTest: XCTestCase {
}
func testExtensionStatusIsCorrect() throws {
let extensions = PhpExtension.load(from: Self.phpIniFileUrl)
let extensions = PhpExtension.from(filePath: Self.phpIniFileUrl.path)
// xdebug should be enabled
XCTAssertEqual(extensions[0].enabled, true)
@ -51,7 +51,7 @@ class PhpExtensionTest: XCTestCase {
func testToggleWorksAsExpected() throws {
let destination = Utility.copyToTemporaryFile(resourceName: "php", fileExtension: "ini")!
let extensions = PhpExtension.load(from: destination)
let extensions = PhpExtension.from(filePath: destination.path)
XCTAssertEqual(extensions.count, 6)
// Try to disable xdebug (should be detected first)!
@ -66,7 +66,7 @@ class PhpExtensionTest: XCTestCase {
XCTAssertTrue(file.contains("; zend_extension=\"xdebug.so\""))
// Make sure if we load the data again, it's disabled
XCTAssertEqual(PhpExtension.load(from: destination).first!.enabled, false)
XCTAssertEqual(PhpExtension.from(filePath: destination.path).first!.enabled, false)
}
func testCanRetrieveXdebugMode() throws {

View File

@ -22,6 +22,7 @@ class PhpConfigurationFile: CreatedFromFile {
/// The actual content of the configuration file.
var content: Config
/** Resolves a PHP configuration file (.ini) */
static func from(filePath: String) -> Self? {
let path = filePath.replacingOccurrences(
of: "~",
@ -44,11 +45,24 @@ class PhpConfigurationFile: CreatedFromFile {
required init(path: String, contents: String) {
self.file = path
self.extensions = PhpExtension.load(from: URL(string: path)!)
let lines = contents.components(separatedBy: "\n")
self.content = Self.parseConfig(from: contents.components(separatedBy: "\n"))
self.extensions = PhpExtension.from(lines, filePath: path)
self.content = Self.parseConfig(lines: lines)
}
dump(self)
// MARK: API
public func has(key: String) {
// TODO
}
public func value(for key: String) {
// TODO
}
public func replace(key: String, value: String) {
// TODO
}
// MARK: Parsing Logic
@ -58,7 +72,7 @@ class PhpConfigurationFile: CreatedFromFile {
Attempts to parse the configuration file, based on an array of strings.
Each string is a line from the configuration file.
*/
private static func parseConfig(from lines: [String]) -> Config {
private static func parseConfig(lines: [String]) -> Config {
var config = Config()
var currentSectionName = "main"

View File

@ -89,24 +89,26 @@ class PhpExtension {
// MARK: - Static Methods
/**
This method will attempt to identify all extensions in the .ini file at a certain URL.
*/
static func load(from path: URL) -> [PhpExtension] {
let file = try? String(contentsOf: path, encoding: .utf8)
static func from(_ lines: [String], filePath: String) -> [PhpExtension] {
return lines.filter {
return $0.range(of: Self.extensionRegex, options: .regularExpression) != nil
}.map {
return PhpExtension($0, file: filePath)
}
}
static func from(filePath: String) -> [PhpExtension] {
let file = try? String(contentsOfFile: filePath)
if file == nil {
Log.err("There was an issue reading the file. Assuming no extensions were found.")
return []
}
return file!.components(separatedBy: "\n")
.filter {
return $0.range(of: Self.extensionRegex, options: .regularExpression) != nil
}
.map {
return PhpExtension($0, file: path.path)
}
return Self.from(
file!.components(separatedBy: "\n"),
filePath: filePath
)
}
}

View File

@ -10,15 +10,16 @@ import Foundation
class NginxConfigurationFile: CreatedFromFile {
/** Contents of the Nginx file in question, as a string. */
/// Contents of the Nginx file in question, as a string.
var contents: String!
/** The name of the domain, usually derived from the name of the file. */
/// The name of the domain, usually derived from the name of the file.
var domain: String
/** The TLD of the domain, usually derived from the name of the file. */
/// The TLD of the domain, usually derived from the name of the file.
var tld: String
/** Resolves an nginx configuration file (.conf) */
static func from(filePath: String) -> Self? {
let path = filePath.replacingOccurrences(
of: "~",
@ -47,9 +48,7 @@ class NginxConfigurationFile: CreatedFromFile {
self.tld = tld
}
/**
Retrieves what address this domain is proxying.
*/
/** Retrieves what address this domain is proxying. */
lazy var proxy: String? = {
let regex = try! NSRegularExpression(
pattern: #"proxy_pass (?<proxy>.*:\d*)(\/*);"#,
@ -62,9 +61,7 @@ class NginxConfigurationFile: CreatedFromFile {
return contents[Range(match.range(withName: "proxy"), in: contents)!]
}()
/**
Retrieves which isolated version is active for this domain (if applicable).
*/
/** Retrieves which isolated version is active for this domain (if applicable). */
lazy var isolatedVersion: String? = {
let regex = try! NSRegularExpression(
// PHP versions have (so far) never needed multiple digits for version numbers