|
1
|
|
|
<?php namespace Knot\Dict; |
|
2
|
|
|
|
|
3
|
|
|
use Knot\Dict\Helpers\HelperInterface; |
|
4
|
|
|
use Knot\Exceptions\FunctionExecuteException; |
|
5
|
|
|
use Knot\Exceptions\WrongFunctionException; |
|
6
|
|
|
|
|
7
|
|
|
class HelperManager { |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Helper Manager Object. |
|
11
|
|
|
*/ |
|
12
|
|
|
protected static $instance = null; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Helper list. |
|
16
|
|
|
*/ |
|
17
|
|
|
private $helperList = [ ]; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Functions Routes. |
|
21
|
|
|
*/ |
|
22
|
|
|
private $functionRoutes = [ ]; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Return instance of Self! |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function getInstance() |
|
29
|
|
|
{ |
|
30
|
|
|
if ( ! self::$instance ) |
|
31
|
|
|
{ |
|
32
|
|
|
self::$instance = new self(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return self::$instance; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
public function __construct() |
|
40
|
|
|
{ |
|
41
|
|
|
$baseHelpers = self::getBaseHelpers(); |
|
42
|
|
|
|
|
43
|
|
|
// Load core helpers! |
|
44
|
|
|
foreach ($baseHelpers as $helperObjectAddress) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->loadHelper(new $helperObjectAddress($this)); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
public static function getBaseHelpers() |
|
52
|
|
|
{ |
|
53
|
|
|
return [ |
|
54
|
|
|
"\\Knot\\Dict\\Helpers\\PHPArrayChangerHelper", |
|
55
|
|
|
"\\Knot\\Dict\\Helpers\\PHPArrayEqualizerHelper", |
|
56
|
|
|
"\\Knot\\Dict\\Helpers\\UnderscoreHelper" |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Add new functions to static function list. |
|
63
|
|
|
* |
|
64
|
|
|
* @param $functionRoute |
|
65
|
|
|
* @param Callable $function |
|
66
|
|
|
* |
|
67
|
|
|
* @return false|Callable |
|
68
|
|
|
*/ |
|
69
|
|
|
public function addRoute($functionRoute, callable $function) |
|
70
|
|
|
{ |
|
71
|
|
|
if ( $this->isRoute($functionRoute) ) |
|
72
|
|
|
{ |
|
73
|
|
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $this->functionRoutes[$functionRoute] = $function; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $functionName |
|
82
|
|
|
* |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
|
|
public function isRoute($functionName) |
|
86
|
|
|
{ |
|
87
|
|
|
return isset( $this->functionRoutes[$functionName] ); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Load Helper! |
|
93
|
|
|
* |
|
94
|
|
|
* @param HelperInterface $helperObject |
|
95
|
|
|
* |
|
96
|
|
|
* @return false|HelperInterface |
|
97
|
|
|
*/ |
|
98
|
|
|
public function loadHelper(HelperInterface $helperObject) |
|
99
|
|
|
{ |
|
100
|
|
|
$helperName = $helperObject->getName(); |
|
101
|
|
|
|
|
102
|
|
|
if ( $this->isHelper($helperName) ) |
|
103
|
|
|
{ |
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$helperObject->addRoutes($this); |
|
108
|
|
|
|
|
109
|
|
|
return $this->helperList[$helperName] = $helperObject; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param string $functionName |
|
115
|
|
|
* @param array $arguments |
|
116
|
|
|
* @param \Knot\ParentArray |
|
117
|
|
|
* |
|
118
|
|
|
* @return mixed |
|
119
|
|
|
* @throws WrongFunctionException|FunctionExecuteException |
|
120
|
|
|
*/ |
|
121
|
|
|
public function execute($functionName, $arguments, $knot) |
|
122
|
|
|
{ |
|
123
|
|
|
if ( $this->isRoute($functionName) ) |
|
124
|
|
|
{ |
|
125
|
|
|
$targetFunction = $this->getRoute($functionName); |
|
126
|
|
|
|
|
127
|
|
|
try |
|
128
|
|
|
{ |
|
129
|
|
|
return call_user_func($targetFunction, $knot, $arguments, $functionName); |
|
130
|
|
|
} |
|
131
|
|
|
catch (\Exception $e) |
|
132
|
|
|
{ |
|
133
|
|
|
throw new FunctionExecuteException($functionName); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
else |
|
137
|
|
|
{ |
|
138
|
|
|
throw new WrongFunctionException($functionName); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
public function getRoute($staticFunctionName) |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->functionRoutes[$staticFunctionName]; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
public function isHelper($helperName) |
|
150
|
|
|
{ |
|
151
|
|
|
return isset( $this->helperList[$helperName] ); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|