1: <?php
2: /**
3: * A binder object for binding an interface to a factory class and method.
4: *
5: * An interface may be bound to a factory class. That factory class must
6: * provide a method or methods that accept a Horde_Injector, and return an
7: * object that satisfies the instance requirement. For example:
8: *
9: * <pre>
10: * class MyFactory {
11: * ...
12: * public function create(Horde_Injector $injector)
13: * {
14: * return new MyClass($injector->getInstance('Collaborator'), new MyOtherClass(17));
15: * }
16: * ...
17: * }
18: * </pre>
19: *
20: * @author Bob Mckee <bmckee@bywires.com>
21: * @author James Pepin <james@jamespepin.com>
22: * @category Horde
23: * @package Injector
24: */
25: class Horde_Injector_Binder_Factory implements Horde_Injector_Binder
26: {
27: /**
28: * TODO
29: *
30: * @var string
31: */
32: private $_factory;
33:
34: /**
35: * TODO
36: *
37: * @var string
38: */
39: private $_method;
40:
41: /**
42: * Create a new Horde_Injector_Binder_Factory instance.
43: *
44: * @param string $factory The factory class to use for creating objects.
45: * @param string $method The method on that factory to use for creating
46: * objects.
47: */
48: public function __construct($factory, $method)
49: {
50: $this->_factory = $factory;
51: $this->_method = $method;
52: }
53:
54: /**
55: * TODO
56: *
57: * @param Horde_Injector_Binder $otherBinder TODO
58: *
59: * @return boolean Equality.
60: */
61: public function equals(Horde_Injector_Binder $otherBinder)
62: {
63: return (($otherBinder instanceof Horde_Injector_Binder_Factory) &&
64: ($otherBinder->getFactory() == $this->_factory) &&
65: ($otherBinder->getMethod() == $this->_method));
66: }
67:
68: /**
69: * Get the factory classname that this binder was bound to.
70: *
71: * @return string The factory classname this binder is bound to.
72: */
73: public function getFactory()
74: {
75: return $this->_factory;
76: }
77:
78: /**
79: * Get the method that this binder was bound to.
80: *
81: * @return string The method this binder is bound to.
82: */
83: public function getMethod()
84: {
85: return $this->_method;
86: }
87:
88: /**
89: * Create instance using a factory method
90: *
91: * If the factory depends on a Horde_Injector we want to limit its scope
92: * so it cannot change anything that effects any higher-level scope. A
93: * factory should not have the responsibility of making a higher-level
94: * scope change.
95: * To enforce this we create a new child Horde_Injector. When a
96: * Horde_Injector is requested from a Horde_Injector it will return
97: * itself. This means that the factory will only ever be able to work on
98: * the child Horde_Injector we give it now.
99: *
100: * @param Horde_Injector $injector Injector object.
101: *
102: * @return TODO
103: */
104: public function create(Horde_Injector $injector)
105: {
106: $childInjector = $injector->createChildInjector();
107:
108: /* We use getInstance() here because we don't want to have to create
109: * this factory more than one time to create more objects of this
110: * type. */
111: return $childInjector->getInstance($this->_factory)->{$this->_method}($childInjector);
112: }
113:
114: }
115: