1 | <?php |
||
52 | class DependencyInjectionManager |
||
53 | { |
||
54 | /** |
||
55 | * @access protected |
||
56 | * @var Framework |
||
57 | */ |
||
58 | protected $framework; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $sharedInstances = array(); |
||
64 | |||
65 | /** |
||
66 | * Constructs the object |
||
67 | * |
||
68 | * @access public |
||
69 | * @param \Zepi\Turbo\Framework $framework |
||
70 | */ |
||
71 | 17 | public function __construct(Framework $framework) |
|
72 | { |
||
73 | 17 | $this->framework = $framework; |
|
74 | 17 | } |
|
75 | |||
76 | /** |
||
77 | * Initiates the given class name |
||
78 | * |
||
79 | * @param string $className |
||
80 | * @param array $additionalParameters |
||
81 | * @param boolean $shared |
||
82 | * @return object |
||
83 | */ |
||
84 | public function initiateObject($className, $additionalParameters = array(), $shared = false) |
||
85 | { |
||
86 | if (isset($this->sharedInstances[$className])) { |
||
87 | return $this->sharedInstances[$className]; |
||
88 | } |
||
89 | |||
90 | $reflection = new ReflectionClass($className); |
||
91 | |||
92 | if ($reflection->hasMethod('__construct')) { |
||
93 | $constructor = $reflection->getConstructor(); |
||
94 | $parameters = $this->prepareParameters($constructor, $additionalParameters); |
||
95 | |||
96 | $instance = $reflection->newInstanceArgs($parameters); |
||
97 | } else { |
||
98 | $instance = new $className(); |
||
99 | } |
||
100 | |||
101 | if ($shared) { |
||
102 | $this->sharedInstances[$className] = $instance; |
||
103 | } |
||
104 | |||
105 | return $instance; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Prepares the parameters for the given constructor |
||
110 | * |
||
111 | * @param \ReflectionMethod $constructor |
||
112 | * @param array $additionalParameters |
||
113 | * @return array |
||
114 | * |
||
115 | * @throws \Zepi\Turbo\Exception Cannot find correct value for parameter "{parameterName}" in class "{className}". |
||
116 | */ |
||
117 | protected function prepareParameters(ReflectionMethod $constructor, $additionalParameters) |
||
138 | |||
139 | /** |
||
140 | * Returns the instance for the given class |
||
141 | * |
||
142 | * @param \ReflectionClass $parameterClass |
||
143 | * @return null|object |
||
144 | */ |
||
145 | protected function getInstance(ReflectionClass $parameterClass) |
||
159 | |||
160 | /** |
||
161 | * Returns the instance for the given class if the class path |
||
162 | * starts with Zepi\Turbo. |
||
163 | * |
||
164 | * @param \ReflectionClass $parameterClass |
||
165 | * @return object |
||
166 | */ |
||
167 | protected function getFrameworkInstance(ReflectionClass $parameterClass) |
||
185 | |||
186 | /** |
||
187 | * Returns the framework manager for the given class name |
||
188 | * |
||
189 | * @param string $className |
||
190 | * @return object |
||
191 | * |
||
192 | * @throws \Zepi\Turbo\Exception Cannot find framework manager "{className}". |
||
193 | */ |
||
194 | protected function getFrameworkManager($className) |
||
226 | } |
||
227 |