|
| 1 | +import Foundation |
| 2 | +import SnapshotTestingPlugin |
| 3 | + |
| 4 | +#if canImport(SwiftUI) && canImport(ObjectiveC) |
| 5 | +import ObjectiveC.runtime |
| 6 | +#endif |
| 7 | + |
| 8 | +/// A singleton class responsible for managing and registering plugins conforming to the `SnapshotTestingPlugin` protocol. |
| 9 | +/// |
| 10 | +/// The `PluginRegistry` automatically discovers and registers classes conforming to the `SnapshotTestingPlugin` protocol |
| 11 | +/// within the Objective-C runtime. It allows retrieval of specific plugins by identifier, access to all registered plugins, |
| 12 | +/// and filtering of plugins that conform to the `ImageSerialization` protocol. |
| 13 | +class PluginRegistry { |
| 14 | + |
| 15 | + /// Shared singleton instance of `PluginRegistry`. |
| 16 | + private static let shared = PluginRegistry() |
| 17 | + |
| 18 | + /// Dictionary holding registered plugins, keyed by their identifier. |
| 19 | + private var plugins: [String: AnyObject] = [:] |
| 20 | + |
| 21 | + /// Private initializer enforcing the singleton pattern. |
| 22 | + /// |
| 23 | + /// Automatically triggers `automaticPluginRegistration()` to discover and register plugins. |
| 24 | + private init() { |
| 25 | + defer { automaticPluginRegistration() } |
| 26 | + } |
| 27 | + |
| 28 | + // MARK: - Internal Methods |
| 29 | + |
| 30 | + /// Registers a plugin. |
| 31 | + /// |
| 32 | + /// - Parameter plugin: An instance conforming to `SnapshotTestingPlugin`. |
| 33 | + static func registerPlugin(_ plugin: SnapshotTestingPlugin) { |
| 34 | + PluginRegistry.shared.registerPlugin(plugin) |
| 35 | + } |
| 36 | + |
| 37 | + /// Retrieves a plugin by its identifier, casting it to the specified type. |
| 38 | + /// |
| 39 | + /// - Parameter identifier: The unique identifier for the plugin. |
| 40 | + /// - Returns: The plugin instance cast to `Output` if found and castable, otherwise `nil`. |
| 41 | + static func plugin<Output>(for identifier: String) -> Output? { |
| 42 | + PluginRegistry.shared.plugin(for: identifier) |
| 43 | + } |
| 44 | + |
| 45 | + /// Returns all registered plugins cast to the specified type. |
| 46 | + /// |
| 47 | + /// - Returns: An array of all registered plugins that can be cast to `Output`. |
| 48 | + static func allPlugins<Output>() -> [Output] { |
| 49 | + PluginRegistry.shared.allPlugins() |
| 50 | + } |
| 51 | + |
| 52 | + // MARK: - Internal Methods |
| 53 | + |
| 54 | + /// Registers a plugin. |
| 55 | + /// |
| 56 | + /// - Parameter plugin: An instance conforming to `SnapshotTestingPlugin`. |
| 57 | + private func registerPlugin(_ plugin: SnapshotTestingPlugin) { |
| 58 | + plugins[type(of: plugin).identifier] = plugin |
| 59 | + } |
| 60 | + |
| 61 | + /// Retrieves a plugin by its identifier, casting it to the specified type. |
| 62 | + /// |
| 63 | + /// - Parameter identifier: The unique identifier for the plugin. |
| 64 | + /// - Returns: The plugin instance cast to `Output` if found and castable, otherwise `nil`. |
| 65 | + private func plugin<Output>(for identifier: String) -> Output? { |
| 66 | + return plugins[identifier] as? Output |
| 67 | + } |
| 68 | + |
| 69 | + /// Returns all registered plugins cast to the specified type. |
| 70 | + /// |
| 71 | + /// - Returns: An array of all registered plugins that can be cast to `Output`. |
| 72 | + private func allPlugins<Output>() -> [Output] { |
| 73 | + return Array(plugins.values.compactMap { $0 as? Output }) |
| 74 | + } |
| 75 | + |
| 76 | +#if canImport(SwiftUI) && canImport(ObjectiveC) |
| 77 | + /// Discovers and registers all classes conforming to the `SnapshotTestingPlugin` protocol. |
| 78 | + /// |
| 79 | + /// This method iterates over all Objective-C runtime classes, identifying those that conform to `SnapshotTestingPlugin`, |
| 80 | + /// instantiating them, and registering them as plugins. |
| 81 | + private func automaticPluginRegistration() { |
| 82 | + let classCount = objc_getClassList(nil, 0) |
| 83 | + guard classCount > 0 else { return } |
| 84 | + |
| 85 | + let classes = UnsafeMutablePointer<AnyClass?>.allocate(capacity: Int(classCount)) |
| 86 | + defer { classes.deallocate() } |
| 87 | + |
| 88 | + let autoreleasingClasses = AutoreleasingUnsafeMutablePointer<AnyClass>(classes) |
| 89 | + objc_getClassList(autoreleasingClasses, classCount) |
| 90 | + |
| 91 | + for i in 0..<Int(classCount) { |
| 92 | + guard |
| 93 | + let someClass = classes[i], |
| 94 | + class_conformsToProtocol(someClass, SnapshotTestingPlugin.self), |
| 95 | + let pluginType = someClass as? SnapshotTestingPlugin.Type |
| 96 | + else { continue } |
| 97 | + self.registerPlugin(pluginType.init()) |
| 98 | + } |
| 99 | + } |
| 100 | +#endif |
| 101 | + |
| 102 | + // TEST-ONLY Reset Method |
| 103 | +#if DEBUG |
| 104 | + internal static func reset() { |
| 105 | + shared.plugins.removeAll() |
| 106 | + } |
| 107 | + |
| 108 | + internal static func automaticPluginRegistration() { |
| 109 | + shared.automaticPluginRegistration() |
| 110 | + } |
| 111 | +#endif |
| 112 | +} |
0 commit comments