Changeset 810

Show
Ignore:
Timestamp:
08/13/07 22:17:42 (1 year ago)
Author:
schst
Message:

New experimental features:

  • scoping
  • implicit bindings

This still needs a massive refactoring!

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/experiments/people/schst/ioc/simple.php

    r809 r810  
    3030} 
    3131 
     32class Window { 
     33} 
     34 
    3235class Goodyear implements Tire { 
    3336 
     
    5154    protected $tire; 
    5255    protected $driver; 
     56    protected $window; 
    5357 
    5458    /** 
     
    7074    public function setDriver(Person $driver) { 
    7175        $this->driver = $driver; 
     76    } 
     77 
     78    /** 
     79     * Set the window 
     80     * 
     81     * @param Window $concrete 
     82     * @Inject 
     83     */ 
     84    public function setWindow(Window $window) { 
     85        $this->window = $window; 
    7286    } 
    7387 
     
    95109$binder->bind('Person')->to('Schst'); 
    96110 
     111// This will bind the class 'Window' to 'Window' 
     112// It could be ommitted, as this will be done by implicit binding 
     113$binder->bind('Window'); 
     114 
    97115$vehicle = $injector->getInstance('Vehicle'); 
    98116$vehicle->getDriver()->sayHello(); 
  • trunk/src/main/php/net/stubbles/ioc/injection/stubBinder.php

    r809 r810  
    1616                      'net.stubbles.ioc.injection.stubBinding', 
    1717                      'net.stubbles.ioc.injection.stubInjector', 
     18                      'net.stubbles.ioc.injection.stubBindingScope', 
    1819                      'net.stubbles.ioc.injection.annotations.stubInjectAnnotation'); 
    1920/** 
  • trunk/src/main/php/net/stubbles/ioc/injection/stubBinding.php

    r809 r810  
    1616 * @package     stubbles 
    1717 * @subpackage  ioc_injection 
     18 * @todo        Introduce providers that create the concrete instances 
    1819 */ 
    1920class stubBinding { 
     
    4142 
    4243    /** 
     44     * Scope of the binding 
     45     * 
     46     * @var stubBindingScope 
     47     */ 
     48    protected $scope = null; 
     49 
     50    /** 
    4351     * Create a new binding 
    4452     * 
     
    4957        $this->injector = $injector; 
    5058        $this->key = $key; 
     59        $this->impl = $key; 
    5160    } 
    5261 
     
    6170 
    6271    /** 
     72     * Get the scope of the binding 
     73     * 
     74     * @return stubBindingScope 
     75     */ 
     76    public function getScope() { 
     77        return $this->scope; 
     78    } 
     79 
     80    /** 
    6381     * Set the concrete implementation 
    6482     * 
     
    6785    public function to($impl) { 
    6886        $this->impl = $impl; 
     87        return $this; 
     88    } 
     89 
     90    /** 
     91     * Set the scope 
     92     * 
     93     * @param stubBindingScope $scope 
     94     */ 
     95    public function in(stubBindingScope $scope) { 
     96        $this->scope = $scope; 
     97        return $this; 
    6998    } 
    7099 
     
    82111        $constructor = $this->impl->getConstructor(); 
    83112        if ($constructor === null) { 
    84             return $this->impl->newInstance(); 
    85         } 
    86  
    87         $paramValues = array(); 
    88         if (!$constructor->hasAnnotation('Inject')) { 
    89             $instance = $this->impl->newInstanceArgs($paramValues); 
     113            $instance = $this->impl->newInstance(); 
     114        } elseif (!$constructor->hasAnnotation('Inject')) { 
     115            $instance = $this->impl->newInstance(); 
    90116        } else { 
     117            $paramValues = array(); 
    91118            foreach ($constructor->getParameters() as $param) { 
    92119                $class = $param->getClass(); 
     
    97124 
    98125        foreach ($this->impl->getMethods() as $method) { 
     126            if (strncmp($method->getName(), '__', 2) === 0) { 
     127                continue; 
     128            } 
    99129            if (!$method->isPublic()) { 
    100130                continue; 
  • trunk/src/main/php/net/stubbles/ioc/injection/stubInjector.php

    r808 r810  
    3333     * Get an instance 
    3434     * 
    35      * @param string $key 
    36      * @return object 
     35     * @param   string $key 
     36     * @return  object 
     37     * @todo    build an index of the bindings 
    3738     */ 
    3839    public function getInstance($key) { 
    3940        foreach ($this->bindings as $binding) { 
    4041            if ($binding->getKey() === $key) { 
    41                 return $binding->getInstance(); 
     42                $scope = $binding->getScope(); 
     43                if ($scope != null) { 
     44                    return $scope->getInstance($key, $binding); 
     45                } else { 
     46                    return $binding->getInstance(); 
     47                } 
    4248            } 
     49        } 
     50 
     51        // try implicit binding 
     52        // TODO: Find a useful location for this code 
     53        $clazz = new stubReflectionClass($key); 
     54        if (!$clazz->isInterface()) { 
     55            $tmp = new stubBinding($this, $key); 
     56            $this->bindings[] = $tmp; 
     57            return $tmp->getInstance(); 
    4358        } 
    4459        throw new stubException('No binding for ' . $key . ' defined'); 
  • trunk/src/main/php/net/stubbles/stubClassLoader.php

    r808 r810  
    210210            } 
    211211 
    212             if ((@include $uri) == false) { 
     212            if ((include $uri) == false) { 
    213213                throw new stubClassNotFoundException($fqClassName); 
    214214            }