1: <?php
2: /**
3: * This is a simple class that uses reflection to figure out the dependencies of
4: * a method and attempts to return them using the Injector instance.
5: *
6: * @author Bob Mckee <bmckee@bywires.com>
7: * @author James Pepin <james@jamespepin.com>
8: * @author Chuck Hagenbuch <chuck@horde.org>
9: * @category Horde
10: * @package Injector
11: */
12: class Horde_Injector_DependencyFinder
13: {
14: /**
15: * TODO
16: *
17: * @param Horde_Injector $injector TODO
18: * @param ReflectionMethod $method TODO
19: *
20: * @return array TODO
21: * @throws Horde_Injector_Exception
22: */
23: public function getMethodDependencies(Horde_Injector $injector,
24: ReflectionMethod $method)
25: {
26: $dependencies = array();
27:
28: try {
29: foreach ($method->getParameters() as $parameter) {
30: $dependencies[] = $this->getParameterDependency($injector, $parameter);
31: }
32: } catch (Horde_Injector_Exception $e) {
33: throw new Horde_Injector_Exception("$method has unfulfilled dependencies ($parameter)", 0, $e);
34: }
35:
36: return $dependencies;
37: }
38:
39: /**
40: * TODO
41: *
42: * @param Horde_Injector $injector TODO
43: * @param ReflectionParameter $method TODO
44: *
45: * @return mixed TODO
46: * @throws Horde_Injector_Exception
47: */
48: public function getParameterDependency(Horde_Injector $injector,
49: ReflectionParameter $parameter)
50: {
51: if ($parameter->getClass()) {
52: return $injector->getInstance($parameter->getClass()->getName());
53: } elseif ($parameter->isOptional()) {
54: return $parameter->getDefaultValue();
55: }
56:
57: throw new Horde_Injector_Exception("Untyped parameter \$" . $parameter->getName() . "can't be fulfilled");
58: }
59:
60: }
61: