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: 40:
41: class Method extends Nette\Object
42: {
43:
44: private $name;
45:
46:
47: private $parameters = array();
48:
49:
50: private $uses = array();
51:
52:
53: private $body;
54:
55:
56: private $static;
57:
58:
59: private $visibility;
60:
61:
62: private $final;
63:
64:
65: private $abstract;
66:
67:
68: private $returnReference;
69:
70:
71: private $variadic;
72:
73:
74: private $documents = array();
75:
76:
77:
78: public static function from($from)
79: {
80: $from = $from instanceof \ReflectionMethod ? $from : new \ReflectionMethod($from);
81: $method = new static;
82: $method->name = $from->getName();
83: foreach ($from->getParameters() as $param) {
84: $method->parameters[$param->getName()] = Parameter::from($param);
85: }
86: $method->static = $from->isStatic();
87: $method->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : '');
88: $method->final = $from->isFinal();
89: $method->abstract = $from->isAbstract() && !$from->getDeclaringClass()->isInterface();
90: $method->body = $from->isAbstract() ? FALSE : '';
91: $method->returnReference = $from->returnsReference();
92: $method->variadic = PHP_VERSION_ID >= 50600 && $from->isVariadic();
93: $method->documents = preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n"));
94: return $method;
95: }
96:
97:
98:
99: public function addParameter($name, $defaultValue = NULL)
100: {
101: $param = new Parameter;
102: if (func_num_args() > 1) {
103: $param->setOptional(TRUE)->setDefaultValue($defaultValue);
104: }
105: return $this->parameters[$name] = $param->setName($name);
106: }
107:
108:
109:
110: public function addUse($name)
111: {
112: $param = new Parameter;
113: return $this->uses[] = $param->setName($name);
114: }
115:
116:
117:
118: public function setBody($statement, array $args = NULL)
119: {
120: $this->body = func_num_args() > 1 ? Helpers::formatArgs($statement, $args) : $statement;
121: return $this;
122: }
123:
124:
125:
126: public function addBody($statement, array $args = NULL)
127: {
128: $this->body .= (func_num_args() > 1 ? Helpers::formatArgs($statement, $args) : $statement) . "\n";
129: return $this;
130: }
131:
132:
133:
134: public function __toString()
135: {
136: $parameters = array();
137: foreach ($this->parameters as $param) {
138: $variadic = $this->variadic && $param === end($this->parameters);
139: $parameters[] = ($param->typeHint ? $param->typeHint . ' ' : '')
140: . ($param->reference ? '&' : '')
141: . ($variadic ? '...' : '')
142: . '$' . $param->name
143: . ($param->optional && !$variadic ? ' = ' . Helpers::dump($param->defaultValue) : '');
144: }
145: $uses = array();
146: foreach ($this->uses as $param) {
147: $uses[] = ($param->reference ? '&' : '') . '$' . $param->name;
148: }
149: return ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n" : '')
150: . ($this->abstract ? 'abstract ' : '')
151: . ($this->final ? 'final ' : '')
152: . ($this->visibility ? $this->visibility . ' ' : '')
153: . ($this->static ? 'static ' : '')
154: . 'function'
155: . ($this->returnReference ? ' &' : '')
156: . ($this->name ? ' ' . $this->name : '')
157: . '(' . implode(', ', $parameters) . ')'
158: . ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '')
159: . ($this->abstract || $this->body === FALSE ? ';'
160: : ($this->name ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(trim($this->body), 1) . "\n}");
161: }
162:
163: }
164: