-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
ProxyConstructor.php
85 lines (78 loc) · 3.69 KB
/
ProxyConstructor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Neos\Flow\ObjectManagement\Proxy;
/*
* This file is part of the Neos.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\ObjectManagement\Exception\CannotBuildObjectException;
use Neos\Flow\Reflection\ClassReflection;
use Neos\Flow\Reflection\Exception\ClassLoadingForReflectionFailedException;
/**
* Representation of a constructor method within a proxy class
*
*/
class ProxyConstructor extends ProxyMethod
{
/**
*
*
* @param string $fullOriginalClassName The fully qualified class name of the original class
* @psalm-param class-string $fullOriginalClassName
*/
public function __construct($fullOriginalClassName)
{
parent::__construct($fullOriginalClassName, '__construct');
}
/**
* Renders the code for a proxy constructor
*
* @return string PHP code
* @throws CannotBuildObjectException|ClassLoadingForReflectionFailedException
*/
public function render()
{
$methodDocumentation = $this->buildMethodDocumentation($this->fullOriginalClassName, $this->methodName);
$callParentMethodCode = $this->buildCallParentMethodCode($this->fullOriginalClassName, $this->methodName);
$finalKeyword = $this->reflectionService->isMethodFinal($this->fullOriginalClassName, $this->methodName) ? 'final ' : '';
$staticKeyword = $this->reflectionService->isMethodStatic($this->fullOriginalClassName, $this->methodName) ? 'static ' : '';
$code = '';
if ($this->addedPreParentCallCode !== '' || $this->addedPostParentCallCode !== '') {
foreach ((new ClassReflection($this->fullOriginalClassName))->getInterfaceNames() as $interfaceName) {
if ($this->reflectionService->getMethodParameters($interfaceName, $this->methodName) !== []) {
throw new CannotBuildObjectException(sprintf('Cannot build proxy for class "%s" because it implements interface "%s" that defines a constructor with arguments. Adjust the interface or avoid the proxy class from being created (for example by adjusting the Method privilege in your Policy)', $this->fullOriginalClassName, $interfaceName), 1630656424);
}
}
$argumentsCode = (count($this->reflectionService->getMethodParameters($this->fullOriginalClassName, $this->methodName)) > 0) ? ' $arguments = func_get_args();' . "\n" : '';
$code = "\n" .
$methodDocumentation .
' ' . $finalKeyword . $staticKeyword . "public function __construct()\n {\n" .
$argumentsCode .
$this->addedPreParentCallCode . $callParentMethodCode . $this->addedPostParentCallCode .
" }\n";
}
return $code;
}
/**
* Builds PHP code which calls the original (ie. parent) method after the added code has been executed.
*
* @param string $fullClassName Fully qualified name of the original class
* @param string $methodName Name of the original method
* @return string PHP code
*/
protected function buildCallParentMethodCode($fullClassName, $methodName)
{
if (!$this->reflectionService->hasMethod($fullClassName, $methodName)) {
return '';
}
if (count($this->reflectionService->getMethodParameters($this->fullOriginalClassName, $this->methodName)) > 0) {
return " parent::" . $methodName . "(...\$arguments);\n";
} else {
return " parent::" . $methodName . "();\n";
}
}
}