1
|
|
|
<?php |
2
|
|
|
namespace gossi\codegen\model; |
3
|
|
|
|
4
|
|
|
use gossi\codegen\model\parts\AbstractPart; |
5
|
|
|
use gossi\codegen\model\parts\ConstantsPart; |
6
|
|
|
use gossi\codegen\model\parts\FinalPart; |
7
|
|
|
use gossi\codegen\model\parts\InterfacesPart; |
8
|
|
|
use gossi\codegen\model\parts\PropertiesPart; |
9
|
|
|
use gossi\codegen\model\parts\TraitsPart; |
10
|
|
|
use gossi\codegen\parser\FileParser; |
11
|
|
|
use gossi\codegen\parser\visitor\ClassParserVisitor; |
12
|
|
|
use gossi\codegen\parser\visitor\ConstantParserVisitor; |
13
|
|
|
use gossi\codegen\parser\visitor\MethodParserVisitor; |
14
|
|
|
use gossi\codegen\parser\visitor\PropertyParserVisitor; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Represents a PHP class. |
18
|
|
|
* |
19
|
|
|
* @author Thomas Gossmann |
20
|
|
|
*/ |
21
|
|
|
class PhpClass extends AbstractPhpStruct implements GenerateableInterface, TraitsInterface, ConstantsInterface, PropertiesInterface { |
22
|
|
|
|
23
|
|
|
use AbstractPart; |
24
|
|
|
use ConstantsPart; |
25
|
|
|
use FinalPart; |
26
|
|
|
use InterfacesPart; |
27
|
|
|
use PropertiesPart; |
28
|
|
|
use TraitsPart; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
private $parentClassName; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Creates a PHP class from reflection |
35
|
|
|
* |
36
|
|
|
* @deprecated Use fromFile() instead |
37
|
|
|
* @param \ReflectionClass $ref |
38
|
|
|
* @return PhpClass |
39
|
|
|
*/ |
40
|
|
|
public static function fromReflection(\ReflectionClass $ref) { |
41
|
|
|
return static::fromFile($ref->getFileName()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Creates a PHP class from file |
46
|
|
|
* |
47
|
|
|
* @param string $filename |
48
|
|
|
* @return PhpClass |
49
|
|
|
*/ |
50
|
11 |
|
public static function fromFile($filename) { |
51
|
11 |
|
$class = new PhpClass(); |
52
|
11 |
|
$parser = new FileParser($filename); |
53
|
11 |
|
$parser->addVisitor(new ClassParserVisitor($class)); |
54
|
11 |
|
$parser->addVisitor(new MethodParserVisitor($class)); |
55
|
11 |
|
$parser->addVisitor(new ConstantParserVisitor($class)); |
56
|
11 |
|
$parser->addVisitor(new PropertyParserVisitor($class)); |
57
|
11 |
|
$parser->parse(); |
58
|
|
|
|
59
|
11 |
|
return $class; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Creates a new PHP class |
64
|
|
|
* |
65
|
|
|
* @param string $name the qualified name |
66
|
|
|
*/ |
67
|
45 |
|
public function __construct($name = null) { |
68
|
45 |
|
parent::__construct($name); |
69
|
45 |
|
$this->initProperties(); |
70
|
45 |
|
$this->initConstants(); |
71
|
45 |
|
$this->initInterfaces(); |
72
|
45 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the parent class name |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
13 |
|
public function getParentClassName() { |
80
|
13 |
|
return $this->parentClassName; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Sets the parent class name |
85
|
|
|
* |
86
|
|
|
* @param string|null $name the new parent |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
3 |
|
public function setParentClassName($name) { |
90
|
3 |
|
$this->parentClassName = $name; |
91
|
|
|
|
92
|
3 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
5 |
|
public function generateDocblock() { |
96
|
5 |
|
parent::generateDocblock(); |
97
|
|
|
|
98
|
5 |
|
foreach ($this->constants as $constant) { |
99
|
2 |
|
$constant->generateDocblock(); |
100
|
5 |
|
} |
101
|
|
|
|
102
|
5 |
|
foreach ($this->properties as $prop) { |
103
|
3 |
|
$prop->generateDocblock(); |
104
|
5 |
|
} |
105
|
5 |
|
} |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|