This repository has been archived by the owner on Apr 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
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 #56 from nodes-vapor/feature/mutable-leaf-tag-config
Mutable leaf tag config + Provider
- Loading branch information
Showing
4 changed files
with
69 additions
and
9 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
3 changes: 3 additions & 0 deletions
3
Sources/Sugar/Helpers/LeafTagConfig.swift → ...es/Sugar/Helpers/Leaf/LeafTagConfig.swift
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,30 @@ | ||
import Leaf | ||
import Vapor | ||
|
||
/// Service that can be used to add tags from multiple sources which can then be registered at once | ||
/// on `Leaf`s `LeafTagConfig`. | ||
/// This is a workaround for not being able to mutate `Leaf`'s `LeafTagConfig`. | ||
/// - See: https://github.com/vapor/leaf/pull/113. | ||
public final class MutableLeafTagConfig: Service { | ||
var storage: [String: TagRenderer] = [:] | ||
} | ||
|
||
extension MutableLeafTagConfig { | ||
/// Register multiple tags using the keys as their names. | ||
/// | ||
/// - Parameter list: Map of names to tags. | ||
public func use(_ list: [String: TagRenderer]) { | ||
for (name, tag) in list { | ||
storage[name] = tag | ||
} | ||
} | ||
|
||
/// Register a single tag. | ||
/// | ||
/// - Parameters: | ||
/// - tag: the tag to register. | ||
/// - name: the name for the tag to use in template files. | ||
public func use(_ tag: TagRenderer, as name: String) { | ||
storage[name] = tag | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Sources/Sugar/Helpers/Leaf/MutableLeafTagConfigProvider.swift
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,28 @@ | ||
import Leaf | ||
|
||
/// Provider that transfers the leaf tags from the `MutableLeafTagConfig` to `Leaf`'s | ||
/// `LeafTagConfig`. | ||
/// This is a workaround for not being able to mutate `Leaf`'s `LeafTagConfig`. | ||
/// - See: https://github.com/vapor/leaf/pull/113. | ||
public final class MutableLeafTagConfigProvider: Provider { | ||
/// Creates an `MutableLeafTagConfigProvider`. | ||
public init() {} | ||
|
||
/// See `Provider.register`. | ||
public func register(_ services: inout Services) throws { | ||
// register LeafProvider here to ensure it does not override our `LeafTagConfig`. | ||
try services.register(LeafProvider()) | ||
services.register(MutableLeafTagConfig()) | ||
services.register { container -> LeafTagConfig in | ||
let mutableConfig: MutableLeafTagConfig = try container.make() | ||
var config = LeafTagConfig.default() | ||
config.use(mutableConfig.storage) | ||
return config | ||
} | ||
} | ||
|
||
/// See `Provider.didBoot`. | ||
public func didBoot(_ container: Container) throws -> Future<Void> { | ||
return .done(on: container) | ||
} | ||
} |