developer.jelix.org is not used any more and exists only for history. Post new tickets on the Github account.
developer.jelix.org n'est plus utilisée, et existe uniquement pour son historique. Postez les nouveaux tickets sur le compte github.

Ticket #602: createBinded.patch

File createBinded.patch, 4.1 KB (added by doubleface, 13 years ago)
  • lib/jelix/utils/jBinding.class.php

    diff -r 08c931278a88 -r a029904e5ef5 lib/jelix/utils/jBinding.class.php
    a b  
    7272     *
    7373     * @return mixed
    7474     */
    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();
    8183        }
    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());
    8398    }
    8499
    85100    /**
  • lib/jelix/utils/jClasses.class.php

    diff -r 08c931278a88 -r a029904e5ef5 lib/jelix/utils/jClasses.class.php
    a b  
    4444     * @since 1.1
    4545     */
    4646    static public function createBinded($selector) {
    47         return self::getBinding($selector, false);
     47        return self::getBinding($selector)->getInstance(false);
    4848    }
    4949
    5050    /**
     
    106106     * @see jClasses::bind
    107107     * @since 1.1
    108108     */
    109     static public function getBinding($selector, $singleton=true) {
     109    static public function getBinding($selector) {
    110110        $osel = jSelectorFactory::create($selector, 'iface');
    111111        $s    = $osel->toString(true);
    112112
    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);
    119115        }
    120116
    121 
    122         return $binding;
     117        return self::$_bindings[$s];
    123118    }
    124119
    125120    /**
  • testapp/modules/jelix_tests/tests/utils.jclasses.html_cli.php

    diff -r 08c931278a88 -r a029904e5ef5 testapp/modules/jelix_tests/tests/utils.jclasses.html_cli.php
    a b  
    110110    public function testcreateBindedCalledTwice(){
    111111        $obj1 = jClasses::createBinded('class:jelix_tests~myclass');
    112112        $obj2 = jClasses::createBinded('class:jelix_tests~myclass');
     113        $this->assertTrue($obj1 instanceof myclass);
     114        $this->assertTrue($obj2 instanceof myclass);
    113115        $this->assertTrue($obj1 !== $obj2);
    114116
    115117        jClasses::bind('jelix_tests~test')->to('jelix_tests~myclass');
     
    122124    // then toInstance has no effect...
    123125    public function testCreateBindedWithToInstanceCalledTwice(){
    124126        $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);
    128132        $this->assertTrue($obj1 !== $obj2);
    129133    }
    130134}