diff --git a/README.md b/README.md index f36d462..c5283df 100644 --- a/README.md +++ b/README.md @@ -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) { diff --git a/pimple.js b/pimple.js index 0e46434..399c1fc 100644 --- a/pimple.js +++ b/pimple.js @@ -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); } @@ -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){ @@ -125,7 +129,7 @@ this.define('pimple',[],function(){return self.Pimple;}); } //CommonJS - if(module && module.exports){ + if(typeof exports !== 'undefined' && module && module.exports){ module.exports = this.Pimple; } }).call(this); diff --git a/pimple.js.map b/pimple.js.map index f15c873..5b31fc3 100644 --- a/pimple.js.map +++ b/pimple.js.map @@ -1 +1 @@ -{"version":3,"file":"pimple.min.js","sources":["pimple.js"],"names":["self","_isFunction","reservedProperties","this","object","Function","Pimple","services","_definitions","_raw","Object","keys","forEach","service","set","prototype","name","s","cached","pimple","undefined","indexOf","defineProperty","get","e","factory","function_","protect","extend","serviceName","def","container","raw","register","provider","define","module","exports","call"],"mappings":";;;;;;CAQA,WACI,YACA,IAAIA,MAAKC,YAAYC,kBACrBF,MAAKG,IAMLF,aAAY,SAASG,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AAKG,OAAOS,UAAUa,OAAO,SAASC,YAAYhB,SAC9C,GAAIiB,KAAI9B,KAAKG,IACb,IAAGA,KAAKK,aAAaqB,eAAeT,UAAU,CAC1CU,IAAI9B,KAAKQ,aAAaqB,YACtB,OAAO,UAASE,WACZ,GAAGD,cAAezB,UAAS,CACvByB,IAAIA,IAAIC,WAEZ,MAAOlB,SAAQiB,IAAIC,aAK/B5B,MAAKG,OAAOS,UAAUiB,IAAI,SAAShB,MAC/B,MAAOb,MAAKM,KAAKO,MAGrBb,MAAKG,OAAOS,UAAUkB,SAAS,SAASC,UACpC,GAAGA,SAASD,mBAAoB5B,UAAS,CACrC6B,SAASD,SAAS9B,UAChB,IAAG+B,mBAAoB7B,UAAS,CAClC6B,SAAS/B,MAEb,MAAOA,MAGX,IAAGA,KAAKgC,iBAAkB9B,UAAS,CAC/BF,KAAKgC,OAAO,YAAY,WAAW,MAAOnC,MAAKM,SAGnD,GAAG8B,QAAUA,OAAOC,QAAQ,CACxBD,OAAOC,QAAUlC,KAAKG,SAE3BgC,KAAKnC"} \ No newline at end of file +{"version":3,"file":"pimple.min.js","sources":["pimple.js"],"names":["self","_isFunction","reservedProperties","this","object","Function","Pimple","services","_definitions","_raw","Object","keys","forEach","service","set","prototype","name","s","cached","pimple","undefined","indexOf","defineProperty","get","e","factory","function_","Error","replace","protect","extend","serviceName","def","raw","register","provider","define","exports","module","call"],"mappings":";;;;;;CAQA,WACI,YACA,IAAIA,MAAKC,YAAYC,kBACrBF,MAAKG,IAMLF,aAAY,SAASG,QACjB,MAAOA,kBAAkBC,UAE7BH,qBAAoB,MAAM,MAAM,UAAU,MAAM,UAAU,QAAQ,WAAW,cAAc,YAO3FC,MAAKG,OAAQ,QAASA,QAAOC,UACzB,KAAKJ,eAAgBG,SAAQ,CACzB,MAAO,IAAIA,QAAOC,UAEtBJ,KAAKK,eACLL,MAAKM,OACL,IAAGF,SAAS,CACRG,OAAOC,KAAKJ,UAAUK,QAAQ,SAASC,SACnCV,KA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aAAauB,YACtB,IAAGC,cAAe3B,UAAS,CACvB2B,IAAIA,IAAI7B,MAEZU,QAAQmB,IAAI7B,KAEnB,OAAOA,MAGJA,MAAKG,OAAOS,UAAUkB,IAAI,SAASjB,MAC/B,MAAOb,MAAKM,KAAKO,MAGrBb,MAAKG,OAAOS,UAAUmB,SAAS,SAASC,UACpC,GAAGA,SAASD,mBAAoB7B,UAAS,CACrC8B,SAASD,SAAS/B,UAChB,IAAGgC,mBAAoB9B,UAAS,CAClC8B,SAAShC,MAEb,MAAOA,MAGX,IAAGA,KAAKiC,iBAAkB/B,UAAS,CAC/BF,KAAKiC,OAAO,YAAY,WAAW,MAAOpC,MAAKM,SAGnD,SAAU+B,WAAY,aAAeC,QAAUA,OAAOD,QAAQ,CAC1DC,OAAOD,QAAUlC,KAAKG,UAE3BiC,KAAKpC"} \ No newline at end of file diff --git a/pimple.min.js b/pimple.min.js index 81f469a..8d24747 100644 --- a/pimple.min.js +++ b/pimple.min.js @@ -4,5 +4,5 @@ * @license LGPL * @version 2.0.0 */ -!function(){"use strict";var self,_isFunction,reservedProperties;self=this;_isFunction=function(object){return object instanceof Function};reservedProperties=["get","set","factory","raw","protect","share","toString","constructor","prototype"];this.Pimple=function Pimple(services){if(!(this instanceof Pimple)){return new Pimple(services)}this._definitions={};this._raw={};if(services){Object.keys(services).forEach(function(service){this.set(service,services[service])},this)}};this.Pimple.prototype.set=function(name,service){var s;this._raw[name]=service;if(service instanceof Function){s=function(){var cached;return function(pimple){if(cached===undefined){cached=service(pimple)}return cached}}()}else{s=service}this._definitions[name]=s;try{if(reservedProperties.indexOf(name)===-1){Object.defineProperty(this,name,{get:function(){return this.get(name)}})}}catch(e){}return this};this.Pimple.prototype.factory=function(name,function_){var self=this;this._raw[name]=function_;this._definitions[name]=function_;try{if(reservedProperties.indexOf(name)===-1){Object.defineProperty(this,name,{get:function(){return this.get(name)}})}}catch(e){}};this.Pimple.prototype.get=function(name){if(this._definitions[name]instanceof Function){return this._definitions[name](this)}return this._definitions[name]};this.Pimple.prototype.protect=function(service){return function(){return 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)}}};this.Pimple.prototype.raw=function(name){return this._raw[name]};this.Pimple.prototype.register=function(provider){if(provider.register instanceof Function){provider.register(this)}else if(provider instanceof Function){provider(this)}return this};if(this.define instanceof Function){this.define("pimple",[],function(){return self.Pimple})}if(module&&module.exports){module.exports=this.Pimple}}.call(this); +(function(){"use strict";var self,_isFunction,reservedProperties;self=this;_isFunction=function(object){return object instanceof Function};reservedProperties=["get","set","factory","raw","protect","share","toString","constructor","prototype"];this.Pimple=function Pimple(services){if(!(this instanceof Pimple)){return new Pimple(services)}this._definitions={};this._raw={};if(services){Object.keys(services).forEach(function(service){this.set(service,services[service])},this)}};this.Pimple.prototype.set=function(name,service){var s;this._raw[name]=service;if(service instanceof Function){s=function(){var cached;return function(pimple){if(cached===undefined){cached=service(pimple)}return cached}}()}else{s=service}this._definitions[name]=s;try{if(reservedProperties.indexOf(name)===-1){Object.defineProperty(this,name,{get:function(){return this.get(name)}})}}catch(e){}return this};this.Pimple.prototype.factory=function(name,function_){var self=this;this._raw[name]=function_;this._definitions[name]=function_;try{if(reservedProperties.indexOf(name)===-1){Object.defineProperty(this,name,{get:function(){return this.get(name)}})}}catch(e){}};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)}return this._definitions[name]};this.Pimple.prototype.protect=function(service){return function(){return service}};this.Pimple.prototype.extend=function(serviceName,service){var def,self=this;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};this.Pimple.prototype.raw=function(name){return this._raw[name]};this.Pimple.prototype.register=function(provider){if(provider.register instanceof Function){provider.register(this)}else if(provider instanceof Function){provider(this)}return this};if(this.define instanceof Function){this.define("pimple",[],function(){return self.Pimple})}if(typeof exports!=="undefined"&&module&&module.exports){module.exports=this.Pimple}}).call(this); //# sourceMappingURL=pimple.js.map \ No newline at end of file diff --git a/test/pimple.test.js b/test/pimple.test.js index 7a1ccae..4e46933 100644 --- a/test/pimple.test.js +++ b/test/pimple.test.js @@ -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);