Skip to content

Commit 31fc38b

Browse files
committed
Add the special service named @container to self inject the container instance as service dependencies
1 parent 8f2aacd commit 31fc38b

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

src/FunctionArgument.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
var CONTAINER_SERVICE_NAME = '@container';
4+
35
/**
46
* @param {*} value
57
* @constructor
@@ -36,6 +38,19 @@ FunctionArgument.prototype._isParameterReference = function _isParameterReferenc
3638
return variableReferenceRegex.test(argumentValue);
3739
};
3840

41+
/**
42+
* @param {*} argumentValue
43+
* @return {boolean}
44+
* @private
45+
*/
46+
FunctionArgument.prototype._isContainerReference = function _isContainerReference(argumentValue) {
47+
if (!this._isString(argumentValue)) {
48+
return false;
49+
}
50+
51+
return argumentValue === CONTAINER_SERVICE_NAME;
52+
};
53+
3954
/**
4055
* @param {*} argumentValue
4156
* @return {boolean}
@@ -59,6 +74,8 @@ FunctionArgument.prototype._isServiceReference = function _isServiceReference(ar
5974
FunctionArgument.prototype._detectType = function _detectType(argumentValue) {
6075
if (this._isParameterReference(argumentValue)) {
6176
return FunctionArgument.TYPE_PARAMETER_REFERENCE;
77+
} else if (this._isContainerReference(argumentValue)) {
78+
return FunctionArgument.TYPE_CONTAINER_REFERENCE;
6279
} else if (this._isServiceReference(argumentValue)) {
6380
return FunctionArgument.TYPE_SERVICE_REFERENCE;
6481
}
@@ -123,7 +140,9 @@ FunctionArgument.prototype.isParameterReference = function isParameterReference(
123140
* @return {*}
124141
*/
125142
FunctionArgument.prototype.resolve = function resolve(container) {
126-
if (FunctionArgument.TYPE_PARAMETER_REFERENCE === this.type) {
143+
if (FunctionArgument.TYPE_CONTAINER_REFERENCE === this.type) {
144+
return container;
145+
} else if (FunctionArgument.TYPE_PARAMETER_REFERENCE === this.type) {
127146
return container.getParameter(this._extractReferencedParameterName(this.name));
128147
} else if (FunctionArgument.TYPE_SERVICE_REFERENCE === this.type) {
129148
return container.getService(this._extractReferencedServiceName(this.name));
@@ -132,6 +151,7 @@ FunctionArgument.prototype.resolve = function resolve(container) {
132151
return this.name;
133152
};
134153

154+
FunctionArgument.TYPE_CONTAINER_REFERENCE = 'container-reference';
135155
FunctionArgument.TYPE_PARAMETER_REFERENCE = 'parameter-reference';
136156
FunctionArgument.TYPE_SERVICE_REFERENCE = 'service-reference';
137157
FunctionArgument.TYPE_VALUE = 'simple-value';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
/**
4+
* @constructor
5+
*/
6+
var ServiceThatDependOnTheContainer = function ServiceThatDependOnTheContainer(container) {
7+
this.container = container;
8+
};
9+
10+
ServiceThatDependOnTheContainer.prototype.getContainer = function getContainer() {
11+
return this.container;
12+
};
13+
14+
module.exports = ServiceThatDependOnTheContainer;

tests/fixture/valid/services.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ module.exports = {
4646
{
4747
'name': 'foo.serviceE',
4848
'service': require('./ServiceE')
49-
}
49+
},
50+
51+
{
52+
'name': 'foo.service.that.depend.on.the.container',
53+
'service': require('./ServiceThatDependOnTheContainer'),
54+
'arguments': [
55+
'@container',
56+
],
57+
'singleton': true
58+
},
5059
]
5160
};

tests/functionals/ContainerFactorySpec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var servicesConfigurationErroredWithCyclicDependencies = require('./../fixture/e
77
var servicesConfigurationErroredWithUnknownDependencies = require('./../fixture/errored-with-unknown-dependencies/services');
88
var servicesConfigurationErroredWithInvalidCalls = require('./../fixture/errored-with-invalid-calls/services');
99
var ServiceA = require('./../fixture/valid/ServiceA');
10+
var ServiceThatDependOnTheContainer = require('./../fixture/valid/ServiceThatDependOnTheContainer');
1011

1112
describe('ContainerFactory', function () {
1213
it('should return the parameter value', function () {
@@ -159,4 +160,11 @@ describe('ContainerFactory', function () {
159160
ContainerFactory.create(servicesConfigurationErroredWithInvalidCalls.services, servicesConfigurationErroredWithInvalidCalls.parameters);
160161
}).to.not.throw();
161162
});
163+
164+
it('should inject itself as service dependency', function () {
165+
var container = ContainerFactory.create(servicesConfigurationValid.services, servicesConfigurationValid.parameters);
166+
167+
var service = container.getService('foo.service.that.depend.on.the.container');
168+
expect(service.getContainer()).to.be.equal(container);
169+
});
162170
});

0 commit comments

Comments
 (0)