|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
declare(strict_types=1); |
|
20
|
|
|
|
|
21
|
|
|
namespace ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator; |
|
22
|
|
|
|
|
23
|
|
|
use ProxyManager\Generator\MethodGenerator; |
|
24
|
|
|
use Zend\Code\Generator\PropertyGenerator; |
|
25
|
|
|
use Zend\Code\Reflection\MethodReflection; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Method decorator for lazy loading value holder objects |
|
29
|
|
|
* |
|
30
|
|
|
* @author Marco Pivetta <[email protected]> |
|
31
|
|
|
* @license MIT |
|
32
|
|
|
*/ |
|
33
|
|
|
class LazyLoadingMethodInterceptor extends MethodGenerator |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @param \Zend\Code\Reflection\MethodReflection $originalMethod |
|
37
|
|
|
* @param \Zend\Code\Generator\PropertyGenerator $initializerProperty |
|
38
|
|
|
* @param \Zend\Code\Generator\PropertyGenerator $valueHolderProperty |
|
39
|
|
|
* |
|
40
|
|
|
* @return self|static |
|
41
|
|
|
*/ |
|
42
|
2 |
|
public static function generateMethod( |
|
43
|
|
|
MethodReflection $originalMethod, |
|
44
|
|
|
PropertyGenerator $initializerProperty, |
|
45
|
|
|
PropertyGenerator $valueHolderProperty |
|
46
|
|
|
) : self { |
|
47
|
|
|
/* @var $method self */ |
|
48
|
2 |
|
$method = static::fromReflection($originalMethod); |
|
49
|
2 |
|
$initializerName = $initializerProperty->getName(); |
|
50
|
2 |
|
$valueHolderName = $valueHolderProperty->getName(); |
|
51
|
2 |
|
$parameters = $originalMethod->getParameters(); |
|
52
|
2 |
|
$methodName = $originalMethod->getName(); |
|
53
|
2 |
|
$initializerParams = []; |
|
54
|
2 |
|
$forwardedParams = []; |
|
55
|
|
|
|
|
56
|
2 |
|
foreach ($parameters as $parameter) { |
|
57
|
1 |
|
$parameterName = $parameter->getName(); |
|
58
|
1 |
|
$variadicPrefix = $parameter->isVariadic() ? '...' : ''; |
|
59
|
1 |
|
$initializerParams[] = var_export($parameterName, true) . ' => $' . $parameterName; |
|
60
|
1 |
|
$forwardedParams[] = $variadicPrefix . '$' . $parameterName; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
$method->setBody( |
|
64
|
2 |
|
'$this->' . $initializerName |
|
65
|
2 |
|
. ' && $this->' . $initializerName |
|
66
|
2 |
|
. '->__invoke($this->' . $valueHolderName . ', $this, ' . var_export($methodName, true) |
|
67
|
2 |
|
. ', array(' . implode(', ', $initializerParams) . '), $this->' . $initializerName . ");\n\n" |
|
68
|
2 |
|
. rtrim(self::returnStatement( |
|
69
|
2 |
|
'$this->' . $valueHolderName . '->' . $methodName . '(' . implode(', ', $forwardedParams) . ')', |
|
70
|
|
|
$originalMethod |
|
71
|
|
|
)) |
|
72
|
|
|
); |
|
73
|
2 |
|
$method->setDocblock('{@inheritDoc}'); |
|
74
|
|
|
|
|
75
|
2 |
|
return $method; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
private static function returnStatement(string $returnedValue, \ReflectionMethod $originalMethod) : string |
|
79
|
|
|
{ |
|
80
|
2 |
|
$returnType = $originalMethod->getReturnType(); |
|
81
|
|
|
|
|
82
|
2 |
|
if ($returnType && 'void' === (string) $returnType) { |
|
83
|
|
|
return $returnedValue . ";\n\nreturn;\n"; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
2 |
|
return 'return ' . $returnedValue . ";\n"; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|