From 0579ebb1c1d890f96c2bcc7d52ba0521cb24c1dd Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Wed, 18 May 2022 19:23:56 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20More=20efficient=20extensi?= =?UTF-8?q?on=20parsing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Parsers/PhpConfigurationTest.swift | 12 ++++++++- phpmon-tests/Parsers/PhpExtensionTest.swift | 10 +++---- phpmon/Common/PHP/PhpConfigurationFile.swift | 22 +++++++++++++--- phpmon/Common/PHP/PhpExtension.swift | 26 ++++++++++--------- .../Nginx/NginxConfigurationFile.swift | 15 +++++------ 5 files changed, 54 insertions(+), 31 deletions(-) diff --git a/phpmon-tests/Parsers/PhpConfigurationTest.swift b/phpmon-tests/Parsers/PhpConfigurationTest.swift index c939778..de26703 100644 --- a/phpmon-tests/Parsers/PhpConfigurationTest.swift +++ b/phpmon-tests/Parsers/PhpConfigurationTest.swift @@ -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) } } diff --git a/phpmon-tests/Parsers/PhpExtensionTest.swift b/phpmon-tests/Parsers/PhpExtensionTest.swift index 375760b..856cfbf 100644 --- a/phpmon-tests/Parsers/PhpExtensionTest.swift +++ b/phpmon-tests/Parsers/PhpExtensionTest.swift @@ -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 { diff --git a/phpmon/Common/PHP/PhpConfigurationFile.swift b/phpmon/Common/PHP/PhpConfigurationFile.swift index cd59c42..8642341 100644 --- a/phpmon/Common/PHP/PhpConfigurationFile.swift +++ b/phpmon/Common/PHP/PhpConfigurationFile.swift @@ -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" diff --git a/phpmon/Common/PHP/PhpExtension.swift b/phpmon/Common/PHP/PhpExtension.swift index c6d86d7..84a63a9 100644 --- a/phpmon/Common/PHP/PhpExtension.swift +++ b/phpmon/Common/PHP/PhpExtension.swift @@ -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 + ) } } diff --git a/phpmon/Domain/Integrations/Nginx/NginxConfigurationFile.swift b/phpmon/Domain/Integrations/Nginx/NginxConfigurationFile.swift index 85846c3..c8dade6 100644 --- a/phpmon/Domain/Integrations/Nginx/NginxConfigurationFile.swift +++ b/phpmon/Domain/Integrations/Nginx/NginxConfigurationFile.swift @@ -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 (?.*:\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