|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator; |
|
6
|
|
|
|
|
7
|
|
|
use ProxyManager\Generator\MethodGenerator; |
|
8
|
|
|
use ProxyManager\Generator\Util\ProxiedMethodReturnExpression; |
|
9
|
|
|
use ReflectionClass; |
|
10
|
|
|
use Zend\Code\Generator\PropertyGenerator; |
|
11
|
|
|
use Zend\Code\Reflection\MethodReflection; |
|
12
|
|
|
|
|
13
|
|
|
use function str_replace; |
|
14
|
|
|
use function var_export; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Method decorator for remote objects |
|
18
|
|
|
*/ |
|
19
|
|
|
class RemoteObjectMethod extends MethodGenerator |
|
20
|
3 |
|
{ |
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
private const TEMPLATE |
|
23
|
|
|
= <<<PHP |
|
24
|
|
|
\$reflectionMethod = new \\ReflectionMethod(__CLASS__, __FUNCTION__); |
|
25
|
|
|
\$args = \\func_get_args(); |
|
26
|
3 |
|
foreach (\\array_slice(\$reflectionMethod->getParameters(), \\count(\$args)) as \$param) { |
|
27
|
|
|
/** |
|
28
|
3 |
|
* @var ReflectionParameter \$param |
|
29
|
3 |
|
*/ |
|
30
|
3 |
|
if (\$param->isDefaultValueAvailable()) { |
|
31
|
3 |
|
\$args[] = \$param->getDefaultValue(); |
|
32
|
3 |
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
3 |
|
#proxiedReturn# |
|
36
|
|
|
PHP; |
|
37
|
|
|
|
|
38
|
|
|
/** @return self|static */ |
|
39
|
|
|
public static function generateMethod( |
|
40
|
|
|
MethodReflection $originalMethod, |
|
41
|
|
|
PropertyGenerator $adapterProperty, |
|
42
|
|
|
ReflectionClass $originalClass |
|
43
|
|
|
): self { |
|
44
|
|
|
/** @var self $method */ |
|
45
|
|
|
$method = static::fromReflectionWithoutBodyAndDocBlock($originalMethod); |
|
46
|
|
|
$proxiedReturn = '$return = $this->' . $adapterProperty->getName() |
|
47
|
|
|
. '->call(' . var_export($originalClass->getName(), true) |
|
48
|
|
|
. ', ' . var_export($originalMethod->getName(), true) . ', $args);' . "\n\n" |
|
49
|
|
|
. ProxiedMethodReturnExpression::generate('$return', $originalMethod); |
|
50
|
|
|
|
|
51
|
|
|
$body = str_replace( |
|
52
|
|
|
['#proxiedReturn#'], |
|
53
|
|
|
['#proxciedReturn#' => $proxiedReturn], |
|
54
|
|
|
self::TEMPLATE, |
|
55
|
|
|
); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
$method->setBody( |
|
58
|
|
|
$body |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
return $method; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|