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