Plugin is a Swift SPM project that provides a flexible and modular approach to enhancing object functionality through the use of plugins. By conforming to the Plugin protocol, custom plugins can be created and added to objects that conform to the Pluginable protocol, without having to modify their original implementation. This allows developers the ability to add custom behaviors to their objects in a plug-and-play manner, making it easy to maintain and extend code over time.
Usage
Creating a plugin
To create a plugin, create a struct or class that conforms to the Plugin protocol and implement its requirements.
Here’s an example of a simple plugin that adds an auth header to a URLRequest object:
To use plugins with an object, make it conform to the Pluginable protocol. Here’s an example of a PluginURLRequest class that can have plugins added to it:
class PluginURLRequest: Pluginable {
var plugins: [any Plugin] = []
var request: URLRequest
init(url: URL) {
self.request = URLRequest(url: url)
}
}
Registering and handling plugins
Plugins can be registered to a Pluginable object using the register(plugin: any Plugin) method. Once registered, the handle(value: Any) method can be called on the object to apply the registered plugins to it.
Scribe: Scribe is a flexible logging library for Swift, designed to make logging easy and efficient. It provides a centralized system for logging messages and events within your application, and supports multiple logging outputs and plugins to extend its capabilities to meet your needs. Scribe integrates with swift-log for console logging, making it a versatile solution for all your logging requirements.
PluginTask: PluginTask is a custom Task that allows users to add plugins to modify its behavior. Plugins can be added to the PluginTask instance to perform additional functionality before or after the task’s main operation. This makes it easy to modify the task’s behavior without modifying its original implementation.
Plugin
🔌 Plug and Play
Plugin is a Swift SPM project that provides a flexible and modular approach to enhancing object functionality through the use of plugins. By conforming to the
Plugin
protocol, custom plugins can be created and added to objects that conform to thePluginable
protocol, without having to modify their original implementation. This allows developers the ability to add custom behaviors to their objects in a plug-and-play manner, making it easy to maintain and extend code over time.Usage
Creating a plugin
To create a plugin, create a struct or class that conforms to the Plugin protocol and implement its requirements.
Here’s an example of a simple plugin that adds an
auth
header to aURLRequest
object:Making an object pluginable
To use plugins with an object, make it conform to the
Pluginable
protocol. Here’s an example of aPluginURLRequest
class that can have plugins added to it:Registering and handling plugins
Plugins can be registered to a
Pluginable
object using theregister(plugin: any Plugin)
method. Once registered, thehandle(value: Any)
method can be called on the object to apply the registered plugins to it.Additional Features
You can also access the
inputType
andoutputType
properties of any plugin and get the list of plugins with theirinputType
andoutputType
properties.Projects Using Plugins
Scribe: Scribe is a flexible logging library for Swift, designed to make logging easy and efficient. It provides a centralized system for logging messages and events within your application, and supports multiple logging outputs and plugins to extend its capabilities to meet your needs. Scribe integrates with swift-log for console logging, making it a versatile solution for all your logging requirements.
PluginTask: PluginTask is a custom Task that allows users to add plugins to modify its behavior. Plugins can be added to the PluginTask instance to perform additional functionality before or after the task’s main operation. This makes it easy to modify the task’s behavior without modifying its original implementation.