Skip to content

Commit 9f2d592

Browse files
committed
Final polish to Interpose facade
1 parent 61ff35e commit 9f2d592

3 files changed

Lines changed: 88 additions & 99 deletions

File tree

Sources/InterposeKit/Deprecated/Interpose+Deprecated.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extension Interpose {
1111
installation, or 'Interpose.prepareHook(…)' for manual control.
1212
"""
1313
)
14-
public convenience init(
14+
public init(
1515
_ class: AnyClass,
1616
builder: (Interpose) throws -> Void
1717
) throws {
@@ -27,7 +27,7 @@ extension Interpose {
2727
installation, or 'Interpose.prepareHook(…)' for manual control.
2828
"""
2929
)
30-
public convenience init(
30+
public init(
3131
_ object: NSObject,
3232
builder: ((Interpose) throws -> Void)? = nil
3333
) throws {
@@ -95,7 +95,6 @@ extension Interpose {
9595
directly using the new static API.
9696
"""
9797
)
98-
@discardableResult
9998
public func apply(_ builder: ((Interpose) throws -> Void)? = nil) throws -> Interpose {
10099
Interpose.fail("Unavailable API")
101100
}
@@ -108,7 +107,6 @@ extension Interpose {
108107
'revert()' on them directly.
109108
"""
110109
)
111-
@discardableResult
112110
public func revert(_ builder: ((Interpose) throws -> Void)? = nil) throws -> Interpose {
113111
Interpose.fail("Unavailable API")
114112
}
Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,97 @@
1-
import Foundation
1+
import ObjectiveC
22

33
/// Interpose is a modern library to swizzle elegantly in Swift.
4-
///
5-
/// Methods are hooked via replacing the implementation, instead of the usual exchange.
6-
/// Supports both swizzling classes and individual objects.
7-
final public class Interpose {
4+
public enum Interpose {
85

9-
public init() {}
6+
// ============================================================================ //
7+
// MARK: Class Hooks
8+
// ============================================================================ //
109

11-
}
12-
13-
// MARK: Logging
14-
15-
extension Interpose {
10+
public static func prepareHook<MethodSignature, HookSignature>(
11+
on `class`: AnyClass,
12+
for selector: Selector,
13+
methodSignature: MethodSignature.Type,
14+
hookSignature: HookSignature.Type,
15+
build: @escaping HookBuilder<MethodSignature, HookSignature>
16+
) throws -> Hook {
17+
try Hook(
18+
target: .class(`class`),
19+
selector: selector,
20+
build: build
21+
)
22+
}
23+
24+
@discardableResult
25+
public static func applyHook<MethodSignature, HookSignature>(
26+
on `class`: AnyClass,
27+
for selector: Selector,
28+
methodSignature: MethodSignature.Type,
29+
hookSignature: HookSignature.Type,
30+
build: @escaping HookBuilder<MethodSignature, HookSignature>
31+
) throws -> Hook {
32+
let hook = try prepareHook(
33+
on: `class`,
34+
for: selector,
35+
methodSignature: methodSignature,
36+
hookSignature: hookSignature,
37+
build: build
38+
)
39+
try hook.apply()
40+
return hook
41+
}
42+
43+
// ============================================================================ //
44+
// MARK: Object Hooks
45+
// ============================================================================ //
46+
47+
public static func prepareHook<MethodSignature, HookSignature>(
48+
on object: NSObject,
49+
for selector: Selector,
50+
methodSignature: MethodSignature.Type,
51+
hookSignature: HookSignature.Type,
52+
build: @escaping HookBuilder<MethodSignature, HookSignature>
53+
) throws -> Hook {
54+
try Hook(
55+
target: .object(object),
56+
selector: selector,
57+
build: build
58+
)
59+
}
60+
61+
@discardableResult
62+
public static func applyHook<MethodSignature, HookSignature>(
63+
on object: NSObject,
64+
for selector: Selector,
65+
methodSignature: MethodSignature.Type,
66+
hookSignature: HookSignature.Type,
67+
build: @escaping HookBuilder<MethodSignature, HookSignature>
68+
) throws -> Hook {
69+
let hook = try prepareHook(
70+
on: object,
71+
for: selector,
72+
methodSignature: methodSignature,
73+
hookSignature: hookSignature,
74+
build: build
75+
)
76+
try hook.apply()
77+
return hook
78+
}
79+
80+
// ============================================================================ //
81+
// MARK: Logging
82+
// ============================================================================ //
83+
84+
/// The flag indicating whether logging is enabled.
1685
public static var isLoggingEnabled = false
17-
18-
static func log(_ object: Any) {
19-
if isLoggingEnabled {
20-
print("[InterposeKit] \(object)")
86+
87+
internal static func log(_ message: String) {
88+
if self.isLoggingEnabled {
89+
print("[InterposeKit] \(message)")
2190
}
2291
}
2392

24-
static func fail(_ message: String) -> Never {
93+
internal static func fail(_ message: String) -> Never {
2594
fatalError("[InterposeKit] \(message)")
2695
}
96+
2797
}

Sources/InterposeKit/__Interpose.swift

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)