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