Skip to content

Commit 5b88e12

Browse files
author
Hadevs
committed
add md images
1 parent 8af1a74 commit 5b88e12

File tree

10 files changed

+74
-17
lines changed

10 files changed

+74
-17
lines changed

SwiftInjector/SwiftInjector.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
E676F73122C954BB00DC1282 /* TestContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = E676F73022C954BB00DC1282 /* TestContainer.swift */; };
2121
E676F73322C955B200DC1282 /* DIContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = E676F73222C955B200DC1282 /* DIContainer.swift */; };
2222
E676F73522C9688F00DC1282 /* TestClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = E676F73422C9688F00DC1282 /* TestClass.swift */; };
23+
E6BF5C6322CD2A32009A9331 /* SILogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6BF5C6222CD2A32009A9331 /* SILogger.swift */; };
2324
/* End PBXBuildFile section */
2425

2526
/* Begin PBXContainerItemProxy section */
@@ -50,6 +51,7 @@
5051
E676F73022C954BB00DC1282 /* TestContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestContainer.swift; sourceTree = "<group>"; };
5152
E676F73222C955B200DC1282 /* DIContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DIContainer.swift; sourceTree = "<group>"; };
5253
E676F73422C9688F00DC1282 /* TestClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestClass.swift; sourceTree = "<group>"; };
54+
E6BF5C6222CD2A32009A9331 /* SILogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SILogger.swift; sourceTree = "<group>"; };
5355
/* End PBXFileReference section */
5456

5557
/* Begin PBXFrameworksBuildPhase section */
@@ -117,6 +119,7 @@
117119
isa = PBXGroup;
118120
children = (
119121
E676F72622C94F4700DC1282 /* RootContainer.swift */,
122+
E6BF5C6222CD2A32009A9331 /* SILogger.swift */,
120123
E676F73022C954BB00DC1282 /* TestContainer.swift */,
121124
E676F73222C955B200DC1282 /* DIContainer.swift */,
122125
E676F72E22C9548B00DC1282 /* Container.swift */,
@@ -229,6 +232,7 @@
229232
E676F73522C9688F00DC1282 /* TestClass.swift in Sources */,
230233
E676F73322C955B200DC1282 /* DIContainer.swift in Sources */,
231234
E676F70822C94F2B00DC1282 /* ViewController.swift in Sources */,
235+
E6BF5C6322CD2A32009A9331 /* SILogger.swift in Sources */,
232236
E676F72F22C9548B00DC1282 /* Container.swift in Sources */,
233237
E676F72922C94F6000DC1282 /* Containerable.swift in Sources */,
234238
E676F73122C954BB00DC1282 /* TestContainer.swift in Sources */,

SwiftInjector/SwiftInjector/AppDelegate.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1616

1717

1818
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
19-
let newVc: ViewController? = container.resolve()
19+
let newVc: ViewController? = container.resolve(name: "123")
2020
let testClass = newVc?.testClass
21-
print(testClass)
2221

22+
23+
print(testClass)
2324
return true
2425
}
2526

SwiftInjector/SwiftInjector/Source/Containerable.swift

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,30 @@ protocol Containerable: class {
1919
}
2020

2121
struct ContainerObject {
22+
var name: String? = nil
2223
var registration: Containerable.Service
2324
var object: Containerable.Object? = nil
24-
init(_ registration: @escaping Containerable.Service) {
25+
init(_ registration: @escaping Containerable.Service, name: String? = nil) {
2526
self.registration = registration
27+
self.name = name
2628
}
2729
}
2830

2931
extension Containerable {
30-
private func resolveAny(typeString: String) -> Object? {
32+
private func resolveAny(typeString: String, name: String? = nil) -> Object? {
3133
// TODO: replace .first with more smart condition
32-
let array = services[typeString]
34+
let array: [ContainerObject]? = {
35+
if let filterName = name {
36+
return services[typeString]?.filter { $0.name == filterName }
37+
} else {
38+
return services[typeString]
39+
}
40+
}()
41+
42+
if (array?.count ?? 0) > 1 {
43+
SILogger("Warning in \(typeString) resolving. You registered two different object for this type. Try to provide \"name\" argument while registering your object. But you will receive first object of all services you registered.").log()
44+
}
45+
3346
if let object = array?.first?.object {
3447
autoresolve(on: object)
3548
return object
@@ -46,9 +59,9 @@ extension Containerable {
4659
}
4760
}
4861

49-
func resolve<T: Object>() -> T? {
62+
func resolve<T: Object>(name: String? = nil) -> T? {
5063
let key = String(describing: T.self)
51-
let object = resolveAny(typeString: key) as? T
64+
let object = resolveAny(typeString: key, name: name) as? T
5265

5366
return object
5467
}
@@ -69,16 +82,17 @@ extension Containerable {
6982
}
7083
}
7184

72-
func register<T: Object>(_ registration: @escaping (() -> T)) {
85+
func register<T: Object>(_ registration: @escaping (() -> T), name: String? = nil) {
86+
7387
dispatchRegistrationGroup.enter()
7488
let object = registration()
7589
let key = String(describing: type(of: object))
7690
if let array = services[key] {
7791
var newArray = array
78-
newArray.append(ContainerObject(registration))
92+
newArray.append(ContainerObject(registration, name: name))
7993
services[key] = newArray
8094
} else {
81-
services[key] = [ContainerObject(registration)]
95+
services[key] = [ContainerObject(registration, name: name)]
8296
}
8397
dispatchRegistrationGroup.leave()
8498
}

SwiftInjector/SwiftInjector/Source/DIContainer.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ class DIContainer: Container {
1616
self.register()
1717
}
1818

19-
final func register<T: AnyObject>(_ registration: @escaping (() -> T)) {
20-
parentContainer.register(registration)
19+
final func register<T: Containerable.Object>(name: String? = nil, _ registration: @escaping (() -> T)) {
20+
parentContainer.register(registration, name: name)
2121
}
2222

23-
final func resolve<T: Containerable.Object>() -> T? {
24-
return parentContainer.resolve()
23+
final func resolve<T: Containerable.Object>(name: String? = nil) -> T? {
24+
return parentContainer.resolve(name: name)
2525
}
2626

2727
func register() {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// SILogger.swift
3+
// SwiftInjector
4+
//
5+
// Created by Ghost on 03.07.2019.
6+
// Copyright © 2019 Ghost. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
struct SILogger {
12+
private let prefix: String = "SwiftInjector: "
13+
enum Priority {
14+
case low
15+
case medium
16+
case high
17+
18+
var prefix: String {
19+
switch self {
20+
case .low, .medium: return ""
21+
case .high: return "[!IMPORTANT!]:"
22+
}
23+
}
24+
}
25+
26+
private let text: String
27+
28+
init(_ text: String) {
29+
self.text = text
30+
}
31+
32+
func log(priority: Priority = .medium) {
33+
let result = prefix + priority.prefix + " " + text
34+
print(result)
35+
}
36+
}

SwiftInjector/SwiftInjector/Source/TestContainer.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ import Foundation
1010

1111
class TestContainer: DIContainer {
1212
let vc = ViewController()
13+
let vc2 = ViewController()
14+
1315
override func register() {
1416
register { TestClass() }
15-
register { self.vc }
17+
register(name: "123") { self.vc }
18+
register { self.vc2 }
1619
}
1720
}

SwiftInjector/SwiftInjector/ViewController.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ import UIKit
1010

1111
class ViewController: UIViewController {
1212
var testClass: TestClass?
13+
1314
override func viewDidLoad() {
1415
super.viewDidLoad()
1516
// Do any additional setup after loading the view.
1617
}
17-
18-
1918
}
2019

example_1.png

89.8 KB
Loading

example_2.png

40.4 KB
Loading

example_3.png

72.7 KB
Loading

0 commit comments

Comments
 (0)