1: <?php
2:
3: /**
4: * Picon Framework
5: * http://code.google.com/p/picon-framework/
6: *
7: * Copyright (C) 2011-2012 Martin Cassidy <martin.cassidy@webquub.com>
8:
9: * Picon Framework is free software: you can redistribute it and/or modify
10: * it under the terms of the GNU General Public License as published by
11: * the Free Software Foundation, either version 3 of the License, or
12: * (at your option) any later version.
13:
14: * Picon Framework is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: * General Public License for more details.
18:
19: * You should have received a copy of the GNU General Public License
20: * along with Picon Framework. If not, see <http://www.gnu.org/licenses/>.
21: * */
22:
23: namespace picon;
24:
25: /**
26: * Dependency injector for resources. All resources are inject after instantiation.
27: * Although any object can be manually injected at any time.
28: *
29: * @author Martin Cassidy
30: * @package context
31: */
32: class Injector
33: {
34: private $context;
35: private static $injector;
36:
37: public function __construct(ApplicationContext $context)
38: {
39: $this->context = $context;
40: Injector::$injector = $this;
41: }
42:
43: /**
44: * @param type $object
45: */
46: public function inject(&$object, $reflection = null)
47: {
48: if($reflection==null)
49: {
50: $reflection = new \ReflectionAnnotatedClass($object);
51: }
52: $properties = $reflection->getProperties();
53:
54: foreach($properties as $property)
55: {
56: if($property->hasAnnotation("Resource"))
57: {
58: $annotation = $property->getAnnotation("Resource");
59: $resource = $property->getName();
60:
61: if(!empty($annotation->name))
62: {
63: $resource = $annotation->name;
64: }
65:
66: $property->setAccessible(true);
67: $property->setValue($object, $this->context->getResource($resource));
68: }
69: }
70:
71: $parent = $reflection->getParentClass();
72: if($parent!=null)
73: {
74: $this->inject($object, $parent);
75: }
76: }
77:
78: public static function get()
79: {
80: return Injector::$injector;
81: }
82: }
83:
84: ?>
85: