Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.x #6

Open
wants to merge 7 commits into
base: 2.X
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Because Pimple sees anonymous functions as service definitions, you need to wrap
In some cases you may want to modify a service definition after it has been defined. You can use the extend() method to define additional code to be run on your service just after it is created

container.set('mail',function (c) {
return new \Mail();
return new Mail();
});

container.extend('mail', function(mail, c) {
Expand Down
22 changes: 13 additions & 9 deletions pimple.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
};
/** get a service instance */
this.Pimple.prototype.get=function(name){
if(this._definitions[name]===undefined){
throw new Error('Identifier "%s" is not defined.'.replace('%s', name));
}
if (this._definitions[name] instanceof Function){
return this._definitions[name](this);
}
Expand All @@ -97,15 +100,16 @@
/** extend a service */
this.Pimple.prototype.extend=function(serviceName,service){
var def,self=this;
if(this._definitions[serviceName]!==undefined){
def=self._definitions[serviceName];
return function(container){
if(def instanceof Function){
def=def(container);
}
return service(def,container);
};
if(this._definitions[serviceName]===undefined){
throw new Error('Identifier "%s" is not defined.'.replace('%s', serviceName));
}
def=self._definitions[serviceName];
if(def instanceof Function){
def=def(this);
}
service(def,this);

return this;
};
/** get a service raw definition */
this.Pimple.prototype.raw=function(name){
Expand All @@ -125,7 +129,7 @@
this.define('pimple',[],function(){return self.Pimple;});
}
//CommonJS
if(module && module.exports){
if(typeof exports !== 'undefined' && module && module.exports){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need exports do be defined? Isn't is enough with module.exports?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't remember. I have created my own Pimple repository for faster support and I used it. The Pull Request is dated end of december 2014, your question end of march 2016 ... Cancel this line, I don't remember and I don't see now why.

Have a nice day

module.exports = this.Pimple;
}
}).call(this);
2 changes: 1 addition & 1 deletion pimple.js.map

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

2 changes: 1 addition & 1 deletion pimple.min.js

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

24 changes: 22 additions & 2 deletions test/pimple.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,37 @@ describe('Pimple',function(){
assert.ok(container.protected instanceof Function);
assert.equal(container.protected(),5);
});
it('should throw exception when trying to get service that not exists', function () {
try {
container.get('none');
assert.fail('Expected exception');
} catch (e) {
assert(e instanceof Error);
assert.equal(e.message, 'Identifier "none" is not defined.');
}
});
it('should throw exception when trying to extend definition that not exists', function () {
try {
container.extend('params', function (params, container) {
return params;
});
assert.fail('Expected exception');
} catch (e) {
assert(e instanceof Error);
assert.equal(e.message, 'Identifier "params" is not defined.');
}
});
it(('should support definition extension'),function(){
container.set('params',function(){
return {
param1:1
}
});
container.set('params',container.extend('params',function(params,container){
container.extend('params',function(params,container){
params.param2=2;
container.set('param3',3);
return params;
}));
});
assert.equal(container.params.param2,2);
assert.equal(container.params.param1,1);
assert.equal(container.param3,3);
Expand Down