root/trunk/src/main/php/net/stubbles/ioc/stubBinder.php

Revision 1371, 2.5 kB (checked in by mikey, 4 months ago)

finished enhancement #97: added tests for new use cases and some more tests for better code coverage of the ioc container

Line 
1 <?php
2 /**
3  * Binder for the IoC functionality.
4  *
5  * Used to create various Binding instance used by
6  * the same injector.
7  *
8  * @author      Stephan Schmidt <schst@stubbles.net>
9  * @package     stubbles
10  * @subpackage  ioc
11  */
12 stubClassLoader::load('net::stubbles::reflection::stubReflectionClass',
13                       'net::stubbles::ioc::stubBinding',
14                       'net::stubbles::ioc::stubClassBinding',
15                       'net::stubbles::ioc::stubConstantBinding',
16                       'net::stubbles::ioc::stubInjector',
17                       'net::stubbles::ioc::stubBindingScope',
18                       'net::stubbles::ioc::stubBindingScopes',
19                       'net::stubbles::ioc::annotations::stubInjectAnnotation',
20                       'net::stubbles::ioc::annotations::stubSingletonAnnotation',
21                       'net::stubbles::ioc::annotations::stubNamedAnnotation',
22                       'net::stubbles::ioc::annotations::stubImplementedByAnnotation');
23 /**
24  * Binder for the IoC functionality.
25  *
26  * Used to create various Binding instance used by
27  * the same injector.
28  *
29  * @package     stubbles
30  * @subpackage  ioc
31  */
32 class stubBinder extends stubBaseObject
33 {
34     /**
35      * Key for storing the binder in the registry
36      */
37     const REGISTRY_KEY = 'net.stubbles.ioc.stubBinder';
38
39     /**
40      * Injector used by this binder
41      *
42      * @var  stubInjector
43      */
44     protected $injector = null;
45
46     /**
47      * Create a new binder
48      *
49      * @param  stubInjector  $injector
50      */
51     public function __construct(stubInjector $injector = null)
52     {
53         if ($injector === null) {
54             $this->injector = new stubInjector();
55         } else {
56             $this->injector = $injector;
57         }
58     }
59
60     /**
61      * Bind a new interface to a class
62      *
63      * @param   string  $interface
64      * @return  stubClassBinding
65      */
66     public function bind($interface)
67     {
68         $binding = new stubClassBinding($this->injector, $interface);
69         $this->injector->addBinding($binding);
70         return $binding;
71     }
72
73     /**
74      * Bind a new constant
75      *
76      * @return  stubConstantBinding
77      */
78     public function bindConstant()
79     {
80         $binding = new stubConstantBinding();
81         $this->injector->addBinding($binding);
82         return $binding;
83     }
84
85     /**
86      * Get an injector for this binder
87      *
88      * @return  stubInjector
89      */
90     public function getInjector()
91     {
92         return $this->injector;
93     }
94 }
95 ?>
Note: See TracBrowser for help on using the browser.