Skip to content

Commit

Permalink
Fix bug and support one small feature. Increase version to 3.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
ivlevAstef committed Oct 4, 2017
1 parent 0a17081 commit 36f2b97
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v3.0.6
* feature: You can now pass the bundle from which to retrieve the object
* bugfix: When created ViewController, library didn't consider storyboard bundle

# v3.0.5
* swift4 support

Expand Down
2 changes: 1 addition & 1 deletion DITranquillity.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = 'DITranquillity'
s.version = '3.0.5'
s.version = '3.0.6'
s.summary = 'DITranquillity - Dependency injection for iOS/macOS/tvOS (Swift) '

s.description = <<-DESC
Expand Down
8 changes: 4 additions & 4 deletions Sources/Core/Private/Resolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ class Resolver {
self.container = container // onowned
}

func resolve<T>(type: T.Type = T.self, name: String? = nil) -> T {
func resolve<T>(type: T.Type = T.self, name: String? = nil, from bundle: Bundle? = nil) -> T {
log(.info, msg: "Begin resolve \(description(type: type))", brace: .begin)
defer { log(.info, msg: "End resolve \(description(type: type))", brace: .end) }

return gmake(by: make(by: type, with: name, from: nil, use: nil))
return gmake(by: make(by: type, with: name, from: bundle, use: nil))
}

func injection<T>(obj: T) {
func injection<T>(obj: T, from bundle: Bundle? = nil) {
log(.info, msg: "Begin injection in obj: \(obj)", brace: .begin)
defer { log(.info, msg: "End injection in obj: \(obj)", brace: .end) }

// swift bug - if T is Any then type(of: obj) return always any. - compile optimization?
_ = make(by: type(of: (obj as Any)), with: nil, from: nil, use: obj)
_ = make(by: type(of: (obj as Any)), with: nil, from: bundle, use: obj)
}


Expand Down
29 changes: 18 additions & 11 deletions Sources/Core/Public/DIContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,29 +122,34 @@ public extension DIContainer {
/// Can crash application, if can't found the type.
/// But if the type is optional, then the application will not crash, but it returns nil.
///
/// - Parameter bundle: Bundle from which to resolve a object
/// - Returns: Object for the specified type, or nil (see description).
public func resolve<T>() -> T {
return resolver.resolve()
public func resolve<T>(from bundle: Bundle? = nil) -> T {
return resolver.resolve(from: bundle)
}

/// Resolve object by type with tag.
/// Can crash application, if can't found the type with tag.
/// But if the type is optional, then the application will not crash, but it returns nil.
///
/// - Parameter tag: Resolve tag.
/// - Parameters:
/// - tag: Resolve tag.
/// - bundle: Bundle from which to resolve a object
/// - Returns: Object for the specified type with tag, or nil (see description).
public func resolve<T, Tag>(tag: Tag.Type) -> T {
return by(tag: tag, on: resolver.resolve())
public func resolve<T, Tag>(tag: Tag.Type, from bundle: Bundle? = nil) -> T {
return by(tag: tag, on: resolver.resolve(from: bundle))
}

/// Resolve object by type with name.
/// Can crash application, if can't found the type with name.
/// But if the type is optional, then the application will not crash, but it returns nil.
///
/// - Parameter name: Resolve name.
/// - Parameters:
/// - name: Resolve name.
/// - bundle: Bundle from which to resolve a object
/// - Returns: Object for the specified type with name, or nil (see description).
public func resolve<T>(name: String) -> T {
return resolver.resolve(name: name)
public func resolve<T>(name: String, from bundle: Bundle? = nil) -> T {
return resolver.resolve(name: name, from: bundle)
}

/// Resolve many objects by type.
Expand All @@ -157,9 +162,11 @@ public extension DIContainer {
/// Injected all dependencies into object.
/// If the object type couldn't be found, then in logs there will be a warning, and nothing will happen.
///
/// - Parameter object: object in which injections will be introduced.
public func inject<T>(into object: T) {
_ = resolver.injection(obj: object)
/// - Parameters:
/// - object: object in which injections will be introduced.
/// - bundle: Bundle from which to injection into object
public func inject<T>(into object: T, from bundle: Bundle? = nil) {
_ = resolver.injection(obj: object, from: bundle)
}

public func initializeSingletonObjects() {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>3.0.5</string>
<string>3.0.6</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion Sources/Storyboard/DIStoryboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public final class DIStoryboard: _DIStoryboardBase {
/// - Returns: The new instane of `DIStoryboard`.
public class func create(name: String, bundle storyboardBundleOrNil: Bundle?, container: DIContainer) -> DIStoryboard {
let storyboard = DIStoryboard._create(name, bundle: storyboardBundleOrNil)
storyboard.resolver = StoryboardResolver(container: container)
storyboard.resolver = StoryboardResolver(container: container, bundle: storyboardBundleOrNil)
return storyboard
}

Expand Down
8 changes: 5 additions & 3 deletions Sources/Storyboard/StoryboardResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@

/// The class responsible for injecting dependencies in the view/window controller.
final class StoryboardResolver {
init(container: DIContainer) {
init(container: DIContainer, bundle: Bundle?) {
self.container = container
self.bundle = bundle ?? Bundle.main
}

#if os(iOS) || os(tvOS)

func inject(into viewController: UIViewController) {
self.container.inject(into: viewController)
self.container.inject(into: viewController, from: bundle)

for childVC in viewController.childViewControllers {
inject(into: childVC)
Expand All @@ -32,7 +33,7 @@ final class StoryboardResolver {
#elseif os(OSX)

func inject(into viewController: Any) {
self.container.inject(into: viewController)
self.container.inject(into: viewController, from: bundle)

if let windowController = viewController as? NSWindowController, let viewController = windowController.contentViewController {
inject(into: viewController)
Expand All @@ -48,6 +49,7 @@ final class StoryboardResolver {
#endif

private let container: DIContainer
private let bundle: Bundle
}

#endif

0 comments on commit 36f2b97

Please sign in to comment.