Skip to content

Commit

Permalink
Push VerboseLogging Param Down to SyrupInjector via SyrupInjectorOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffan207 committed Aug 25, 2022
1 parent 8388e41 commit 95fd15e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
4 changes: 3 additions & 1 deletion SyrupSource/Syrup/Framework/SyrupComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public class SyrupComponent : MonoBehaviour {
if (SyrupInjector != null) {
SyrupInjector.AddSyrupModules(syrupModules);
} else {
SyrupInjector = new SyrupInjector(syrupModules);
SyrupInjectorOptions syrupInjectorOptions = new();
syrupInjectorOptions.VerboseLogging = verboseLogging;
SyrupInjector = new SyrupInjector(syrupInjectorOptions, syrupModules);
}
}

Expand Down
23 changes: 21 additions & 2 deletions SyrupSource/Syrup/Framework/SyrupInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public class SyrupInjector {
//Given a param, map to all types that need it
private readonly Dictionary<NamedDependency, HashSet<NamedDependency>> paramOfDependencies;

private bool verboseLogging = false;

//Dependencies that have been fully constructed
private readonly Dictionary<NamedDependency, object> fulfilledDependencies;
private readonly Dictionary<NamedDependency, object> fulfilledDependencies;

public SyrupInjector(params ISyrupModule[] modules) {
dependencySources = new Dictionary<NamedDependency, DependencyInfo>();
Expand All @@ -35,6 +37,16 @@ public class SyrupInjector {
AddSyrupModules(modules);
}

public SyrupInjector(SyrupInjectorOptions syrupInjectorOptions, params ISyrupModule[] modules) {
verboseLogging = syrupInjectorOptions.VerboseLogging;

dependencySources = new Dictionary<NamedDependency, DependencyInfo>();
paramOfDependencies = new Dictionary<NamedDependency, HashSet<NamedDependency>>();
fulfilledDependencies = new Dictionary<NamedDependency, object>();

AddSyrupModules(modules);
}

internal void AddSyrupModules(params ISyrupModule[] modules) {
indegreesForType = new Dictionary<NamedDependency, int>();

Expand Down Expand Up @@ -214,9 +226,16 @@ public class SyrupInjector {

DependencyInfo dependencyInfo = dependencySources[namedDependency];
if (dependencyInfo.IsSingleton && fulfilledDependencies.ContainsKey(namedDependency)) {
if (verboseLogging) {
Debug.Log(string.Format("Provide singleton: {0}", namedDependency.ToString()));
}
return fulfilledDependencies[namedDependency];
}


if (verboseLogging) {
Debug.Log(string.Format("Constructing object: {0}", namedDependency.ToString()));
}

object dependency;
if (dependencyInfo.DependencySource == DependencySource.PROVIDER) {
MethodInfo method = dependencyInfo.ProviderMethod;
Expand Down
10 changes: 10 additions & 0 deletions SyrupSource/Syrup/Framework/SyrupInjectorOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

/// <summary>
/// Optional options block that can be passed to the SyrupInjector
/// </summary>
public class SyrupInjectorOptions {

public bool VerboseLogging { get; set; }

}
11 changes: 11 additions & 0 deletions SyrupSource/Syrup/Framework/SyrupInjectorOptions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 95fd15e

Please sign in to comment.