Ticket #602: createBinded.patch
| File createBinded.patch, 4.1 kB (added by doubleface, 8 months ago) |
|---|
-
a/lib/jelix/utils/jBinding.class.php
old new 72 72 * 73 73 * @return mixed 74 74 */ 75 public function getInstance() { 76 if ($this->instance === null) { 77 if ($this->toSelector === null) { 78 $this->toSelector = $this->_getClassSelector(); 79 } 80 $this->instance = jClasses::create($this->toSelector->toString()); 75 public function getInstance($singleton=true) { 76 $instance = null; 77 if (true === $singleton && $this->instance !== null) { 78 $instance = $this->instance; 79 } elseif (true === $singleton && $this->instance === null) { 80 $instance = $this->instance = $this->_createInstance(); 81 } else { // all cases with $singleton === false 82 $instance = $this->_createInstance(); 81 83 } 82 return $this->instance; 84 return $instance; 85 } 86 87 /** 88 * Create the binded selector if not initialzed yet 89 * 90 * @return mixed 91 */ 92 protected function _createInstance() { 93 if ($this->toSelector === null) { 94 $this->instance = null; 95 $this->toSelector = $this->_getClassSelector(); 96 } 97 return jClasses::create($this->toSelector->toString()); 83 98 } 84 99 85 100 /** -
a/lib/jelix/utils/jClasses.class.php
old new 44 44 * @since 1.1 45 45 */ 46 46 static public function createBinded($selector) { 47 return self::getBinding($selector ,false);47 return self::getBinding($selector)->getInstance(false); 48 48 } 49 49 50 50 /** … … 106 106 * @see jClasses::bind 107 107 * @since 1.1 108 108 */ 109 static public function getBinding($selector , $singleton=true) {109 static public function getBinding($selector) { 110 110 $osel = jSelectorFactory::create($selector, 'iface'); 111 111 $s = $osel->toString(true); 112 112 113 $binding = null; 114 if ($singleton === false || !isset(self::$_bindings[$s])) { 115 $binding = new jBinding($osel); 116 if ($singleton === true) self::$_bindings[$s] = $binding; 117 } else { 118 $binding = self::$_bindings[$s]; 113 if (!isset(self::$_bindings[$s])) { 114 self::$_bindings[$s] = new jBinding($osel); 119 115 } 120 116 121 122 return $binding; 117 return self::$_bindings[$s]; 123 118 } 124 119 125 120 /** -
a/testapp/modules/jelix_tests/tests/utils.jclasses.html_cli.php
old new 110 110 public function testcreateBindedCalledTwice(){ 111 111 $obj1 = jClasses::createBinded('class:jelix_tests~myclass'); 112 112 $obj2 = jClasses::createBinded('class:jelix_tests~myclass'); 113 $this->assertTrue($obj1 instanceof myclass); 114 $this->assertTrue($obj2 instanceof myclass); 113 115 $this->assertTrue($obj1 !== $obj2); 114 116 115 117 jClasses::bind('jelix_tests~test')->to('jelix_tests~myclass'); … … 122 124 // then toInstance has no effect... 123 125 public function testCreateBindedWithToInstanceCalledTwice(){ 124 126 $instance = new StdClass(); 125 jClasses::bind('jelix_tests~test')->toInstance($instance); 126 $obj1 = jClasses::createBinded('jelix_tests~test'); 127 $obj2 = jClasses::createBinded('jelix_tests~test'); 127 jClasses::bind('class:jelix_tests~myclass')->toInstance($instance); 128 $obj1 = jClasses::createBinded('class:jelix_tests~myclass'); 129 $obj2 = jClasses::createBinded('class:jelix_tests~myclass'); 130 $this->assertTrue($obj1 instanceof myclass); 131 $this->assertTrue($obj2 instanceof myclass); 128 132 $this->assertTrue($obj1 !== $obj2); 129 133 } 130 134 }
