@@ -11,7 +11,7 @@ import SnapshotTestingPlugin
11
11
/// and filter plugins that conform to the `ImageSerialization` protocol.
12
12
public class PluginRegistry {
13
13
/// The shared instance of the `PluginRegistry`, providing a single point of access.
14
- public static let shared = PluginRegistry ( )
14
+ private static let shared = PluginRegistry ( )
15
15
16
16
/// A dictionary holding the registered plugins, keyed by their identifier.
17
17
private var plugins : [ String : AnyObject ] = [ : ]
@@ -20,13 +20,42 @@ public class PluginRegistry {
20
20
///
21
21
/// Upon initialization, the registry automatically calls `registerAllPlugins()` to discover and register plugins.
22
22
private init ( ) {
23
- defer { registerAllPlugins ( ) }
23
+ defer { automaticPluginRegistration ( ) }
24
24
}
25
25
26
26
/// Registers a given plugin in the registry.
27
27
///
28
28
/// - Parameter plugin: An instance of a class conforming to `SnapshotTestingPlugin`.
29
- public func registerPlugin( _ plugin: SnapshotTestingPlugin ) {
29
+ public static func registerPlugin( _ plugin: SnapshotTestingPlugin ) {
30
+ PluginRegistry . shared. registerPlugin ( plugin)
31
+ }
32
+
33
+ /// Retrieves a plugin from the registry by its identifier and casts it to the specified type.
34
+ ///
35
+ /// This method attempts to find a plugin in the registry that matches the given identifier and cast it to the specified generic type `Output`.
36
+ /// If the plugin exists and can be cast to the specified type, it is returned; otherwise, `nil` is returned.
37
+ ///
38
+ /// - Parameter identifier: A unique string identifier for the plugin.
39
+ /// - Returns: The plugin instance cast to the specified type `Output` if found and castable, otherwise `nil`.
40
+ public static func plugin< Output> ( for identifier: String ) -> Output ? {
41
+ PluginRegistry . shared. plugin ( for: identifier)
42
+ }
43
+
44
+ /// Returns all registered plugins that can be cast to the specified type.
45
+ ///
46
+ /// This method retrieves all registered plugins and attempts to cast each one to the specified generic type `Output`.
47
+ /// Only the plugins that can be successfully cast to `Output` are included in the returned array.
48
+ ///
49
+ /// - Returns: An array of all registered plugins that can be cast to the specified type `Output`.
50
+ public static func allPlugins< Output> ( ) -> [ Output ] {
51
+ PluginRegistry . shared. allPlugins ( )
52
+ }
53
+
54
+ // MARK: - Internal Representation
55
+ /// Registers a given plugin in the registry.
56
+ ///
57
+ /// - Parameter plugin: An instance of a class conforming to `SnapshotTestingPlugin`.
58
+ private func registerPlugin( _ plugin: SnapshotTestingPlugin ) {
30
59
plugins [ type ( of: plugin) . identifier] = plugin
31
60
}
32
61
@@ -37,7 +66,7 @@ public class PluginRegistry {
37
66
///
38
67
/// - Parameter identifier: A unique string identifier for the plugin.
39
68
/// - Returns: The plugin instance cast to the specified type `Output` if found and castable, otherwise `nil`.
40
- public func plugin< Output> ( for identifier: String ) -> Output ? {
69
+ private func plugin< Output> ( for identifier: String ) -> Output ? {
41
70
return plugins [ identifier] as? Output
42
71
}
43
72
@@ -47,10 +76,10 @@ public class PluginRegistry {
47
76
/// Only the plugins that can be successfully cast to `Output` are included in the returned array.
48
77
///
49
78
/// - Returns: An array of all registered plugins that can be cast to the specified type `Output`.
50
- public func allPlugins< Output> ( ) -> [ Output ] {
79
+ private func allPlugins< Output> ( ) -> [ Output ] {
51
80
return Array ( plugins. values. compactMap { $0 as? Output } )
52
81
}
53
-
82
+
54
83
/// Discovers and registers all classes that conform to the `SnapshotTestingPlugin` protocol.
55
84
///
56
85
/// This method iterates over all classes in the Objective-C runtime, identifies those that conform to the `SnapshotTestingPlugin`
@@ -62,7 +91,7 @@ public class PluginRegistry {
62
91
/// 3. All class references are retrieved into the allocated memory.
63
92
/// 4. Each class reference is checked for conformance to the `SnapshotTestingPlugin` protocol.
64
93
/// 5. If a class conforms, it is instantiated and registered as a plugin using the `registerPlugin(_:)` method.
65
- func registerAllPlugins ( ) {
94
+ private func automaticPluginRegistration ( ) {
66
95
let classCount = objc_getClassList ( nil , 0 )
67
96
guard classCount > 0 else { return }
68
97
0 commit comments