1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jabe\Engine\Impl\Util; |
4
|
|
|
|
5
|
|
|
use Jabe\Engine\ProcessEngineException; |
6
|
|
|
use Jabe\Engine\Impl\ProcessEngineLogger; |
7
|
|
|
use Jabe\Engine\Impl\Cfg\ProcessEngineConfigurationImpl; |
8
|
|
|
use Jabe\Engine\Impl\Context\Context; |
9
|
|
|
use Jabe\Engine\Impl\Util\EnsureUtil; |
10
|
|
|
|
11
|
|
|
class ReflectUtil |
12
|
|
|
{ |
13
|
|
|
//private static final EngineUtilLogger LOG = ProcessEngineLogger.UTIL_LOGGER; |
14
|
|
|
|
15
|
|
|
private const CHAR_ENCODINGS = [ |
16
|
|
|
"ä" => "%C3%A4", |
17
|
|
|
"ö" => "%C3%B6", |
18
|
|
|
"ü" => "%C3%BC", |
19
|
|
|
"Ä" => "%C3%84", |
20
|
|
|
"Ö" => "%C3%96", |
21
|
|
|
"Ü" => "%C3%9C" |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
public static function getResourceAsStream(string $name) |
25
|
|
|
{ |
26
|
|
|
$resourceStream = null; |
27
|
|
|
if (file_exists($name)) { |
28
|
|
|
$resourceStream = fopen($name, 'r+'); |
29
|
|
|
} |
30
|
|
|
return $resourceStream; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public static function getResource(string $name): ?string |
34
|
|
|
{ |
35
|
|
|
return file_exists($name) ? $name : null; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function getResourceUrlAsString(string $name): string |
39
|
|
|
{ |
40
|
|
|
$url = self::getResource($name); |
41
|
|
|
foreach (self::CHAR_ENCODINGS as $key => $value) { |
42
|
|
|
$url = str_replace($key, $value, $url); |
43
|
|
|
} |
44
|
|
|
return $url; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function instantiate(string $className) |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
|
|
return new $className(); |
51
|
|
|
} catch (\Exception $e) { |
52
|
|
|
//throw LOG.exceptionWhileInstantiatingClass(className, e); |
53
|
|
|
throw new \Exception(sprintf("exceptionWhileInstantiatingClass %s", $className)); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the field of the given object or null if it doesnt exist. |
59
|
|
|
*/ |
60
|
|
|
public static function getField(string $fieldName, $object): ?\ReflectionProperty |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
|
|
$ref = new \ReflectionClass($object); |
64
|
|
|
return $ref->getProperty($fieldName); |
65
|
|
|
} catch (\Exception $e) { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function setField(\ReflectionProperty $field, $object, $value): void |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
try { |
73
|
|
|
$field->setValue($value); |
74
|
|
|
} catch (\Exception $e) { |
75
|
|
|
//throw LOG.exceptionWhileSettingField(field, object, value, e); |
76
|
|
|
throw $e; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the setter-method for the given field name or null if no setter exists. |
82
|
|
|
*/ |
83
|
|
|
public static function getSetter(string $fieldName, string $clazz): ?\ReflectionMethod |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$setterName = self::buildSetterName($fieldName); |
86
|
|
|
try { |
87
|
|
|
// Using getMathods(), getMathod(...) expects exact parameter type |
88
|
|
|
// matching and ignores inheritance-tree. |
89
|
|
|
$ref = new \ReflectionClass($object); |
|
|
|
|
90
|
|
|
$methods = $ref->getMethods(); |
91
|
|
|
foreach ($methods as $method) { |
92
|
|
|
if ($method->name == $setterName) { |
93
|
|
|
return $method; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
return null; |
97
|
|
|
} catch (\Exception $e) { |
98
|
|
|
//throw LOG.unableToAccessMethod(setterName, clazz.getName()); |
99
|
|
|
throw new \Exception("unableToAccessMethod"); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private static function buildSetterName(string $fieldName): string |
104
|
|
|
{ |
105
|
|
|
return "set" . strtoupper($fieldName[0]) . substr($fieldName, 1); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Finds a method by name |
110
|
|
|
* |
111
|
|
|
* @param declaringType the name of the class |
112
|
|
|
* @param methodName the name of the method to look for |
113
|
|
|
*/ |
114
|
|
|
public static function getMethod(string $declaringType, string $methodName): ?\ReflectionMethod |
115
|
|
|
{ |
116
|
|
|
try { |
117
|
|
|
$ref = new \ReflectionClass($declaringType); |
118
|
|
|
$methods = $ref->getMethods(); |
119
|
|
|
foreach ($methods as $method) { |
120
|
|
|
if ($method->name == $methodName) { |
121
|
|
|
return $method; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
return null; |
125
|
|
|
} catch (\Exception $e) { |
126
|
|
|
return null; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|