Changeset 822

Show
Ignore:
Timestamp:
08/14/07 20:16:49 (1 year ago)
Author:
schst
Message:

Improved performance of the binding lookups

Files:

Legend:

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

    r820 r822  
    2424 
    2525    /** 
    26      * All bindings used by the injector 
     26     * All bindings used by the injector, that are not yet in the index 
    2727     * 
    2828     * @var array<stubBinding> 
    2929     */ 
    3030    protected $bindings = array(); 
     31 
     32    /** 
     33     * Index to faster access the bindings 
     34     * 
     35     * @var array<string,stubBinding> 
     36     */ 
     37    protected $bindingIndex = array(); 
    3138 
    3239    /** 
     
    3845     */ 
    3946    public function getInstance($key) { 
    40         foreach ($this->bindings as $binding) { 
    41             if ($binding->getKey() === $key) { 
    42                 $scope = $binding->getScope(); 
    43                 if ($scope != null) { 
    44                     return $scope->getInstance($key, $binding); 
    45                 } else { 
    46                     return $binding->getInstance(); 
    47                 } 
     47        if (!empty($this->bindings)) { 
     48            $this->updateIndex(); 
     49        } 
     50 
     51        if (isset($this->bindingIndex[$key])) { 
     52            $binding = $this->bindingIndex[$key]; 
     53            $scope = $binding->getScope(); 
     54            if ($scope != null) { 
     55                return $scope->getInstance($key, $binding); 
     56            } else { 
     57                return $binding->getInstance(); 
    4858            } 
    4959        } 
     
    5565            $tmp = new stubBinding($this, $key); 
    5666            $tmp->to($implementedBy->getDefaultImplementation()); 
    57             $this->bindings[] = $tmp
     67            $this->addBinding($tmp)
    5868            return $tmp->getInstance(); 
    5969        } 
     
    6373        if (!$clazz->isInterface()) { 
    6474            $tmp = new stubBinding($this, $key); 
    65             $this->bindings[] = $tmp
     75            $this->addBinding($tmp)
    6676            return $tmp->getInstance(); 
    6777        } 
    6878        throw new stubException('No binding for ' . $key . ' defined'); 
     79    } 
     80 
     81    /** 
     82     * Update the binding index 
     83     */ 
     84    protected function updateIndex() { 
     85        foreach ($this->bindings as $binding) { 
     86            $this->bindingIndex[$this->createKey($binding)] = $binding; 
     87        } 
     88        $this->bindings = array(); 
     89    } 
     90 
     91    /** 
     92     * Creates a unique key for a binding 
     93     * 
     94     * @param stubBinding $binding 
     95     * @todo  Key must also contain @Named() annotation value 
     96     */ 
     97    protected function createKey(stubBinding $binding) { 
     98        $interface = $binding->getKey(); 
     99        if ($interface instanceof stubReflectionClass) { 
     100            $interface = $interface->getName(); 
     101        } 
     102        return $interface; 
    69103    } 
    70104