1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-07 03:50:08 +02:00
Files
app/phpmon/Domain/Integrations/Composer/ComposerJson.swift
Nico Verbruggen c6c3996c7b 🐛 Fixed detection order (#263)
Dictionary key order in Swift isn't a thing, so the process is
now a two-pronged approach: 1) check for specific apps, 2) check for
specific broad frameworks after no other matches were made.

Previously, detection would only work correctly some of the time.

Also cleaned up the `getNotableDependencies()` method.
2023-10-03 20:37:47 +02:00

79 lines
1.9 KiB
Swift

//
// ComposerJson.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 04/01/2022.
// Copyright © 2023 Nico Verbruggen. All rights reserved.
//
import Foundation
/**
This `Decodable` class is used to directly map `composer.json`
to this object.
*/
struct ComposerJson: Decodable {
// MARK: - JSON structure
let dependencies: [String: String]?
let devDependencies: [String: String]?
let configuration: Config?
private enum CodingKeys: String, CodingKey {
case dependencies = "require"
case devDependencies = "require-dev"
case configuration = "config"
}
struct Config: Decodable {
let platform: Platform?
}
struct Platform: Decodable {
let php: String?
}
// MARK: - Helpers
/**
Checks what the PHP version constraint is.
Returns a tuple (constraint, location of constraint).
*/
public func getPhpVersion() -> (String, PhpVersionSource) {
// Check if in platform
if configuration?.platform?.php != nil {
return (configuration!.platform!.php!, .platform)
}
// Check if in dependencies
if dependencies?["php"] != nil {
return (dependencies!["php"]!, .require)
}
// Unknown!
return ("???", .unknown)
}
/**
Checks if any notable dependencies can be resolved.
Only notable dependencies are saved.
*/
public func getNotableDependencies() -> [String: String] {
var notable: [String: String] = [:]
let scan = Array(ProjectTypeDetection.CommonDependencyList.keys) +
Array(ProjectTypeDetection.SpecificDependencyList.keys) +
["php"]
scan.forEach { dependency in
if let resolvedDependency = dependencies?[dependency] {
notable[dependency] = resolvedDependency
}
}
return notable
}
}