From f6c1afc9facbb2f202086f05842dd1ad9902de97 Mon Sep 17 00:00:00 2001 From: Aubin VIGHIER Date: Sat, 27 Dec 2014 23:15:32 +0000 Subject: [PATCH 1/6] Fix contract extend --- pimple.js | 17 +++++++++-------- test/pimple.test.js | 4 ++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/pimple.js b/pimple.js index 0e46434..fd6cea8 100644 --- a/pimple.js +++ b/pimple.js @@ -97,15 +97,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){ + return this; + } + 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){ diff --git a/test/pimple.test.js b/test/pimple.test.js index 7a1ccae..2d54a7d 100644 --- a/test/pimple.test.js +++ b/test/pimple.test.js @@ -57,11 +57,11 @@ describe('Pimple',function(){ 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); From b6eb3559713e9d79c23dd2e9166e7abe5ef3473e Mon Sep 17 00:00:00 2001 From: Aubin VIGHIER Date: Sat, 27 Dec 2014 23:21:57 +0000 Subject: [PATCH 2/6] Fix extend : now extend throws Error if extending unexisting service --- pimple.js | 2 +- test/pimple.test.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pimple.js b/pimple.js index fd6cea8..55d60d3 100644 --- a/pimple.js +++ b/pimple.js @@ -98,7 +98,7 @@ this.Pimple.prototype.extend=function(serviceName,service){ var def,self=this; if(this._definitions[serviceName]===undefined){ - return this; + throw new Error('Identifier "%s" is not defined.'.replace('%s', serviceName)); } def=self._definitions[serviceName]; if(def instanceof Function){ diff --git a/test/pimple.test.js b/test/pimple.test.js index 2d54a7d..bc88aee 100644 --- a/test/pimple.test.js +++ b/test/pimple.test.js @@ -51,6 +51,17 @@ describe('Pimple',function(){ assert.ok(container.protected instanceof Function); assert.equal(container.protected(),5); }); + 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 { From 35190d3ce7be0584607f186f60a6c037445a057f Mon Sep 17 00:00:00 2001 From: Aubin VIGHIER Date: Sat, 27 Dec 2014 23:23:36 +0000 Subject: [PATCH 3/6] Fix "\" in documentation (from PHP) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) { From f9eea034e08bb2ffb6aafaf31ed56d359d472141 Mon Sep 17 00:00:00 2001 From: Aubin VIGHIER Date: Sat, 27 Dec 2014 23:26:08 +0000 Subject: [PATCH 4/6] Project builded --- pimple.js.map | 2 +- pimple.min.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pimple.js.map b/pimple.js.map index f15c873..10b0862 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,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UY,QAAQ,SAASd,SACnC,MAAO,YACH,MAAOA,UAIfV,MAAKG,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_","protect","extend","serviceName","def","Error","replace","raw","register","provider","define","module","exports","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,CAC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AAIA,IAAI3B,MAEZU,QAAQiB,IAAI3B,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,GAAG+B,QAAUA,OAAOC,QAAQ,CACxBD,OAAOC,QAAUnC,KAAKG,UAE3BiC,KAAKpC"} \ No newline at end of file diff --git a/pimple.min.js b/pimple.min.js index 81f469a..67f308d 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]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(module&&module.exports){module.exports=this.Pimple}}).call(this); //# sourceMappingURL=pimple.js.map \ No newline at end of file From 1db56b7a00addb082ffa665ca72c6c097673878d Mon Sep 17 00:00:00 2001 From: Aubin VIGHIER Date: Sat, 27 Dec 2014 23:38:18 +0000 Subject: [PATCH 5/6] Added Error when trying to get service that not exists (silexphp/Pimple behavior) --- pimple.js | 3 +++ pimple.js.map | 2 +- pimple.min.js | 2 +- test/pimple.test.js | 9 +++++++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pimple.js b/pimple.js index 55d60d3..2bcf432 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); } diff --git a/pimple.js.map b/pimple.js.map index 10b0862..8bfb180 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","Error","replace","raw","register","provider","define","module","exports","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,KAAKW,IAAID,QAAQN,SAASM,WAC5BV,OAIVA,MAAKG,OAAOS,UAAUD,IAAI,SAASE,KAAKH,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Bb,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,GAAG+B,QAAUA,OAAOC,QAAQ,CACxBD,OAAOC,QAAUnC,KAAKG,UAE3BiC,KAAKpC"} \ 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","module","exports","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,KAAKW,IAAID,QAAQN,SAASM,WAC5BV,OAIVA,MAAKG,OAAOS,UAAUD,IAAI,SAASE,KAAKH,SACpC,GAAII,EACJd,MAAKM,KAAKO,MAAMH,OAChB,IAAGA,kBAAmBR,UAAS,CAC3BY,EAAK,WACD,GAAIC,OACJ,OAAO,UAASC,QACZ,GAAGD,SAAWE,UAAU,CACpBF,OAASL,QAAQM,QAErB,MAAOD,gBAGd,CACDD,EAAEJ,QAENV,KAAKK,aAAaQ,MAAMC,CACxB,KACI,GAAGf,mB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MAAOA,MAGX,IAAGA,KAAKiC,iBAAkB/B,UAAS,CAC/BF,KAAKiC,OAAO,YAAY,WAAW,MAAOpC,MAAKM,SAGnD,GAAG+B,QAAUA,OAAOC,QAAQ,CACxBD,OAAOC,QAAUnC,KAAKG,UAE3BiC,KAAKpC"} \ No newline at end of file diff --git a/pimple.min.js b/pimple.min.js index 67f308d..d840709 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){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(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(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 bc88aee..4e46933 100644 --- a/test/pimple.test.js +++ b/test/pimple.test.js @@ -51,6 +51,15 @@ 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) { From 06fe049dcd4d3124152b4b6e469e7975bc852dd6 Mon Sep 17 00:00:00 2001 From: Nestor Toro Date: Tue, 28 Oct 2014 22:38:39 -0700 Subject: [PATCH 6/6] Fixes bug #3 "pimple and requirejs" which throws a ReferenceError in browser environment Uncaught ReferenceError: module is not defined pimple.js:128 --- pimple.js | 2 +- pimple.js.map | 2 +- pimple.min.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pimple.js b/pimple.js index 2bcf432..399c1fc 100644 --- a/pimple.js +++ b/pimple.js @@ -129,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 8bfb180..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_","Error","replace","protect","extend","serviceName","def","raw","register","provider","define","module","exports","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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KAAMG,cAEpEC,IAAIhC,KAAKQ,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,GAAG+B,QAAUA,OAAOC,QAAQ,CACxBD,OAAOC,QAAUnC,KAAKG,UAE3BiC,KAAKpC"} \ 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,KAAKW,IAAID,QAAQN,SAASM,WAC5BV,OAIVA,MAAKG,OAAOS,UAAUD,IAAI,SAASE,KAAKH,SACpC,GAAII,EACJd,MAAKM,KAAKO,MAAM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,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 d840709..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]===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(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