1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\PhpGenerator;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38:
39: class Method extends Nette\Object
40: {
41:
42: private $name;
43:
44:
45: private $parameters = array();
46:
47:
48: private $uses = array();
49:
50:
51: private $body;
52:
53:
54: private $static;
55:
56:
57: private $visibility;
58:
59:
60: private $final;
61:
62:
63: private $abstract;
64:
65:
66: private $returnReference;
67:
68:
69: private $documents = array();
70:
71:
72:
73: public static function from($from)
74: {
75: $from = $from instanceof \ReflectionMethod ? $from : new \ReflectionMethod($from);
76: $method = new static;
77: $method->name = $from->getName();
78: foreach ($from->getParameters() as $param) {
79: $method->parameters[$param->getName()] = Parameter::from($param);
80: }
81: $method->static = $from->isStatic();
82: $method->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : '');
83: $method->final = $from->isFinal();
84: $method->abstract = $from->isAbstract() && !$from->getDeclaringClass()->isInterface();
85: $method->body = $from->isAbstract() ? FALSE : '';
86: $method->returnReference = $from->returnsReference();
87: $method->documents = preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n"));
88: return $method;
89: }
90:
91:
92:
93: public function addParameter($name, $defaultValue = NULL)
94: {
95: $param = new Parameter;
96: if (func_num_args() > 1) {
97: $param->setOptional(TRUE)->setDefaultValue($defaultValue);
98: }
99: return $this->parameters[$name] = $param->setName($name);
100: }
101:
102:
103:
104: public function addUse($name)
105: {
106: $param = new Parameter;
107: return $this->uses[] = $param->setName($name);
108: }
109:
110:
111:
112: public function setBody($statement, array $args = NULL)
113: {
114: $this->body = func_num_args() > 1 ? Helpers::formatArgs($statement, $args) : $statement;
115: return $this;
116: }
117:
118:
119:
120: public function addBody($statement, array $args = NULL)
121: {
122: $this->body .= (func_num_args() > 1 ? Helpers::formatArgs($statement, $args) : $statement) . "\n";
123: return $this;
124: }
125:
126:
127:
128: public function __toString()
129: {
130: $parameters = array();
131: foreach ($this->parameters as $param) {
132: $parameters[] = ($param->typeHint ? $param->typeHint . ' ' : '')
133: . ($param->reference ? '&' : '')
134: . '$' . $param->name
135: . ($param->optional ? ' = ' . Helpers::dump($param->defaultValue) : '');
136: }
137: $uses = array();
138: foreach ($this->uses as $param) {
139: $uses[] = ($param->reference ? '&' : '') . '$' . $param->name;
140: }
141: return ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n" : '')
142: . ($this->abstract ? 'abstract ' : '')
143: . ($this->final ? 'final ' : '')
144: . ($this->visibility ? $this->visibility . ' ' : '')
145: . ($this->static ? 'static ' : '')
146: . 'function'
147: . ($this->returnReference ? ' &' : '')
148: . ($this->name ? ' ' . $this->name : '')
149: . '(' . implode(', ', $parameters) . ')'
150: . ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '')
151: . ($this->abstract || $this->body === FALSE ? ';'
152: : ($this->name ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(trim($this->body), 1) . "\n}");
153: }
154:
155: }
156: