1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jabe\Engine\Impl\Util; |
4
|
|
|
|
5
|
|
|
use Jabe\Engine\ArtifactFactoryInterface; |
6
|
|
|
use Jabe\Engine\Impl\ProcessEngineLogger; |
7
|
|
|
use Jabe\Engine\Impl\Bpmn\Parser\FieldDeclaration; |
8
|
|
|
use Jabe\Engine\Impl\Context\Context; |
9
|
|
|
use Jabe\Engine\Impl\Util\EnsureUtil; |
10
|
|
|
|
11
|
|
|
class ClassDelegateUtil |
12
|
|
|
{ |
13
|
|
|
//private static final EngineUtilLogger LOG = ProcessEngineLogger.UTIL_LOGGER; |
14
|
|
|
|
15
|
|
|
public static function instantiateDelegate(string $clazz, array $fieldDeclarations) |
16
|
|
|
{ |
17
|
|
|
$artifactFactory = Context::getProcessEngineConfiguration()->getArtifactFactory(); |
|
|
|
|
18
|
|
|
|
19
|
|
|
try { |
20
|
|
|
$object = $artifactFactory->getArtifact($clazz); |
21
|
|
|
|
22
|
|
|
self::applyFieldDeclaration($fieldDeclarations, $object); |
23
|
|
|
return $object; |
24
|
|
|
} catch (\Exception $e) { |
25
|
|
|
//throw LOG.exceptionWhileInstantiatingClass(className, e); |
26
|
|
|
throw new \Exception("exceptionWhileInstantiatingClass"); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function applyFieldDeclaration($fieldDeclarations, $target): void |
31
|
|
|
{ |
32
|
|
|
if (is_array($fieldDeclarations)) { |
33
|
|
|
foreach ($fieldDeclarations as $declaration) { |
34
|
|
|
self::applyFieldDeclaration($declaration, $target); |
35
|
|
|
} |
36
|
|
|
} else { |
37
|
|
|
$declaration = $fieldDeclarations; |
38
|
|
|
$setterMethod = ReflectUtil::getSetter($declaration->getName(), get_class($target)); |
39
|
|
|
|
40
|
|
|
if ($setterMethod != null) { |
41
|
|
|
try { |
42
|
|
|
$setterMethod->invoke($target, $declaration->getValue()); |
43
|
|
|
} catch (\Exception $e) { |
44
|
|
|
//throw LOG.exceptionWhileApplyingFieldDeclatation(declaration.getName(), target.getClass().getName(), e); |
45
|
|
|
throw new \Exception("exceptionWhileApplyingFieldDeclatation"); |
46
|
|
|
} |
47
|
|
|
} else { |
48
|
|
|
$field = ReflectUtil::getField($declaration->getName(), $target); |
49
|
|
|
EnsureUtil::ensureNotNull("Field definition uses unexisting field '" . $declaration->getName() . "' on class " . get_class($target), "field", $field); |
50
|
|
|
// Check if the delegate field's type is correct |
51
|
|
|
if (!self::fieldTypeCompatible($declaration, $field)) { |
52
|
|
|
//throw LOG.incompatibleTypeForFieldDeclaration(declaration, target, field); |
53
|
|
|
throw new \Exception("incompatibleTypeForFieldDeclaration"); |
54
|
|
|
} |
55
|
|
|
ReflectUtil::setField($field, $target, $declaration->getValue()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function fieldTypeCompatible(FieldDeclaration $declaration, \ReflectionProperty $field): bool |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
/*if ($declaration->getValue() !== null) { |
63
|
|
|
return field.getType().isAssignableFrom(declaration.getValue().getClass()); |
64
|
|
|
} else { |
65
|
|
|
// Null can be set any field type |
66
|
|
|
return true; |
67
|
|
|
}*/ |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.