@@ -6,91 +6,76 @@ import SnapshotTestingPlugin
6
6
7
7
/// A singleton class responsible for managing and registering plugins conforming to the `SnapshotTestingPlugin` protocol.
8
8
///
9
- /// The `PluginRegistry` class automatically discovers and registers all classes that conform to the `SnapshotTestingPlugin` protocol
10
- /// within the Objective-C runtime. It provides methods to retrieve specific plugins by their identifier, get all registered plugins,
11
- /// and filter plugins that conform to the `ImageSerialization` protocol.
9
+ /// The `PluginRegistry` automatically discovers and registers classes conforming to the `SnapshotTestingPlugin` protocol
10
+ /// within the Objective-C runtime. It allows retrieval of specific plugins by identifier, access to all registered plugins,
11
+ /// and filtering of plugins that conform to the `ImageSerialization` protocol.
12
12
public class PluginRegistry {
13
- /// The shared instance of the `PluginRegistry`, providing a single point of access.
13
+
14
+ /// Shared singleton instance of `PluginRegistry`.
14
15
private static let shared = PluginRegistry ( )
15
-
16
- /// A dictionary holding the registered plugins, keyed by their identifier.
16
+
17
+ /// Dictionary holding registered plugins, keyed by their identifier.
17
18
private var plugins : [ String : AnyObject ] = [ : ]
18
-
19
- /// Private initializer to enforce the singleton pattern.
19
+
20
+ /// Private initializer enforcing the singleton pattern.
20
21
///
21
- /// Upon initialization, the registry automatically calls `registerAllPlugins ()` to discover and register plugins.
22
+ /// Automatically triggers `automaticPluginRegistration ()` to discover and register plugins.
22
23
private init ( ) {
23
24
defer { automaticPluginRegistration ( ) }
24
25
}
25
26
26
- /// Registers a given plugin in the registry.
27
+ // MARK: - Public Methods
28
+
29
+ /// Registers a plugin.
27
30
///
28
- /// - Parameter plugin: An instance of a class conforming to `SnapshotTestingPlugin`.
31
+ /// - Parameter plugin: An instance conforming to `SnapshotTestingPlugin`.
29
32
public static func registerPlugin( _ plugin: SnapshotTestingPlugin ) {
30
33
PluginRegistry . shared. registerPlugin ( plugin)
31
34
}
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.
35
+
36
+ /// Retrieves a plugin by its identifier, casting it to the specified type.
37
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`.
38
+ /// - Parameter identifier: The unique identifier for the plugin.
39
+ /// - Returns: The plugin instance cast to `Output` if found and castable, otherwise `nil`.
40
40
public static func plugin< Output> ( for identifier: String ) -> Output ? {
41
41
PluginRegistry . shared. plugin ( for: identifier)
42
42
}
43
43
44
- /// Returns all registered plugins that can be cast to the specified type.
44
+ /// Returns all registered plugins cast to the specified type.
45
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`.
46
+ /// - Returns: An array of all registered plugins that can be cast to `Output`.
50
47
public static func allPlugins< Output> ( ) -> [ Output ] {
51
48
PluginRegistry . shared. allPlugins ( )
52
49
}
53
-
54
- // MARK: - Internal Representation
55
- /// Registers a given plugin in the registry.
50
+
51
+ // MARK: - Internal Methods
52
+
53
+ /// Registers a plugin.
56
54
///
57
- /// - Parameter plugin: An instance of a class conforming to `SnapshotTestingPlugin`.
55
+ /// - Parameter plugin: An instance conforming to `SnapshotTestingPlugin`.
58
56
private func registerPlugin( _ plugin: SnapshotTestingPlugin ) {
59
57
plugins [ type ( of: plugin) . identifier] = plugin
60
58
}
61
-
62
- /// Retrieves a plugin from the registry by its identifier and casts it to the specified type.
63
- ///
64
- /// This method attempts to find a plugin in the registry that matches the given identifier and cast it to the specified generic type `Output`.
65
- /// If the plugin exists and can be cast to the specified type, it is returned; otherwise, `nil` is returned.
59
+
60
+ /// Retrieves a plugin by its identifier, casting it to the specified type.
66
61
///
67
- /// - Parameter identifier: A unique string identifier for the plugin.
68
- /// - Returns: The plugin instance cast to the specified type `Output` if found and castable, otherwise `nil`.
62
+ /// - Parameter identifier: The unique identifier for the plugin.
63
+ /// - Returns: The plugin instance cast to `Output` if found and castable, otherwise `nil`.
69
64
private func plugin< Output> ( for identifier: String ) -> Output ? {
70
65
return plugins [ identifier] as? Output
71
66
}
72
67
73
- /// Returns all registered plugins that can be cast to the specified type.
68
+ /// Returns all registered plugins cast to the specified type.
74
69
///
75
- /// This method retrieves all registered plugins and attempts to cast each one to the specified generic type `Output`.
76
- /// Only the plugins that can be successfully cast to `Output` are included in the returned array.
77
- ///
78
- /// - Returns: An array of all registered plugins that can be cast to the specified type `Output`.
70
+ /// - Returns: An array of all registered plugins that can be cast to `Output`.
79
71
private func allPlugins< Output> ( ) -> [ Output ] {
80
72
return Array ( plugins. values. compactMap { $0 as? Output } )
81
73
}
82
-
83
- /// Discovers and registers all classes that conform to the `SnapshotTestingPlugin` protocol.
84
- ///
85
- /// This method iterates over all classes in the Objective-C runtime, identifies those that conform to the `SnapshotTestingPlugin`
86
- /// protocol, and registers them as plugins. The plugins are expected to have a parameterless initializer.
74
+
75
+ /// Discovers and registers all classes conforming to the `SnapshotTestingPlugin` protocol.
87
76
///
88
- /// The process is as follows:
89
- /// 1. The function queries the Objective-C runtime for the total number of classes.
90
- /// 2. Memory is allocated to hold references to these classes.
91
- /// 3. All class references are retrieved into the allocated memory.
92
- /// 4. Each class reference is checked for conformance to the `SnapshotTestingPlugin` protocol.
93
- /// 5. If a class conforms, it is instantiated and registered as a plugin using the `registerPlugin(_:)` method.
77
+ /// This method iterates over all Objective-C runtime classes, identifying those that conform to `SnapshotTestingPlugin`,
78
+ /// instantiating them, and registering them as plugins.
94
79
private func automaticPluginRegistration( ) {
95
80
let classCount = objc_getClassList ( nil , 0 )
96
81
guard classCount > 0 else { return }
@@ -110,6 +95,5 @@ public class PluginRegistry {
110
95
self . registerPlugin ( pluginType. init ( ) )
111
96
}
112
97
}
113
-
114
98
}
115
99
#endif
0 commit comments