1
|
|
|
<?php |
2
|
|
|
namespace EZAMA |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* |
6
|
|
|
* @Name : Shortcut |
7
|
|
|
* @Version : 1.0.0 |
8
|
|
|
* @Programmer : Akpé Aurelle Emmanuel Moïse Zinsou |
9
|
|
|
* @Date : 2019-04-01 |
10
|
|
|
* @Released under : https://github.com/manuwhat/Shortcut/blob/master/LICENSE |
11
|
|
|
* @Repository : https://github.com/manuwhat/Shortcut |
12
|
|
|
* |
13
|
|
|
**/ |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
class Shortcut |
17
|
|
|
{ |
18
|
|
|
const VALID_PHP_FUNCTION_NAME_PATTERN='#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$#'; |
19
|
|
|
const CAN_NEVER_EVER_CHOOSE_THIS_AS_FUNCTION_NAME="new"; |
20
|
|
|
const PLACEHOLDER_FOR_INTERNALS_CLASSES_OPTIONALS_PARAMETERS="acce91966cd8eee995ee1ac30c98c3d89d8f9235"; |
21
|
|
|
private static $DIR=null; |
22
|
|
|
private static $SHORTCUT_FOR_ALL=false; |
23
|
|
|
|
24
|
|
|
public static function SetShortcutForAll($bool) |
25
|
|
|
{ |
26
|
|
|
self::$SHORTCUT_FOR_ALL=(bool)$bool; |
27
|
|
|
} |
28
|
|
|
public static function create($classname, $name=self::CAN_NEVER_EVER_CHOOSE_THIS_AS_FUNCTION_NAME) |
29
|
|
|
{ |
30
|
|
|
if (is_string($classname)&&class_exists($classname, true)) { |
31
|
|
|
$reflectionClass=new \reflectionClass($classname); |
32
|
|
|
$classname=$reflectionClass->getName(); |
33
|
|
|
$fullQualifiedClassname=str_replace('\\', '_', $classname); |
34
|
|
|
self::getTheRightDir($file, $Dir, $fullQualifiedClassname); |
35
|
|
|
$fileExists=file_exists($file); |
36
|
|
|
if (!function_exists($classname)&&!function_exists($name)) { |
37
|
|
|
if (!$fileExists) { |
38
|
|
|
$private_scope=false; |
39
|
|
|
$name=trim($name); |
40
|
|
|
self::createDir($Dir); |
41
|
|
|
$reflectionMethod=$reflectionClass->getConstructor(); |
42
|
|
|
$notInstantiable=false; |
43
|
|
|
if (is_null($reflectionMethod)||$notInstantiable=!$reflectionClass->isInstantiable()) { |
44
|
|
|
self::HandleNotInstantiableAndHasNoConstructor($Shortcut, $fullQualifiedClassname, $name, $notInstantiable, $classname); |
45
|
|
|
if ($Shortcut) { |
46
|
|
|
return self::pushAndShow($file, $Shortcut); |
47
|
|
|
} |
48
|
|
|
$private_scope=true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
self::getSignature($reflectionMethod, $signature, $parameters, $paramsNum, $count); |
52
|
|
|
|
53
|
|
|
$hasInternal=''; |
54
|
|
|
if ($count) { |
55
|
|
|
self::BuildTheSwitch($hasInternal, $count, $paramsNum, $parameters, $classname); |
56
|
|
|
} |
57
|
|
|
self::useTheRightNameAndScope($Shortcut, $name, $fullQualifiedClassname, $signature, $private_scope, $classname); |
58
|
|
|
|
59
|
|
|
self::handleInternals($Shortcut, $hasInternal, $parameters, $signature, $classname); |
60
|
|
|
|
61
|
|
|
return self::pushAndShow($file, $Shortcut); |
62
|
|
|
} else { |
63
|
|
|
return include_once($file); |
64
|
|
|
} |
65
|
|
|
} else { |
66
|
|
|
self::GetTheRightExceptionMessage($fileExists, $name, $fullQualifiedClassname); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private static function getSignature(\ReflectionMethod $method, &$signature, &$parameters, &$paramsNum, &$count) |
72
|
|
|
{ |
73
|
|
|
$params=$method->getParameters(); |
74
|
|
|
$paramsNum=count($params); |
75
|
|
|
$signature=''; |
76
|
|
|
$parameters=array(); |
77
|
|
|
$count=0; |
78
|
|
|
foreach ($params as $k=>$param) { |
79
|
|
|
self::getParameterDeclaration($param, $tmp, $count, $method); |
80
|
|
|
$signature.=$tmp; |
81
|
|
|
$parameters[]='$'.$param->getName(); |
82
|
|
|
$tmp=''; |
83
|
|
|
if ($k<$paramsNum-1) { |
84
|
|
|
$signature.=','; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private static function getParameterDeclaration(\reflectionParameter $param, &$tmp, &$count, $method) |
90
|
|
|
{ |
91
|
|
|
$tmp=$param->isPassedByReference()?'&$'.$param->getName():'$'.$param->getName(); |
92
|
|
|
if ($param->isOptional()) { |
93
|
|
|
$count++; |
94
|
|
|
if ($method->isInternal()) { |
95
|
|
|
$tmp.='="acce91966cd8eee995ee1ac30c98c3d89d8f9235"'; |
96
|
|
|
} else { |
97
|
|
|
self::handleOptionalParameter($param, $tmp); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private static function handleOptionalParameter(\reflectionParameter $param, &$tmp) |
103
|
|
|
{ |
104
|
|
|
if ($param->isDefaultValueConstant()) { |
105
|
|
|
$tmp.='='.$param->getDefaultValueConstantName(); |
106
|
|
|
} elseif ($param->isDefaultValueAvailable()) { |
107
|
|
|
$tmp.='='.var_export($param->getDefaultValue(), true); |
108
|
|
|
} elseif ($param->allowsNull()) { |
109
|
|
|
$tmp.='=null'; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private static function BuildTheSwitch(&$hasInternal, $count, $paramsNum, $parameters, $classname) |
114
|
|
|
{ |
115
|
|
|
$hasInternal.='switch($count){'; |
116
|
|
|
while ($count>0) { |
117
|
|
|
$hasInternal.="case $count:return new $classname(".join(',', array_slice($parameters, 0, $paramsNum-$count))."); break;"; |
118
|
|
|
$count--; |
119
|
|
|
} |
120
|
|
|
$hasInternal.='default:return new '.$classname.'('.join(',', $parameters).');break;}'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private static function useTheRightNameAndScope(&$Shortcut, $name, $fullQualifiedClassname, $signature, $scope, $classname) |
124
|
|
|
{ |
125
|
|
|
if (strtolower($name)!=='new'&&preg_match(self::VALID_PHP_FUNCTION_NAME_PATTERN, $name)) { |
126
|
|
|
$Shortcut="<?php
|
127
|
|
|
function $name($signature){"; |
128
|
|
|
if ($scope) { |
129
|
|
|
$Shortcut.="if(".'@get_class()'."!==$classname){
|
130
|
|
|
throw new scopeException(\"Shortcut function $name can only be called in class $classname scope\");
|
131
|
|
|
}"; |
132
|
|
|
} |
133
|
|
|
} else { |
134
|
|
|
$Shortcut="<?php
|
135
|
|
|
function $fullQualifiedClassname($signature){"; |
136
|
|
|
if ($scope) { |
137
|
|
|
$Shortcut.="if(@get_class()!==\"$classname\"){
|
138
|
|
|
throw new scopeException(\"Shortcut function $fullQualifiedClassname can only be called in class $classname scope\");
|
139
|
|
|
}"; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
private static function handleInternals(&$Shortcut, $hasInternal, $parameters, $signature, $classname) |
145
|
|
|
{ |
146
|
|
|
if (!strpos($signature, "acce91966cd8eee995ee1ac30c98c3d89d8f9235")) { |
147
|
|
|
$Shortcut.="return new $classname(".join(',', $parameters).");
|
148
|
|
|
}"; |
149
|
|
|
} else { |
150
|
|
|
$Shortcut.='
|
151
|
|
|
$count=count(array_keys(get_defined_vars(),"'.self::PLACEHOLDER_FOR_INTERNALS_CLASSES_OPTIONALS_PARAMETERS.'"));
|
152
|
|
|
'.$hasInternal.'
|
153
|
|
|
}'; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
private static function pushAndShow($file, $Shortcut) |
158
|
|
|
{ |
159
|
|
|
file_put_contents($file, str_replace("\t", ' ', $Shortcut)); |
160
|
|
|
file_put_contents($file, php_strip_whitespace($file)); //just for cleanliness of the generated code |
161
|
|
|
return include_once($file); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
private static function GetTheRightExceptionMessage($fileExists, $name, $fullQualifiedClassname) |
165
|
|
|
{ |
166
|
|
|
if (!$fileExists) { |
167
|
|
|
if (strtolower($name)!=='new'&&preg_match(self::VALID_PHP_FUNCTION_NAME_PATTERN, $name)) { |
168
|
|
|
throw new \InvalidArgumentException('function '.$name.' passed as second Argument already exists.
|
169
|
|
|
Can\'t create a shortcut with the same name'); |
170
|
|
|
} else { |
171
|
|
|
throw new \InvalidArgumentException('function '.$fullQualifiedClassname.' already exists and An alias has not been provided as Argument 2.
|
172
|
|
|
Can\'t create a shortcut function with this name'); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
private static function HandleNotInstantiableAndHasNoConstructor(&$Shortcut, $fullQualifiedClassname, $name, $notInstantiable, $classname) |
178
|
|
|
{ |
179
|
|
|
if ($notInstantiable) { |
180
|
|
|
if (!self::$SHORTCUT_FOR_ALL) { |
181
|
|
|
throw new \InvalidArgumentException('Not Instantiable class '.$fullQualifiedClassname.' passed as Argument'); |
182
|
|
|
} |
183
|
|
|
} else { |
184
|
|
|
self::useTheRightNameAndScope($Shortcut, $name, $fullQualifiedClassname, '', false, $classname); |
185
|
|
|
$Shortcut.="return new $classname();
|
186
|
|
|
}"; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
private static function getTheRightDir(&$file, &$Dir, $fullQualifiedClassname) |
191
|
|
|
{ |
192
|
|
|
if ($Dir=self::$DIR) { |
193
|
|
|
$file=self::$DIR.DIRECTORY_SEPARATOR.$fullQualifiedClassname.".Shortcut.php"; |
194
|
|
|
} else { |
195
|
|
|
$Dir=dirname(__DIR__).DIRECTORY_SEPARATOR.'ClassShortcuts'; |
196
|
|
|
$file=$Dir.DIRECTORY_SEPARATOR.$fullQualifiedClassname.".Shortcut.php"; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
private static function createDir($Dir) |
201
|
|
|
{ |
202
|
|
|
if (!file_exists($Dir)) { |
203
|
|
|
mkdir($Dir); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
|
209
|
|
|
public static function setDir($dirname) |
210
|
|
|
{ |
211
|
|
|
if (is_dir($dirname)&&is_writable($dirname)&&!self::$DIR) { |
212
|
|
|
self::$DIR=$dirname; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
private function __construct() |
218
|
|
|
{ |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
namespace{ |
226
|
|
|
function create_Shortcut($classname, $name='new') |
227
|
|
|
{ |
228
|
|
|
return EZAMA\Shortcut::create($classname, $name); |
229
|
|
|
} |
230
|
|
|
class scopeException extends \Exception |
231
|
|
|
{ |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|