Changeset 1178

Show
Ignore:
Timestamp:
12/23/07 00:05:32 (1 year ago)
Author:
mikey
Message:

improved the check for the index update

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/ioc/stubInjector.php

    r1176 r1178  
    1818 * @package     stubbles 
    1919 * @subpackage  ioc 
    20  * @todo        Improve the check for the index update 
    2120 */ 
    2221class stubInjector extends stubBaseObject implements stubClonable 
     
    2726     * @var array<stubBinding> 
    2827     */ 
    29     protected $bindings     = array(); 
     28    protected $bindings   = array(); 
    3029    /** 
    3130     * index for faster access to bindings 
    3231     * 
    33      * @var array<string,stubBinding> 
     32     * Do not access this array directly, use getIndex() instead. 
     33     * 
     34     * @var  array<string,stubBinding> 
     35     * @see  stubInjector::getIndex() 
    3436     */ 
    35     protected $bindingIndex = array(); 
     37    private $bindingIndex = array(); 
    3638 
    3739    /** 
     
    4446    public function getInstance($type, $name = null) 
    4547    { 
    46         if (empty($this->bindings) === false) { 
    47             $this->updateIndex(); 
    48         } 
    49          
    5048        $binding = $this->getBinding($type, $name); 
    5149        if (null !== $binding) { 
     
    7573 
    7674    /** 
    77      * updates the binding index 
    78      */ 
    79     protected function updateIndex() 
    80     { 
    81         foreach ($this->bindings as $binding) { 
    82             $this->bindingIndex[$binding->getKey()] = $binding; 
    83         } 
    84          
    85         $this->bindings = array(); 
    86     } 
    87  
    88     /** 
    8975     * adds a new binding to the injector 
    9076     * 
     
    10692    protected function getBinding($type, $name = null) 
    10793    { 
     94        $bindingIndex = $this->getIndex(); 
    10895        if (null !== $name) { 
    109             if (isset($this->bindingIndex[$type.'#'.$name]) === true) { 
    110                 return $this->bindingIndex[$type.'#'.$name]; 
     96            if (isset($bindingIndex[$type.'#'.$name]) === true) { 
     97                return $bindingIndex[$type.'#'.$name]; 
    11198            } 
    11299        } 
    113100         
    114         if (isset($this->bindingIndex[$type]) === true) { 
    115             return $this->bindingIndex[$type]; 
     101        if (isset($bindingIndex[$type]) === true) { 
     102            return $bindingIndex[$type]; 
    116103        } 
    117104         
    118105        return null; 
     106    } 
     107 
     108    /** 
     109     * returns the binding index 
     110     * 
     111     * @return  array<string,stubBinding> 
     112     */ 
     113    protected function getIndex() 
     114    { 
     115        if (empty($this->bindings) === true) { 
     116            return $this->bindingIndex; 
     117        } 
     118         
     119        foreach ($this->bindings as $binding) { 
     120            $this->bindingIndex[$binding->getKey()] = $binding; 
     121        } 
     122         
     123        $this->bindings = array(); 
     124        return $this->bindingIndex; 
    119125    } 
    120126 
     
    128134    public function hasBinding($type, $name = null) 
    129135    { 
    130         if (empty($this->bindings) === false) { 
    131             $this->updateIndex(); 
    132         } 
    133          
    134136        return ($this->getBinding($type, $name) != null); 
    135137    }