Skip to content

Commit 6104096

Browse files
committed
beta.2
1 parent 3f46028 commit 6104096

File tree

4 files changed

+75
-41
lines changed

4 files changed

+75
-41
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,8 @@ jobs:
88
runs-on: ubuntu-latest
99
steps:
1010
- uses: actions/checkout@v1
11-
- run: swift test --enable-test-discovery
11+
- run: swift test --enable-test-discovery --sanitize=thread
1212
bionic:
13-
container:
14-
image: vapor/swift:5.1-bionic
15-
runs-on: ubuntu-latest
16-
steps:
17-
- uses: actions/checkout@v1
18-
- run: swift test --enable-test-discovery
19-
thread:
2013
container:
2114
image: vapor/swift:5.1-bionic
2215
runs-on: ubuntu-latest

Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ let package = Package(
1010
.library(name: "Leaf", targets: ["Leaf"]),
1111
],
1212
dependencies: [
13-
.package(url: "https://github.com/vapor/leaf-kit.git", .branch("master")),
14-
.package(url: "https://github.com/vapor/vapor.git", .branch("master")),
13+
.package(url: "https://github.com/vapor/leaf-kit.git", from: "1.0.0-beta.2"),
14+
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-beta.2"),
1515
],
1616
targets: [
1717
.target(name: "Leaf", dependencies: ["LeafKit", "Vapor"]),
18-
.testTarget(name: "LeafTests", dependencies: ["Leaf"]),
18+
.testTarget(name: "LeafTests", dependencies: ["Leaf", "XCTVapor"]),
1919
]
2020
)

Sources/Leaf/LeafProvider.swift

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,53 @@
11
import Vapor
22

3-
public final class Leaf: Provider {
4-
public let application: Application
5-
public var cache: LeafCache
6-
7-
public var renderer: LeafRenderer {
8-
.init(
9-
configuration: .init(
10-
rootDirectory: self.application.directory.viewsDirectory
11-
),
12-
cache: self.cache,
13-
fileio: self.application.fileio,
14-
eventLoop: self.application.eventLoopGroup.next()
15-
)
16-
}
17-
18-
public init(_ application: Application) {
19-
self.application = application
20-
self.cache = DefaultLeafCache()
3+
extension Application {
4+
public var leaf: Leaf {
5+
.init(application: self)
216
}
22-
23-
public func register(_ app: Application) {
24-
app.views.use { self.renderer }
7+
8+
public struct Leaf {
9+
final class Storage {
10+
var cache: LeafCache
11+
12+
init() {
13+
self.cache = DefaultLeafCache()
14+
}
15+
}
16+
17+
struct Key: StorageKey {
18+
typealias Value = Storage
19+
}
20+
21+
public var renderer: LeafRenderer {
22+
.init(
23+
configuration: .init(
24+
rootDirectory: self.application.directory.viewsDirectory
25+
),
26+
cache: self.cache,
27+
fileio: self.application.fileio,
28+
eventLoop: self.application.eventLoopGroup.next()
29+
)
30+
}
31+
32+
public var cache: LeafCache {
33+
self.storage.cache
34+
}
35+
36+
var storage: Storage {
37+
if let existing = self.application.storage[Key.self] {
38+
return existing
39+
} else {
40+
let new = Storage()
41+
self.application.storage[Key.self] = new
42+
return new
43+
}
44+
}
45+
46+
public let application: Application
2547
}
2648
}
2749

50+
2851
extension Request {
2952
var leaf: LeafRenderer {
3053
.init(
@@ -36,12 +59,6 @@ extension Request {
3659
}
3760
}
3861

39-
extension Application {
40-
public var leaf: Leaf {
41-
self.providers.require(Leaf.self)
42-
}
43-
}
44-
4562
extension LeafRenderer: ViewRenderer {
4663
public func `for`(_ request: Request) -> ViewRenderer {
4764
LeafRenderer(
@@ -66,3 +83,13 @@ extension LeafRenderer: ViewRenderer {
6683
}
6784
}
6885
}
86+
87+
extension Application.Views.Provider {
88+
public static var leaf: Self {
89+
.init {
90+
$0.views.use {
91+
$0.leaf.renderer
92+
}
93+
}
94+
}
95+
}

Tests/LeafTests/LeafTests.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
import Leaf
2-
import XCTest
2+
import XCTVapor
33

44
class LeafTests: XCTestCase {
5-
func testStub() throws {
6-
5+
func testApplication() throws {
6+
let app = Application(.testing)
7+
defer { app.shutdown() }
8+
9+
app.views.use(.leaf)
10+
11+
app.get("test-file") { req in
12+
req.view.render(#file, ["foo": "bar"])
13+
}
14+
15+
try app.test(.GET, "test-file") { res in
16+
XCTAssertEqual(res.status, .ok)
17+
XCTAssertEqual(res.headers.contentType, .html)
18+
// test: #(foo)
19+
XCTAssertContains(res.body.string, "test: bar")
20+
}
721
}
822
}

0 commit comments

Comments
 (0)