forked from Enrise/Glitch_Lib
/
HelperBroker.php
84 lines (71 loc) · 2.1 KB
/
HelperBroker.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
class Glitch_Controller_Response_Renderer_HelperBroker
{
protected $_helpers;
protected static $_staticHelpers;
protected $_shortcuts = array(
'_' => 'translate'
);
public function __construct($getStatic = true)
{
if($getStatic) {
$this->_helpers = static::_getStaticHelpers();
} else {
$this->_helpers = static::_initStaticHelpers();
}
}
public function __call($method, $args)
{
$helper = $this->_helpers[$method];
return call_user_func_array($helper, $args);
}
public static function __callstatic($method, $args)
{
$helpers = static::_getStaticHelpers();
return call_user_func_array($helpers, $args);
}
protected static function _initStaticHelpers()
{
return array(
'escape' =>
function($string)
{
return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
},
'translate' =>
function($string)
{
$options = func_get_args();
array_shift($options);
return Zend_Layout::getMvcInstance()
->getView()
->getHelper('translate')->translate($string, $options);
}
);
}
protected static function _getStaticHelpers()
{
if (static::$_staticHelpers == null) {
static::$_staticHelpers = static::_initStaticHelpers();
}
return static::$_staticHelpers;
}
/**
* @return array
* @throws \RuntimeException If a defined shortcut is no closure
*/
public function getShortCuts()
{
$out = array();
foreach ($this->_shortcuts as $name => $value) {
$helper = $this->_helpers[$value];
if (!is_callable($helper) || !is_object($helper)) {
throw new \RuntimeException(
'All shortcut helpers must be closures but '.$name.' wasn\'t'
);
}
$out[$name] = $this->_helpers[$value];
}
return $out;
}
}