|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class Dumper |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/mtymek/class-dumper |
|
6
|
|
|
* @copyright Copyright (c) 2015 Mateusz Tymek |
|
7
|
|
|
* @license BSD 2-Clause |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace ClassDumper; |
|
11
|
|
|
|
|
12
|
|
|
use Zend\Code\Reflection\ClassReflection; |
|
13
|
|
|
|
|
14
|
|
|
class CodeGenerator |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @param ClassReflection $class |
|
18
|
|
|
* @return array |
|
19
|
|
|
*/ |
|
20
|
|
|
private function getUseArray(ClassReflection $class) |
|
21
|
|
|
{ |
|
22
|
|
|
$usesNames = []; |
|
23
|
|
|
foreach ($class->getDeclaringFile()->getUses() as $use) { |
|
24
|
|
|
$usesNames[$use['use']] = $use['as']; |
|
25
|
|
|
} |
|
26
|
|
|
return $usesNames; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param ClassReflection $class |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getUseLines(ClassReflection $class) |
|
34
|
|
|
{ |
|
35
|
|
|
$useLines = []; |
|
36
|
|
|
foreach ($this->getUseArray($class) as $name => $as) { |
|
37
|
|
|
$useString = "use $name"; |
|
38
|
|
|
if ($as) { |
|
39
|
|
|
$useString .= " as $as"; |
|
40
|
|
|
} |
|
41
|
|
|
$useLines[] = $useString . ';'; |
|
42
|
|
|
} |
|
43
|
|
|
return $useLines; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param ClassReflection $class |
|
48
|
|
|
* @param string $namespaceName |
|
49
|
|
|
* @param array $useArray |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
private function getClassNameInContext(ClassReflection $class, $namespaceName, $useArray) |
|
53
|
|
|
{ |
|
54
|
|
|
if (!$namespaceName) { |
|
55
|
|
|
return '\\' . $class->getName(); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
return array_key_exists($class->getName(), $useArray) |
|
|
|
|
|
|
58
|
|
|
? ($useArray[$class->getName()] ?: $class->getShortName()) |
|
|
|
|
|
|
59
|
|
|
: ((0 === strpos($class->getName(), $namespaceName)) |
|
|
|
|
|
|
60
|
|
|
? substr($class->getName(), strlen($namespaceName) + 1) |
|
|
|
|
|
|
61
|
|
|
: '\\' . $class->getName()); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param ClassReflection $class |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getDeclarationLine(ClassReflection $class) |
|
69
|
|
|
{ |
|
70
|
|
|
$usesNames = $this->getUseArray($class); |
|
71
|
|
|
|
|
72
|
|
|
$declaration = ''; |
|
73
|
|
|
if ($class->isAbstract() && !$class->isInterface() && !$class->isTrait()) { |
|
74
|
|
|
$declaration .= 'abstract '; |
|
75
|
|
|
} |
|
76
|
|
|
if ($class->isFinal()) { |
|
77
|
|
|
$declaration .= 'final '; |
|
78
|
|
|
} |
|
79
|
|
|
if ($class->isInterface()) { |
|
80
|
|
|
$declaration .= 'interface '; |
|
81
|
|
|
} elseif ($class->isTrait()) { |
|
82
|
|
|
$declaration .= 'trait '; |
|
83
|
|
|
} else { |
|
84
|
|
|
$declaration .= 'class '; |
|
85
|
|
|
} |
|
86
|
|
|
$declaration .= $class->getShortName(); |
|
87
|
|
|
$parentName = false; |
|
88
|
|
|
if ($parent = $class->getParentClass()) { |
|
89
|
|
|
$parentName = $this->getClassNameInContext($parent, $class->getNamespaceName(), $usesNames); |
|
90
|
|
|
} |
|
91
|
|
|
if ($parentName) { |
|
|
|
|
|
|
92
|
|
|
$declaration .= " extends {$parentName}"; |
|
93
|
|
|
} |
|
94
|
|
|
$interfaces = array_diff($class->getInterfaceNames(), $parent ? $parent->getInterfaceNames() : array()); |
|
95
|
|
|
if (count($interfaces)) { |
|
96
|
|
|
foreach ($interfaces as $interface) { |
|
97
|
|
|
$iReflection = new ClassReflection($interface); |
|
98
|
|
|
$interfaces = array_diff($interfaces, $iReflection->getInterfaceNames()); |
|
99
|
|
|
} |
|
100
|
|
|
$declaration .= $class->isInterface() ? ' extends ' : ' implements '; |
|
101
|
|
|
$declaration .= implode(', ', array_map(function($interface) use ($usesNames, $class) { |
|
|
|
|
|
|
102
|
|
|
$iReflection = new ClassReflection($interface); |
|
103
|
|
|
return $this->getClassNameInContext($iReflection, $class->getNamespaceName(), $usesNames); |
|
104
|
|
|
}, $interfaces)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $declaration; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|