1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Utils\PhpGenerator;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27: class ClassType extends Nette\Object
28: {
29:
30: public $name;
31:
32:
33: public $type = 'class';
34:
35:
36: public $final;
37:
38:
39: public $abstract;
40:
41:
42: public $extends = array();
43:
44:
45: public $implements = array();
46:
47:
48: public $traits = array();
49:
50:
51: public $documents = array();
52:
53:
54: public $consts = array();
55:
56:
57: public $properties = array();
58:
59:
60: public $methods = array();
61:
62:
63: public function __construct($name = NULL)
64: {
65: $this->name = $name;
66: }
67:
68:
69:
70: public function addConst($name, $value)
71: {
72: $this->consts[$name] = $value;
73: return $this;
74: }
75:
76:
77:
78: public function addProperty($name, $value = NULL)
79: {
80: $property = new Property;
81: return $this->properties[$name] = $property->setName($name)->setValue($value);
82: }
83:
84:
85:
86: public function addMethod($name)
87: {
88: $method = new Method;
89: if ($this->type === 'interface') {
90: $method->setVisibility('')->setBody(FALSE);
91: } else {
92: $method->setVisibility('public');
93: }
94: return $this->methods[$name] = $method->setName($name);
95: }
96:
97:
98: public function __call($name, $args)
99: {
100: return Nette\ObjectMixin::callProperty($this, $name, $args);
101: }
102:
103:
104:
105: public function __toString()
106: {
107: $consts = array();
108: foreach ($this->consts as $name => $value) {
109: $consts[] = "const $name = " . Helpers::dump($value) . ";\n";
110: }
111: $properties = array();
112: foreach ($this->properties as $property) {
113: $properties[] = ($property->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $property->documents)) . "\n */\n" : '')
114: . $property->visibility . ($property->static ? ' static' : '') . ' $' . $property->name
115: . ($property->value === NULL ? '' : ' = ' . Helpers::dump($property->value))
116: . ";\n";
117: }
118: return Nette\Utils\Strings::normalize(
119: ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n" : '')
120: . ($this->abstract ? 'abstract ' : '')
121: . ($this->final ? 'final ' : '')
122: . $this->type . ' '
123: . $this->name . ' '
124: . ($this->extends ? 'extends ' . implode(', ', (array) $this->extends) . ' ' : '')
125: . ($this->implements ? 'implements ' . implode(', ', (array) $this->implements) . ' ' : '')
126: . "\n{\n\n"
127: . Nette\Utils\Strings::indent(
128: ($this->traits ? "use " . implode(', ', (array) $this->traits) . ";\n\n" : '')
129: . ($this->consts ? implode('', $consts) . "\n\n" : '')
130: . ($this->properties ? implode("\n", $properties) . "\n\n" : '')
131: . implode("\n\n\n", $this->methods), 1)
132: . "\n\n}") . "\n";
133: }
134:
135: }
136: