-
Notifications
You must be signed in to change notification settings - Fork 6
/
DependencyInjection.php
executable file
·63 lines (56 loc) · 1.99 KB
/
DependencyInjection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/**
* Description of DependencyInjection
*
* @author Loïc Frering <loic.frering@gmail.com>
*/
class LoSo_Zend_Controller_Action_Helper_DependencyInjection extends Zend_Controller_Action_Helper_Abstract
{
/**
*
* @var sfServiceContainer
*/
protected $_container;
public function preDispatch()
{
$actionController = $this->getActionController();
$this->_container = $actionController->getInvokeArg('bootstrap')->getContainer();
$r = new Zend_Reflection_Class($actionController);
$properties = $r->getProperties();
foreach($properties as $property) {
if($property->getDeclaringClass()->getName() == get_class($actionController)) {
if($property->getDocComment() && $property->getDocComment()->hasTag('Inject')) {
$injectTag = $property->getDocComment()->getTag('Inject');
$serviceName = $injectTag->getDescription();
if(empty($serviceName)) {
$serviceName = $this->_formatServiceName($property->getName());
}
if($this->_container->hasService($serviceName)) {
$property->setAccessible(true);
$property->setValue($actionController, $this->_container->getService($serviceName));
}
}
}
}
}
protected function _formatServiceName($serviceName)
{
if(strpos($serviceName, '_') === 0) {
$serviceName = substr($serviceName, 1);
}
return $serviceName;
}
public function direct($name)
{
if($this->_container->hasService($name)) {
return $this->_container->getService($name);
}
else if($this->_container->hasParameter($name)) {
return $this->_container->getParameter($name);
}
return null;
}
public function getContainer() {
return $this->_container;
}
}