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