Skip to content

Commit 6b149c5

Browse files
committed
feat: update documentation
1 parent a7f218c commit 6b149c5

File tree

1 file changed

+35
-51
lines changed

1 file changed

+35
-51
lines changed

Sources/SnapshotTesting/Plugins/PluginRegistry.swift

+35-51
Original file line numberDiff line numberDiff line change
@@ -6,91 +6,76 @@ import SnapshotTestingPlugin
66

77
/// A singleton class responsible for managing and registering plugins conforming to the `SnapshotTestingPlugin` protocol.
88
///
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.
1212
public class PluginRegistry {
13-
/// The shared instance of the `PluginRegistry`, providing a single point of access.
13+
14+
/// Shared singleton instance of `PluginRegistry`.
1415
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.
1718
private var plugins: [String: AnyObject] = [:]
18-
19-
/// Private initializer to enforce the singleton pattern.
19+
20+
/// Private initializer enforcing the singleton pattern.
2021
///
21-
/// Upon initialization, the registry automatically calls `registerAllPlugins()` to discover and register plugins.
22+
/// Automatically triggers `automaticPluginRegistration()` to discover and register plugins.
2223
private init() {
2324
defer { automaticPluginRegistration() }
2425
}
2526

26-
/// Registers a given plugin in the registry.
27+
// MARK: - Public Methods
28+
29+
/// Registers a plugin.
2730
///
28-
/// - Parameter plugin: An instance of a class conforming to `SnapshotTestingPlugin`.
31+
/// - Parameter plugin: An instance conforming to `SnapshotTestingPlugin`.
2932
public static func registerPlugin(_ plugin: SnapshotTestingPlugin) {
3033
PluginRegistry.shared.registerPlugin(plugin)
3134
}
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.
3737
///
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`.
4040
public static func plugin<Output>(for identifier: String) -> Output? {
4141
PluginRegistry.shared.plugin(for: identifier)
4242
}
4343

44-
/// Returns all registered plugins that can be cast to the specified type.
44+
/// Returns all registered plugins cast to the specified type.
4545
///
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`.
5047
public static func allPlugins<Output>() -> [Output] {
5148
PluginRegistry.shared.allPlugins()
5249
}
53-
54-
// MARK: - Internal Representation
55-
/// Registers a given plugin in the registry.
50+
51+
// MARK: - Internal Methods
52+
53+
/// Registers a plugin.
5654
///
57-
/// - Parameter plugin: An instance of a class conforming to `SnapshotTestingPlugin`.
55+
/// - Parameter plugin: An instance conforming to `SnapshotTestingPlugin`.
5856
private func registerPlugin(_ plugin: SnapshotTestingPlugin) {
5957
plugins[type(of: plugin).identifier] = plugin
6058
}
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.
6661
///
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`.
6964
private func plugin<Output>(for identifier: String) -> Output? {
7065
return plugins[identifier] as? Output
7166
}
7267

73-
/// Returns all registered plugins that can be cast to the specified type.
68+
/// Returns all registered plugins cast to the specified type.
7469
///
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`.
7971
private func allPlugins<Output>() -> [Output] {
8072
return Array(plugins.values.compactMap { $0 as? Output })
8173
}
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.
8776
///
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.
9479
private func automaticPluginRegistration() {
9580
let classCount = objc_getClassList(nil, 0)
9681
guard classCount > 0 else { return }
@@ -110,6 +95,5 @@ public class PluginRegistry {
11095
self.registerPlugin(pluginType.init())
11196
}
11297
}
113-
11498
}
11599
#endif

0 commit comments

Comments
 (0)