Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
Constructor.php
01 <?php
02
03 declare(strict_types=1);
04
05 namespace ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator;
06
07 use ProxyManager\Generator\MethodGenerator;
08 use ProxyManager\ProxyGenerator\Util\Properties;
09 use ProxyManager\ProxyGenerator\Util\UnsetPropertiesGenerator;
10 use ReflectionClass;
11 use Zend\Code\Generator\PropertyGenerator;
12 use Zend\Code\Reflection\MethodReflection;
13 use Zend\Code\Reflection\ParameterReflection;
14
15 /**
16 * The `__construct` implementation for lazy loading proxies
17 *
18 * @author Marco Pivetta <ocramius@gmail.com>
19 * @license MIT
20 */
21 class Constructor extends MethodGenerator
22 {
23 /**
24 * @throws \Zend\Code\Generator\Exception\InvalidArgumentException
25 */
26 public static function generateMethod(ReflectionClass $originalClass, PropertyGenerator $valueHolder) : self
27 {
28 $originalConstructor = self::getConstructor($originalClass);
29
30 /* @var $constructor self */
31 $constructor = $originalConstructor
32 ? self::fromReflectionWithoutBodyAndDocBlock($originalConstructor)
33 : new self('__construct');
34
35 $constructor->setBody(
36 'static $reflection;' . "\n\n"
37 . 'if (! $this->' . $valueHolder->getName() . ') {' . "\n"
38 . ' $reflection = $reflection ?: new \ReflectionClass('
39 . var_export($originalClass->getName(), true)
40 . ");\n"
41 . ' $this->' . $valueHolder->getName() . ' = $reflection->newInstanceWithoutConstructor();' . "\n"
42 . UnsetPropertiesGenerator::generateSnippet(Properties::fromReflectionClass($originalClass), 'this')
43 . '}'
44 . ($originalConstructor ? self::generateOriginalConstructorCall($originalConstructor, $valueHolder) : '')
45 );
46
47 return $constructor;
48 }
49
50 private static function generateOriginalConstructorCall(
51 MethodReflection $originalConstructor,
52 PropertyGenerator $valueHolder
53 ) : string {
54 return "\n\n"
55 . '$this->' . $valueHolder->getName() . '->' . $originalConstructor->getName() . '('
56 . implode(
57 ', ',
58 array_map(
59 function (ParameterReflection $parameter) : string {
60 return ($parameter->isVariadic() ? '...' : '') . '$' . $parameter->getName();
61 },
62 $originalConstructor->getParameters()
63 )
64 )
65 . ');';
66 }
67
68 /**
69 * @param ReflectionClass $class
70 *
71 * @return MethodReflection|null
72 */
73 private static function getConstructor(ReflectionClass $class)
74 {
75 $constructors = array_map(
76 function (\ReflectionMethod $method) : MethodReflection {
77 return new MethodReflection(
78 $method->getDeclaringClass()->getName(),
79 $method->getName()
80 );
81 },
82 array_filter(
83 $class->getMethods(),
84 function (\ReflectionMethod $method) : bool {
85 return $method->isConstructor();
86 }
87 )
88 );
89
90 return reset($constructors) ?: null;
91 }
92 }
93