mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-08-07 20:10:08 +02:00
⚡️ More efficient extension parsing
This commit is contained in:
@ -15,9 +15,19 @@ class PhpConfigurationTest: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testCanLoadExtension() throws {
|
func testCanLoadExtension() throws {
|
||||||
let iniFile = PhpConfigurationFile.from(filePath: Self.phpIniFileUrl.path)
|
let iniFile = PhpConfigurationFile.from(filePath: Self.phpIniFileUrl.path)!
|
||||||
|
|
||||||
XCTAssertNotNil(iniFile)
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,13 +15,13 @@ class PhpExtensionTest: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testCanLoadExtension() throws {
|
func testCanLoadExtension() throws {
|
||||||
let extensions = PhpExtension.load(from: Self.phpIniFileUrl)
|
let extensions = PhpExtension.from(filePath: Self.phpIniFileUrl.path)
|
||||||
|
|
||||||
XCTAssertGreaterThan(extensions.count, 0)
|
XCTAssertGreaterThan(extensions.count, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testExtensionNameIsCorrect() throws {
|
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
|
let extensionNames = extensions.map { (ext) -> String in
|
||||||
return ext.name
|
return ext.name
|
||||||
@ -40,7 +40,7 @@ class PhpExtensionTest: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testExtensionStatusIsCorrect() throws {
|
func testExtensionStatusIsCorrect() throws {
|
||||||
let extensions = PhpExtension.load(from: Self.phpIniFileUrl)
|
let extensions = PhpExtension.from(filePath: Self.phpIniFileUrl.path)
|
||||||
|
|
||||||
// xdebug should be enabled
|
// xdebug should be enabled
|
||||||
XCTAssertEqual(extensions[0].enabled, true)
|
XCTAssertEqual(extensions[0].enabled, true)
|
||||||
@ -51,7 +51,7 @@ class PhpExtensionTest: XCTestCase {
|
|||||||
|
|
||||||
func testToggleWorksAsExpected() throws {
|
func testToggleWorksAsExpected() throws {
|
||||||
let destination = Utility.copyToTemporaryFile(resourceName: "php", fileExtension: "ini")!
|
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)
|
XCTAssertEqual(extensions.count, 6)
|
||||||
|
|
||||||
// Try to disable xdebug (should be detected first)!
|
// Try to disable xdebug (should be detected first)!
|
||||||
@ -66,7 +66,7 @@ class PhpExtensionTest: XCTestCase {
|
|||||||
XCTAssertTrue(file.contains("; zend_extension=\"xdebug.so\""))
|
XCTAssertTrue(file.contains("; zend_extension=\"xdebug.so\""))
|
||||||
|
|
||||||
// Make sure if we load the data again, it's disabled
|
// 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 {
|
func testCanRetrieveXdebugMode() throws {
|
||||||
|
@ -22,6 +22,7 @@ class PhpConfigurationFile: CreatedFromFile {
|
|||||||
/// The actual content of the configuration file.
|
/// The actual content of the configuration file.
|
||||||
var content: Config
|
var content: Config
|
||||||
|
|
||||||
|
/** Resolves a PHP configuration file (.ini) */
|
||||||
static func from(filePath: String) -> Self? {
|
static func from(filePath: String) -> Self? {
|
||||||
let path = filePath.replacingOccurrences(
|
let path = filePath.replacingOccurrences(
|
||||||
of: "~",
|
of: "~",
|
||||||
@ -44,11 +45,24 @@ class PhpConfigurationFile: CreatedFromFile {
|
|||||||
required init(path: String, contents: String) {
|
required init(path: String, contents: String) {
|
||||||
self.file = path
|
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
|
// MARK: Parsing Logic
|
||||||
@ -58,7 +72,7 @@ class PhpConfigurationFile: CreatedFromFile {
|
|||||||
Attempts to parse the configuration file, based on an array of strings.
|
Attempts to parse the configuration file, based on an array of strings.
|
||||||
Each string is a line from the configuration file.
|
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 config = Config()
|
||||||
|
|
||||||
var currentSectionName = "main"
|
var currentSectionName = "main"
|
||||||
|
@ -89,24 +89,26 @@ class PhpExtension {
|
|||||||
|
|
||||||
// MARK: - Static Methods
|
// MARK: - Static Methods
|
||||||
|
|
||||||
/**
|
static func from(_ lines: [String], filePath: String) -> [PhpExtension] {
|
||||||
This method will attempt to identify all extensions in the .ini file at a certain URL.
|
return lines.filter {
|
||||||
*/
|
return $0.range(of: Self.extensionRegex, options: .regularExpression) != nil
|
||||||
static func load(from path: URL) -> [PhpExtension] {
|
}.map {
|
||||||
let file = try? String(contentsOf: path, encoding: .utf8)
|
return PhpExtension($0, file: filePath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static func from(filePath: String) -> [PhpExtension] {
|
||||||
|
let file = try? String(contentsOfFile: filePath)
|
||||||
|
|
||||||
if file == nil {
|
if file == nil {
|
||||||
Log.err("There was an issue reading the file. Assuming no extensions were found.")
|
Log.err("There was an issue reading the file. Assuming no extensions were found.")
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
return file!.components(separatedBy: "\n")
|
return Self.from(
|
||||||
.filter {
|
file!.components(separatedBy: "\n"),
|
||||||
return $0.range(of: Self.extensionRegex, options: .regularExpression) != nil
|
filePath: filePath
|
||||||
}
|
)
|
||||||
.map {
|
|
||||||
return PhpExtension($0, file: path.path)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -10,15 +10,16 @@ import Foundation
|
|||||||
|
|
||||||
class NginxConfigurationFile: CreatedFromFile {
|
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!
|
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
|
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
|
var tld: String
|
||||||
|
|
||||||
|
/** Resolves an nginx configuration file (.conf) */
|
||||||
static func from(filePath: String) -> Self? {
|
static func from(filePath: String) -> Self? {
|
||||||
let path = filePath.replacingOccurrences(
|
let path = filePath.replacingOccurrences(
|
||||||
of: "~",
|
of: "~",
|
||||||
@ -47,9 +48,7 @@ class NginxConfigurationFile: CreatedFromFile {
|
|||||||
self.tld = tld
|
self.tld = tld
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Retrieves what address this domain is proxying. */
|
||||||
Retrieves what address this domain is proxying.
|
|
||||||
*/
|
|
||||||
lazy var proxy: String? = {
|
lazy var proxy: String? = {
|
||||||
let regex = try! NSRegularExpression(
|
let regex = try! NSRegularExpression(
|
||||||
pattern: #"proxy_pass (?<proxy>.*:\d*)(\/*);"#,
|
pattern: #"proxy_pass (?<proxy>.*:\d*)(\/*);"#,
|
||||||
@ -62,9 +61,7 @@ class NginxConfigurationFile: CreatedFromFile {
|
|||||||
return contents[Range(match.range(withName: "proxy"), in: contents)!]
|
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? = {
|
lazy var isolatedVersion: String? = {
|
||||||
let regex = try! NSRegularExpression(
|
let regex = try! NSRegularExpression(
|
||||||
// PHP versions have (so far) never needed multiple digits for version numbers
|
// PHP versions have (so far) never needed multiple digits for version numbers
|
||||||
|
Reference in New Issue
Block a user