Skip to content

Commit

Permalink
Added userInfo context, config, test (#159)
Browse files Browse the repository at this point in the history
* Added userInfo context, config, test

* Fix userInfo issues
  • Loading branch information
tib authored Mar 25, 2020
1 parent c3941cf commit 409ad1c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
20 changes: 16 additions & 4 deletions Sources/Leaf/Application+Leaf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ extension Application {
public let application: Application

public var renderer: LeafRenderer {
.init(
var userInfo = self.userInfo
userInfo["application"] = self

return .init(
configuration: self.configuration,
cache: self.cache,
files: self.files,
eventLoop: self.application.eventLoopGroup.next(),
userInfo: [
"application": self
]
userInfo: userInfo
)
}

Expand Down Expand Up @@ -67,6 +68,15 @@ extension Application {
self.storage.cache = newValue
}
}

public var userInfo: [AnyHashable: Any] {
get {
self.storage.userInfo
}
nonmutating set {
self.storage.userInfo = newValue
}
}

var storage: Storage {
if let existing = self.application.storage[Key.self] {
Expand All @@ -87,10 +97,12 @@ extension Application {
var configuration: LeafConfiguration?
var files: LeafFiles?
var tags: [String: LeafTag]
var userInfo: [AnyHashable: Any]

init() {
self.cache = DefaultLeafCache()
self.tags = LeafKit.defaultTags
self.userInfo = [:]
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions Sources/Leaf/Request+Leaf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import Vapor

extension Request {
var leaf: LeafRenderer {
.init(
var userInfo = self.application.leaf.userInfo
userInfo["request"] = self
userInfo["application"] = self.application

return .init(
configuration: self.application.leaf.configuration,
tags: self.application.leaf.tags,
cache: self.application.leaf.cache,
files: self.application.leaf.files,
eventLoop: self.eventLoop,
userInfo: [
"request": self,
"application": self.application
]
userInfo: userInfo
)
}
}
Expand Down
38 changes: 38 additions & 0 deletions Tests/LeafTests/LeafTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,44 @@ class LeafTests: XCTestCase {
XCTAssertEqual(res.body.string, "Hello vapor @ /test-file")
}
}

func testContextUserInfo() throws {
var test = TestFiles()
test.files["/foo.leaf"] = """
Hello #custom()!
"""

struct CustomTag: LeafTag {

func render(_ ctx: LeafContext) throws -> LeafData {
let info = ctx.userInfo["info"] as? String ?? ""

return .string(info)
}
}

let app = Application(.testing)
defer { app.shutdown() }

app.views.use(.leaf)
app.leaf.configuration.rootDirectory = "/"
app.leaf.cache.isEnabled = false
app.leaf.tags["custom"] = CustomTag()
app.leaf.files = test
app.leaf.userInfo["info"] = "World"

app.get("test-file") { req in
req.view.render("foo", [
"name": "vapor"
])
}

try app.test(.GET, "test-file") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.headers.contentType, .html)
XCTAssertEqual(res.body.string, "Hello World!")
}
}
}

struct TestFiles: LeafFiles {
Expand Down

0 comments on commit 409ad1c

Please sign in to comment.