adonis-fold
Advanced tools
Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "adonis-fold", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Fold is an IOC container with solid DI layer for node js applications", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node maketest", | ||
"test": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec test && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage", | ||
"postinstall": "node postinstall", | ||
@@ -22,5 +22,7 @@ "coverage": "node makecoverage" | ||
"co": "^4.6.0", | ||
"coveralls": "^2.11.4", | ||
"istanbul": "^0.3.18", | ||
"mocha": "^2.2.5" | ||
"mocha": "^2.2.5", | ||
"mocha-lcov-reporter": "0.0.2" | ||
} | ||
} |
@@ -1,5 +0,11 @@ | ||
[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/dazzlejs/fold) | ||
# Fold | ||
## Fold | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) | ||
[![Coverage Status](https://coveralls.io/repos/adonisjs/fold/badge.svg?branch=master&service=github)](https://coveralls.io/github/adonisjs/fold?branch=master) | ||
![](https://img.shields.io/travis/adonisjs/fold.svg) | ||
Fold is an Ioc container and dependency injection manager for node applications, it is used by adonis. | ||
Fold is the real guy behind adonis framework , it is a IOC Container with solid DI in place. | ||
## Docs | ||
Visit [adonisjs.com](#http://www.adonisjs.com) to read docs. |
@@ -21,2 +21,3 @@ 'use strict' | ||
let staticInjections = [] | ||
let useStatic = false | ||
@@ -80,5 +81,8 @@ /** | ||
IocHelpers.registerProvider = function (Provider) { | ||
return new Promise(function (resolve, reject) { | ||
if (Provider.inject) { | ||
staticInjections = Provider.inject | ||
}else{ | ||
staticInjections = [] | ||
} | ||
@@ -91,5 +95,24 @@ | ||
co(function *() { | ||
yield instance.register() | ||
}).then(resolve).catch(reject) | ||
return yield instance.register() | ||
}).then(function (response) { | ||
/** | ||
* make sure to clear static method injections after register | ||
* is called | ||
*/ | ||
staticInjections = [] | ||
resolve(response) | ||
}).catch(function (error){ | ||
/** | ||
* make sure to clear static method injections after register | ||
* is called | ||
*/ | ||
staticInjections = [] | ||
reject(error) | ||
}) | ||
}) | ||
} | ||
@@ -109,2 +132,3 @@ | ||
IocHelpers.bindProvider = function (resolvedProviders, unResolvedProviders, binding, closure, singleton) { | ||
// removing from unresolved if it was | ||
@@ -129,7 +153,4 @@ // deferred. | ||
// clear staticInjections once have injections in place | ||
staticInjections = [] | ||
// adding to resolved providers | ||
resolvedProviders[binding] = {closure, injections, singleton} | ||
} |
@@ -65,2 +65,5 @@ 'use strict' | ||
let dumpBasePath = null | ||
let dumpNamespace = null | ||
try { | ||
@@ -193,2 +196,14 @@ dump = require('../../dump/hash') | ||
/** | ||
* @function dumpSettings | ||
* @description Set autoload dump settings to make resolves easy | ||
* @param {String} basePath | ||
* @param {String} NameSpace | ||
* @return {void} | ||
*/ | ||
Ioc.dumpSettings = function (basePath,NameSpace){ | ||
dumpBasePath = basePath, | ||
dumpNamespace = NameSpace | ||
} | ||
/** | ||
* @function use | ||
@@ -204,3 +219,3 @@ * @description Equalent to node's require , with | ||
// it can be a PROVIDER, UNRESOLVED_PROVIDER, NPM_MODULE,LOCAL_MODULE | ||
const type = Loader.returnInjectionType(resolveProviders, unResolveProviders, aliases, dump, binding) | ||
const type = Loader.returnInjectionType(resolveProviders, unResolveProviders, aliases, dump, dumpBasePath, dumpNamespace, binding) | ||
@@ -213,3 +228,3 @@ // if looking for unresolved provider , send them back with an error | ||
// here we grab that binding using it's type | ||
let bindingModule = Loader.resolveUsingType(resolveProviders, unResolveProviders, aliases, dump, binding, type) | ||
let bindingModule = Loader.resolveUsingType(resolveProviders, unResolveProviders, aliases, dump, dumpBasePath, dumpNamespace, binding, type) | ||
@@ -222,2 +237,3 @@ // if i am resolved provider than make me before returning | ||
}) | ||
if (bindingModule.singleton) { | ||
@@ -287,3 +303,3 @@ if (!bindingModule.instance) { | ||
if (typeof (binding) === 'string') { | ||
type = Loader.returnInjectionType(resolveProviders, unResolveProviders, aliases, dump, binding) | ||
type = Loader.returnInjectionType(resolveProviders, unResolveProviders, aliases, dump, dumpBasePath, dumpNamespace, binding) | ||
if (type === 'UNRESOLVED_PROVIDER') { | ||
@@ -328,7 +344,7 @@ let provider = require(unResolveProviders[binding]) | ||
Ioc._makeProvider = function (provider) { | ||
let instances = Q() | ||
let instances = [] | ||
provider.injections.forEach(function (injection) { | ||
instances = instances.then(Ioc.make.bind(null, injection)) | ||
instances.push(Ioc.make(injection)) | ||
}) | ||
return instances | ||
return Q.all(instances) | ||
} | ||
@@ -350,3 +366,3 @@ | ||
let injections = [] | ||
let instances = Q() | ||
let instances = [] | ||
@@ -366,6 +382,8 @@ /** | ||
injection = injection.replace(/_/g, '/') | ||
instances = instances.then(Ioc.make.bind(null, injection)) | ||
instances.push(Ioc.make(injection)) | ||
}) | ||
instances.then(function (values) { | ||
Q | ||
.all(instances) | ||
.then(function (values) { | ||
resolve(new (_bind.apply(Binding, [null].concat(values)))()) | ||
@@ -372,0 +390,0 @@ }).catch(reject) |
@@ -64,3 +64,3 @@ 'use strict' | ||
*/ | ||
Loader.resolveUsingType = function (bindings, unresolvedBindings, aliases, dump, injection, type) { | ||
Loader.resolveUsingType = function (bindings, unresolvedBindings, aliases, dump, dumpBasePath, dumpNamespace, injection, type) { | ||
let instance = null | ||
@@ -77,3 +77,7 @@ injection = aliases[injection] || injection | ||
case 'LOCAL_MODULE': | ||
instance = Loader.require(dump[injection]) | ||
if(dump[injection]){ | ||
instance = Loader.require(dump[injection]) | ||
}else{ | ||
instance = Loader._makeDump(injection,dumpBasePath,dumpNamespace) | ||
} | ||
break | ||
@@ -99,11 +103,13 @@ case 'NPM_MODULE': | ||
*/ | ||
Loader.returnInjectionType = function (bindings, unresolvedBindings, aliases, dump, injection) { | ||
Loader.returnInjectionType = function (bindings, unresolvedBindings, aliases, dump, dumpBasePath, dumpNamespace, injection) { | ||
injection = aliases[injection] || injection | ||
if (bindings[injection]) { | ||
return 'PROVIDER' | ||
}else if (unresolvedBindings[injection]) { | ||
return 'UNRESOLVED_PROVIDER' | ||
}else if (dump[injection]) { | ||
return 'LOCAL_MODULE' | ||
} else if (unresolvedBindings[injection]) { | ||
return 'UNRESOLVED_PROVIDER' | ||
} else { | ||
}else if(Loader._isDump(dumpNamespace,injection)) { | ||
return 'LOCAL_MODULE' | ||
}else { | ||
return 'NPM_MODULE' | ||
@@ -114,2 +120,29 @@ } | ||
/** | ||
* @function isDump | ||
* @description Try to figure out whether is it a | ||
* dump path or not. | ||
* @param {String} namespace | ||
* @param {String} injection | ||
* @return {Boolean} | ||
*/ | ||
Loader._isDump = function(namespace,injection){ | ||
return injection.startsWith(namespace) | ||
} | ||
/** | ||
* @function _makeDump | ||
* @description it makes path to local module using namespace and | ||
* require it | ||
* @param {String} injection | ||
* @param {String} basePath | ||
* @param {String} namespace | ||
* @return {*} | ||
*/ | ||
Loader._makeDump = function (injection,basePath,namespace) { | ||
const incrmentalPath = injection.replace(namespace,'') | ||
const modulePath = path.join(basePath,incrmentalPath) | ||
return Loader.require(modulePath) | ||
} | ||
/** | ||
* @function generateDirectoryHash | ||
@@ -116,0 +149,0 @@ * @description Generates directory hash with key/value pairs |
@@ -30,9 +30,13 @@ 'use strict' | ||
let loadedProviders = helpers.requireHash(hash) | ||
let instances = Q() | ||
let instances = [] | ||
loadedProviders.forEach(function (provider) { | ||
instances = instances.then(iocHelpers.registerProvider.bind(null, provider)) | ||
instances.push(iocHelpers.registerProvider(provider)) | ||
}) | ||
instances.then(function () { | ||
Q | ||
.all(instances) | ||
.then(function () { | ||
return Registerar.stableizeCycle() | ||
}).then(resolve).catch(reject) | ||
}) | ||
@@ -55,8 +59,8 @@ } | ||
let instances = Q() | ||
let instances = [] | ||
toBeResolvedFromDeferred.forEach(function (injection) { | ||
let provider = helpers.requireInjection(injection) | ||
instances = instances.then(iocHelpers.registerProvider.bind(null, provider)) | ||
instances.push(iocHelpers.registerProvider(provider)) | ||
}) | ||
return instances | ||
return Q.all(instances) | ||
} | ||
@@ -63,0 +67,0 @@ |
module.exports = function Users(){ | ||
} |
@@ -76,2 +76,3 @@ 'use strict' | ||
it('should make use of static inject property over class constructor while making classes', function (done) { | ||
class Foo { | ||
@@ -333,2 +334,36 @@ constructor () { | ||
it('should make a provider which is dependent upon other providers which has no dependencies', function (done) { | ||
Ioc.bind('Foo/Bar', function () { | ||
return 'bar' | ||
}) | ||
Ioc.bind('Foo/Baz', function () { | ||
return 'baz' | ||
}) | ||
class Birds{ | ||
static get inject(){ | ||
return ['Foo/Bar','Foo/Baz'] | ||
} | ||
constructor(Bar,Baz) { | ||
this.bar = Bar | ||
this.baz = Baz | ||
} | ||
} | ||
Ioc | ||
.make(Birds) | ||
.then (function (user) { | ||
expect(user.bar).to.equal('bar') | ||
expect(user.baz).to.equal('baz') | ||
done() | ||
}).catch(done) | ||
}) | ||
describe('Ioc Helpers', function () { | ||
@@ -335,0 +370,0 @@ it('should register a provider with class defination even if there is no register method', function (done) { |
@@ -57,3 +57,3 @@ 'use strict' | ||
it('should determine type of injection to be fulfilled and fallback to npm module when not find inside container', function () { | ||
const module = Loader.returnInjectionType({}, {}, {}, {}, 'lodash') | ||
const module = Loader.returnInjectionType({}, {}, {}, {}, null, null, 'lodash') | ||
expect(module).to.equal('NPM_MODULE') | ||
@@ -63,3 +63,3 @@ }) | ||
it('should determine type of injection and get internal mapping if exists inside dump', function () { | ||
const module = Loader.returnInjectionType({}, {}, {}, {'App/Users': '../../user'}, 'App/Users') | ||
const module = Loader.returnInjectionType({}, {}, {}, {'App/Users': '../../user'}, null, null, 'App/Users') | ||
expect(module).to.equal('LOCAL_MODULE') | ||
@@ -70,3 +70,3 @@ }) | ||
const binding = function () { return 'foo' } | ||
const module = Loader.returnInjectionType({'App/Users': binding}, {}, {}, {}, 'App/Users') | ||
const module = Loader.returnInjectionType({'App/Users': binding}, {}, {}, {}, null, null, 'App/Users') | ||
expect(module).to.equal('PROVIDER') | ||
@@ -79,4 +79,4 @@ }) | ||
const type = Loader.returnInjectionType(bindings, {}, {}, {}, 'App/Foo') | ||
const instance = Loader.resolveUsingType(bindings, {}, {}, {}, 'App/Foo', type) | ||
const type = Loader.returnInjectionType(bindings, {}, {}, {}, null, null, 'App/Foo') | ||
const instance = Loader.resolveUsingType(bindings, {}, {}, {}, null, null, 'App/Foo', type) | ||
@@ -87,2 +87,19 @@ expect(type).to.equal('PROVIDER') | ||
it('should return local module as type for bindings starting with namespace', function (){ | ||
const type = Loader.returnInjectionType({}, {}, {}, {}, './app', 'App', 'App/Foo') | ||
expect(type).to.equal('LOCAL_MODULE') | ||
}) | ||
it('should require local module when passed namespace', function (){ | ||
const type = Loader.returnInjectionType({}, {}, {}, {}, './app', 'App', 'App/Foo') | ||
const instance = Loader.resolveUsingType({}, {}, {}, {}, path.join(__dirname,'./app'), 'App', 'App/Http/Users',type) | ||
expect(type).to.equal('LOCAL_MODULE') | ||
expect(instance.name).to.equal('Users') | ||
}) | ||
it('should detect unresolved bindings', function () { | ||
@@ -92,4 +109,4 @@ const binding = function () { return 'foo' } | ||
const type = Loader.returnInjectionType({}, bindings, {}, {}, 'App/Foo') | ||
const instance = Loader.resolveUsingType({}, bindings, {}, {}, 'App/Foo', type) | ||
const type = Loader.returnInjectionType({}, bindings, {}, {}, null, null, 'App/Foo') | ||
const instance = Loader.resolveUsingType({}, bindings, {}, {}, null, null, 'App/Foo', type) | ||
@@ -102,3 +119,3 @@ expect(type).to.equal('UNRESOLVED_PROVIDER') | ||
const fn = function () { | ||
return Loader.resolveUsingType({}, {}, {}, 'App/Foo', null) | ||
return Loader.resolveUsingType({}, {}, {}, null, null, 'App/Foo', null) | ||
} | ||
@@ -105,0 +122,0 @@ |
@@ -89,2 +89,20 @@ 'use strict' | ||
it('should work find when service provider binds multiple providers to Ioc container and it makes use of static injections', function (done) { | ||
Ioc.bind('App/Foo', function () { | ||
return 'foo' | ||
}) | ||
let providers = [path.join(__dirname,'./providers/StaticGenerator')] | ||
Registerar.register(providers, {}) | ||
.then(function () { | ||
const Static = Ioc.use('App/Static') | ||
const Fav = Ioc.use('App/Fav') | ||
expect(Static.foo).to.equal('foo') | ||
expect(Fav.foo).to.equal('foo') | ||
done() | ||
}).catch(done) | ||
}) | ||
it('should autoload a given directory and register mappings inside Ioc container', function (done) { | ||
@@ -91,0 +109,0 @@ const appDir = path.join(__dirname, './app') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57700
41
1753
12
6