Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from vapor/dep-flag-config
Dep flag config
- Loading branch information
Showing
8 changed files
with
226 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import Service | ||
|
||
/// Configures commands for a service container. | ||
/// | ||
/// var commandConfig = CommandConfig.default() | ||
/// /// You can register command types that will be lazily created | ||
/// commandConfig.use(FooCommand.self, as: "foo") | ||
/// /// You can also register pre-initialized instances of a command | ||
/// commandConfig.use(barCommand, as: "bar") | ||
/// services.register(commandConfig) | ||
/// | ||
public struct CommandConfig: Service { | ||
/// A lazily initialized `CommandRunnable`. | ||
public typealias LazyCommand = (Container) throws -> CommandRunnable | ||
|
||
/// Internal storage | ||
private var commands: [String: LazyCommand] | ||
|
||
/// The default runnable | ||
private var defaultCommand: LazyCommand? | ||
|
||
/// Create a new `CommandConfig`. | ||
public init() { | ||
self.commands = [:] | ||
} | ||
|
||
/// Adds a `Command` instance to the config. | ||
/// | ||
/// var commandConfig = CommandConfig.default() | ||
/// commandConfig.use(barCommand, as: "bar") | ||
/// services.register(commandConfig) | ||
/// | ||
/// - parameters: | ||
/// - command: Some `CommandRunnable`. This type will be requested from the service container later. | ||
/// - name: A unique name for running this command. | ||
/// - isDefault: If `true`, this command will be set as the default command to run when none other are specified. | ||
/// Setting this overrides any previous default commands. | ||
public mutating func use(_ command: CommandRunnable, as name: String, isDefault: Bool = false) { | ||
commands[name] = { _ in command } | ||
if isDefault { | ||
defaultCommand = { _ in command } | ||
} | ||
} | ||
|
||
/// Adds a `CommandRunnable` type to the config. This type will be lazily initialized later using a `Container`. | ||
/// | ||
/// var commandConfig = CommandConfig.default() | ||
/// commandConfig.use(FooCommand.self, as: "foo") | ||
/// services.register(commandConfig) | ||
/// | ||
/// - parameters: | ||
/// - command: `Type` of some `Command`. This type will be requested from the service container later. | ||
/// - name: A unique name for running this command. | ||
/// - isDefault: If `true`, this command will be set as the default command to run when none other are specified. | ||
/// Setting this overrides any previous default commands. | ||
public mutating func use<R>(_ command: R.Type, as name: String, isDefault: Bool = false) where R: CommandRunnable { | ||
commands[name] = { try $0.make(R.self) } | ||
if isDefault { | ||
defaultCommand = { try $0.make(R.self) } | ||
} | ||
} | ||
|
||
/// Resolves the configured commands to a `ConfiguredCommands` struct. | ||
/// | ||
/// - parameters: | ||
/// - container: `Container` to use for creating lazily initialized commands. | ||
/// - returns: `Commands` struct which contains initialized commands. | ||
/// - throws: Errors creating the lazy commands from the container. | ||
public func resolve(for container: Container) throws -> ConfiguredCommands { | ||
let commands = try self.commands.mapValues { lazy -> CommandRunnable in | ||
return try lazy(container) | ||
} | ||
|
||
return try .init( | ||
commands: commands, | ||
defaultCommand: defaultCommand.flatMap { try $0(container) } | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Service | ||
|
||
/// Represents a top-level group of configured commands. This is usually created by calling `resolvle(for:)` on `CommandConfig`. | ||
public struct ConfiguredCommands: Service { | ||
/// Top-level available commands, stored by unique name. | ||
public let commands: [String: CommandRunnable] | ||
|
||
/// If set, this is the default top-level command that should run if no other commands are specified. | ||
public let defaultCommand: CommandRunnable? | ||
|
||
/// Creates a new `ConfiguredCommands` struct. This is usually done by calling `resolvle(for:)` on `CommandConfig`. | ||
/// | ||
/// - parameters: | ||
/// - commands: Top-level available commands, stored by unique name. | ||
/// - defaultCommand: If set, this is the default top-level command that should run if no other commands are specified. | ||
public init(commands: [String: CommandRunnable] = [:], defaultCommand: CommandRunnable? = nil) { | ||
self.commands = commands | ||
self.defaultCommand = defaultCommand | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters