Skip to content

Commit cc56d1c

Browse files
Merge pull request #3 from Infomaniak/switchToApacheV2
chore: Switch to apache v2
2 parents 9e967fc + 8cf79d6 commit cc56d1c

14 files changed

+405
-923
lines changed

LICENSE

+202-674
Large diffs are not rendered by default.

NOTICE

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Infomaniak InfomaniakDI
2+
Copyright [2013-2014] Infomaniak Network SA
3+
4+
This product includes software developed at
5+
Infomaniak Network SA.

README.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# InfomaniakDI
22

3-
Minimalist dependency injection mechanism.
3+
Minimalist dependency injection mechanism written in pure Swift.
44

55
## Abstract
66
Register factories thanks to the `Factory` type into the resolver.
@@ -15,8 +15,13 @@ Use the property wrapper `@InjectService` to resolve a shared instance from any
1515
- [x] Lazy init (with @LazyInjectService)
1616
- [x] Efficient SwiftUI with `@LazyInjectService` and `@InjectService` used as IVAR in `View`
1717

18+
## OS Support
19+
20+
Anything with first party Swift support. (iOS / macOS / Linux …)
21+
1822
## Roadmap
1923
- [ ] Optionals
24+
- [ ] Multiple containers
2025

2126
## Requirements
2227
- Swift 5.x +
@@ -26,7 +31,7 @@ Use the property wrapper `@InjectService` to resolve a shared instance from any
2631

2732
Early on in the lifecycle of your app, you want to write something like this :
2833

29-
```
34+
```swift
3035
import InfomaniakDI
3136

3237
[]
@@ -44,9 +49,13 @@ catch {
4449
```
4550

4651
later on, when you want to resolve a service use the property wrapper like so:
47-
```
52+
```swift
4853
@InjectService var injected: SomeService
4954
```
5055
Injection will be performed at the init time of the owner of the property.
5156

5257
Checkout `ITSimpleReslover.swift` for more advanced examples.
58+
59+
## Licence
60+
61+
This package is available under the permissive ApacheV2 licence for you to enjoy.

Sources/InfomaniakDI/Factory.swift

+16-23
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
/*
2-
InfomaniakDI
3-
Copyright (C) 2023 Infomaniak Network SA
4-
5-
This program is free software: you can redistribute it and/or modify
6-
it under the terms of the GNU General Public License as published by
7-
the Free Software Foundation, either version 3 of the License, or
8-
(at your option) any later version.
9-
10-
This program is distributed in the hope that it will be useful,
11-
but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
GNU General Public License for more details.
14-
15-
You should have received a copy of the GNU General Public License
16-
along with this program. If not, see <http://www.gnu.org/licenses/>.
17-
*/
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing,
8+
// software distributed under the License is distributed on an
9+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10+
// KIND, either express or implied. See the License for the
11+
// specific language governing permissions and limitations
12+
// under the License.
1813

1914
import Foundation
2015

@@ -23,7 +18,6 @@ public typealias FactoryClosure = (_ parameters: [String: Any]?, _ resolver: Sim
2318

2419
/// Something that can build a type
2520
public protocol Factoryable {
26-
2721
/// Required init for a Factoryable
2822
/// - Parameters:
2923
/// - type: The type we register, prefer using a Protocol here. Great for testing.
@@ -36,16 +30,15 @@ public protocol Factoryable {
3630
/// - resolver: A resolver for chained resolution
3731
/// - Returns: Return something that can be casted as the `type` declared at init. Will throw otherwise.
3832
func build(factoryParameters: [String: Any]?, resolver: SimpleResolvable) throws -> Any
39-
33+
4034
/// The registered type, prefer using a Protocol here. Great for testing.
4135
var type: Any.Type { get }
4236
}
4337

4438
public struct Factory: Factoryable, CustomDebugStringConvertible {
45-
4639
/// The factory closure
4740
private let closure: FactoryClosure
48-
41+
4942
public let type: Any.Type
5043

5144
// MARK: Factoryable
@@ -57,12 +50,12 @@ public struct Factory: Factoryable, CustomDebugStringConvertible {
5750

5851
public func build(factoryParameters: [String: Any]? = nil,
5952
resolver: SimpleResolvable = SimpleResolver.sharedResolver) throws -> Any {
60-
try self.closure(factoryParameters, resolver)
53+
try closure(factoryParameters, resolver)
6154
}
6255

6356
// MARK: CustomDebugStringConvertible
6457

6558
public var debugDescription: String {
66-
"<\(Swift.type(of: self)): for type:\(self.type), closure:\(String(describing: self.closure))>"
59+
"<\(Swift.type(of: self)): for type:\(type), closure:\(String(describing: closure))>"
6760
}
6861
}

Sources/InfomaniakDI/InjectService.swift

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
/*
2-
InfomaniakDI
3-
Copyright (C) 2023 Infomaniak Network SA
4-
5-
This program is free software: you can redistribute it and/or modify
6-
it under the terms of the GNU General Public License as published by
7-
the Free Software Foundation, either version 3 of the License, or
8-
(at your option) any later version.
9-
10-
This program is distributed in the hope that it will be useful,
11-
but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
GNU General Public License for more details.
14-
15-
You should have received a copy of the GNU General Public License
16-
along with this program. If not, see <http://www.gnu.org/licenses/>.
17-
*/
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing,
8+
// software distributed under the License is distributed on an
9+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10+
// KIND, either express or implied. See the License for the
11+
// specific language governing permissions and limitations
12+
// under the License.
1813

1914
import Foundation
2015

Sources/InfomaniakDI/LazyInjectService.swift

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
/*
2-
InfomaniakDI
3-
Copyright (C) 2023 Infomaniak Network SA
4-
5-
This program is free software: you can redistribute it and/or modify
6-
it under the terms of the GNU General Public License as published by
7-
the Free Software Foundation, either version 3 of the License, or
8-
(at your option) any later version.
9-
10-
This program is distributed in the hope that it will be useful,
11-
but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
GNU General Public License for more details.
14-
15-
You should have received a copy of the GNU General Public License
16-
along with this program. If not, see <http://www.gnu.org/licenses/>.
17-
*/
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing,
8+
// software distributed under the License is distributed on an
9+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10+
// KIND, either express or implied. See the License for the
11+
// specific language governing permissions and limitations
12+
// under the License.
1813

1914
import Foundation
2015

Sources/InfomaniakDI/SimpleResolver.swift

+30-35
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
/*
2-
InfomaniakDI
3-
Copyright (C) 2023 Infomaniak Network SA
4-
5-
This program is free software: you can redistribute it and/or modify
6-
it under the terms of the GNU General Public License as published by
7-
the Free Software Foundation, either version 3 of the License, or
8-
(at your option) any later version.
9-
10-
This program is distributed in the hope that it will be useful,
11-
but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
GNU General Public License for more details.
14-
15-
You should have received a copy of the GNU General Public License
16-
along with this program. If not, see <http://www.gnu.org/licenses/>.
17-
*/
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing,
8+
// software distributed under the License is distributed on an
9+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10+
// KIND, either express or implied. See the License for the
11+
// specific language governing permissions and limitations
12+
// under the License.
1813

1914
import Foundation
2015

@@ -66,44 +61,44 @@ public final class SimpleResolver: SimpleResolvable, SimpleStorable, CustomDebug
6661
}
6762
return buffer
6863
}
69-
64+
7065
enum ErrorDomain: Error {
7166
case factoryMissing(identifier: String)
7267
case typeMissmatch(expected: String, got: String)
7368
}
74-
69+
7570
/// One singleton to rule them all
7671
public static let sharedResolver = SimpleResolver()
77-
72+
7873
/// Factory collection
7974
var factories = [String: Factoryable]()
80-
75+
8176
/// Resolved object collection
8277
var store = [String: Any]()
8378

8479
/// A serial queue for thread safety
8580
private let queue = DispatchQueue(label: "com.infomaniakDI.resolver")
86-
81+
8782
// MARK: SimpleStorable
88-
83+
8984
public func store(factory: Factoryable,
9085
forCustomTypeIdentifier customIdentifier: String? = nil) {
9186
let type = factory.type
92-
87+
9388
let identifier = buildIdentifier(type: type, forIdentifier: customIdentifier)
9489
queue.sync {
9590
factories[identifier] = factory
9691
}
9792
}
98-
93+
9994
// MARK: SimpleResolvable
100-
95+
10196
public func resolve<Service>(type: Service.Type,
10297
forCustomTypeIdentifier customIdentifier: String?,
10398
factoryParameters: [String: Any]? = nil,
10499
resolver: SimpleResolvable) throws -> Service {
105100
let serviceIdentifier = buildIdentifier(type: type, forIdentifier: customIdentifier)
106-
101+
107102
// load form store
108103
var fetchedService: Any?
109104
queue.sync {
@@ -112,7 +107,7 @@ public final class SimpleResolver: SimpleResolvable, SimpleStorable, CustomDebug
112107
if let service = fetchedService as? Service {
113108
return service
114109
}
115-
110+
116111
// load service from factory
117112
var factory: Factoryable?
118113
queue.sync {
@@ -121,23 +116,23 @@ public final class SimpleResolver: SimpleResolvable, SimpleStorable, CustomDebug
121116
guard let factory = factory else {
122117
throw ErrorDomain.factoryMissing(identifier: serviceIdentifier)
123118
}
124-
119+
125120
// Apply factory closure
126121
let builtType = try factory.build(factoryParameters: factoryParameters, resolver: resolver)
127122
guard let service = builtType as? Service else {
128123
throw ErrorDomain.typeMissmatch(expected: "\(Service.Type.self)", got: "\(builtType.self)")
129124
}
130-
125+
131126
// keep in store built object for later
132127
queue.sync {
133128
store[serviceIdentifier] = service
134129
}
135-
130+
136131
return service
137132
}
138-
133+
139134
// MARK: internal
140-
135+
141136
func buildIdentifier(type: Any.Type,
142137
forIdentifier identifier: String? = nil) -> String {
143138
if let identifier {
@@ -146,9 +141,9 @@ public final class SimpleResolver: SimpleResolvable, SimpleStorable, CustomDebug
146141
return "\(type)"
147142
}
148143
}
149-
144+
150145
// MARK: testing
151-
146+
152147
func removeAll() {
153148
queue.sync {
154149
factories.removeAll()

Tests/InfomaniakDITests/ITInjectService.swift

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
/*
2-
InfomaniakDITests
3-
Copyright (C) 2023 Infomaniak Network SA
4-
5-
This program is free software: you can redistribute it and/or modify
6-
it under the terms of the GNU General Public License as published by
7-
the Free Software Foundation, either version 3 of the License, or
8-
(at your option) any later version.
9-
10-
This program is distributed in the hope that it will be useful,
11-
but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
GNU General Public License for more details.
14-
15-
You should have received a copy of the GNU General Public License
16-
along with this program. If not, see <http://www.gnu.org/licenses/>.
17-
*/
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing,
8+
// software distributed under the License is distributed on an
9+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10+
// KIND, either express or implied. See the License for the
11+
// specific language governing permissions and limitations
12+
// under the License.
1813

1914
@testable import InfomaniakDI
2015
import XCTest

Tests/InfomaniakDITests/ITLazyInjectService.swift

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
/*
2-
InfomaniakDITests
3-
Copyright (C) 2023 Infomaniak Network SA
4-
5-
This program is free software: you can redistribute it and/or modify
6-
it under the terms of the GNU General Public License as published by
7-
the Free Software Foundation, either version 3 of the License, or
8-
(at your option) any later version.
9-
10-
This program is distributed in the hope that it will be useful,
11-
but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
GNU General Public License for more details.
14-
15-
You should have received a copy of the GNU General Public License
16-
along with this program. If not, see <http://www.gnu.org/licenses/>.
17-
*/
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing,
8+
// software distributed under the License is distributed on an
9+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10+
// KIND, either express or implied. See the License for the
11+
// specific language governing permissions and limitations
12+
// under the License.
1813

1914
@testable import InfomaniakDI
2015
import XCTest

0 commit comments

Comments
 (0)