[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Zend Framework (http://framework.zend.com/) 4 * 5 * @link http://github.com/zendframework/zf2 for the canonical source repository 6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 7 * @license http://framework.zend.com/license/new-bsd New BSD License 8 */ 9 10 namespace Zend\Code\Generator; 11 12 use Zend\Code\Reflection\MethodReflection; 13 14 class MethodGenerator extends AbstractMemberGenerator 15 { 16 /** 17 * @var DocBlockGenerator 18 */ 19 protected $docBlock = null; 20 21 /** 22 * @var ParameterGenerator[] 23 */ 24 protected $parameters = array(); 25 26 /** 27 * @var string 28 */ 29 protected $body = null; 30 31 /** 32 * @param MethodReflection $reflectionMethod 33 * @return MethodGenerator 34 */ 35 public static function fromReflection(MethodReflection $reflectionMethod) 36 { 37 $method = new static(); 38 39 $method->setSourceContent($reflectionMethod->getContents(false)); 40 $method->setSourceDirty(false); 41 42 if ($reflectionMethod->getDocComment() != '') { 43 $method->setDocBlock(DocBlockGenerator::fromReflection($reflectionMethod->getDocBlock())); 44 } 45 46 $method->setFinal($reflectionMethod->isFinal()); 47 48 if ($reflectionMethod->isPrivate()) { 49 $method->setVisibility(self::VISIBILITY_PRIVATE); 50 } elseif ($reflectionMethod->isProtected()) { 51 $method->setVisibility(self::VISIBILITY_PROTECTED); 52 } else { 53 $method->setVisibility(self::VISIBILITY_PUBLIC); 54 } 55 56 $method->setStatic($reflectionMethod->isStatic()); 57 58 $method->setName($reflectionMethod->getName()); 59 60 foreach ($reflectionMethod->getParameters() as $reflectionParameter) { 61 $method->setParameter(ParameterGenerator::fromReflection($reflectionParameter)); 62 } 63 64 $method->setBody(static::clearBodyIndention($reflectionMethod->getBody())); 65 66 return $method; 67 } 68 69 /** 70 * Identify the space indention from the first line and remove this indention 71 * from all lines 72 * 73 * @param string $body 74 * 75 * @return string 76 */ 77 protected static function clearBodyIndention($body) 78 { 79 if (empty($body)) { 80 return $body; 81 } 82 83 $lines = explode(PHP_EOL, $body); 84 85 $indention = str_replace(trim($lines[1]), '', $lines[1]); 86 87 foreach ($lines as $key => $line) { 88 if (substr($line, 0, strlen($indention)) == $indention) { 89 $lines[$key] = substr($line, strlen($indention)); 90 } 91 } 92 93 $body = implode(PHP_EOL, $lines); 94 95 return $body; 96 } 97 98 /** 99 * Generate from array 100 * 101 * @configkey name string [required] Class Name 102 * @configkey docblock string The docblock information 103 * @configkey flags int Flags, one of MethodGenerator::FLAG_ABSTRACT MethodGenerator::FLAG_FINAL 104 * @configkey parameters string Class which this class is extending 105 * @configkey body string 106 * @configkey abstract bool 107 * @configkey final bool 108 * @configkey static bool 109 * @configkey visibility string 110 * 111 * @throws Exception\InvalidArgumentException 112 * @param array $array 113 * @return MethodGenerator 114 */ 115 public static function fromArray(array $array) 116 { 117 if (!isset($array['name'])) { 118 throw new Exception\InvalidArgumentException( 119 'Method generator requires that a name is provided for this object' 120 ); 121 } 122 123 $method = new static($array['name']); 124 foreach ($array as $name => $value) { 125 // normalize key 126 switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) { 127 case 'docblock': 128 $docBlock = ($value instanceof DocBlockGenerator) ? $value : DocBlockGenerator::fromArray($value); 129 $method->setDocBlock($docBlock); 130 break; 131 case 'flags': 132 $method->setFlags($value); 133 break; 134 case 'parameters': 135 $method->setParameters($value); 136 break; 137 case 'body': 138 $method->setBody($value); 139 break; 140 case 'abstract': 141 $method->setAbstract($value); 142 break; 143 case 'final': 144 $method->setFinal($value); 145 break; 146 case 'static': 147 $method->setStatic($value); 148 break; 149 case 'visibility': 150 $method->setVisibility($value); 151 break; 152 } 153 } 154 155 return $method; 156 } 157 158 /** 159 * @param string $name 160 * @param array $parameters 161 * @param int $flags 162 * @param string $body 163 * @param DocBlockGenerator|string $docBlock 164 */ 165 public function __construct( 166 $name = null, 167 array $parameters = array(), 168 $flags = self::FLAG_PUBLIC, 169 $body = null, 170 $docBlock = null 171 ) { 172 if ($name) { 173 $this->setName($name); 174 } 175 if ($parameters) { 176 $this->setParameters($parameters); 177 } 178 if ($flags !== self::FLAG_PUBLIC) { 179 $this->setFlags($flags); 180 } 181 if ($body) { 182 $this->setBody($body); 183 } 184 if ($docBlock) { 185 $this->setDocBlock($docBlock); 186 } 187 } 188 189 /** 190 * @param array $parameters 191 * @return MethodGenerator 192 */ 193 public function setParameters(array $parameters) 194 { 195 foreach ($parameters as $parameter) { 196 $this->setParameter($parameter); 197 } 198 199 return $this; 200 } 201 202 /** 203 * @param ParameterGenerator|array|string $parameter 204 * @throws Exception\InvalidArgumentException 205 * @return MethodGenerator 206 */ 207 public function setParameter($parameter) 208 { 209 if (is_string($parameter)) { 210 $parameter = new ParameterGenerator($parameter); 211 } 212 213 if (is_array($parameter)) { 214 $parameter = ParameterGenerator::fromArray($parameter); 215 } 216 217 if (!$parameter instanceof ParameterGenerator) { 218 throw new Exception\InvalidArgumentException(sprintf( 219 '%s is expecting either a string, array or an instance of %s\ParameterGenerator', 220 __METHOD__, 221 __NAMESPACE__ 222 )); 223 } 224 225 $this->parameters[$parameter->getName()] = $parameter; 226 227 return $this; 228 } 229 230 /** 231 * @return ParameterGenerator[] 232 */ 233 public function getParameters() 234 { 235 return $this->parameters; 236 } 237 238 /** 239 * @param string $body 240 * @return MethodGenerator 241 */ 242 public function setBody($body) 243 { 244 $this->body = $body; 245 return $this; 246 } 247 248 /** 249 * @return string 250 */ 251 public function getBody() 252 { 253 return $this->body; 254 } 255 256 /** 257 * @return string 258 */ 259 public function generate() 260 { 261 $output = ''; 262 263 $indent = $this->getIndentation(); 264 265 if (($docBlock = $this->getDocBlock()) !== null) { 266 $docBlock->setIndentation($indent); 267 $output .= $docBlock->generate(); 268 } 269 270 $output .= $indent; 271 272 if ($this->isAbstract()) { 273 $output .= 'abstract '; 274 } else { 275 $output .= (($this->isFinal()) ? 'final ' : ''); 276 } 277 278 $output .= $this->getVisibility() 279 . (($this->isStatic()) ? ' static' : '') 280 . ' function ' . $this->getName() . '('; 281 282 $parameters = $this->getParameters(); 283 if (!empty($parameters)) { 284 foreach ($parameters as $parameter) { 285 $parameterOutput[] = $parameter->generate(); 286 } 287 288 $output .= implode(', ', $parameterOutput); 289 } 290 291 $output .= ')'; 292 293 if ($this->isAbstract()) { 294 return $output . ';'; 295 } 296 297 $output .= self::LINE_FEED . $indent . '{' . self::LINE_FEED; 298 299 if ($this->body) { 300 $output .= preg_replace('#^((?![a-zA-Z0-9_-]+;).+?)$#m', $indent . $indent . '$1', trim($this->body)) 301 . self::LINE_FEED; 302 } 303 304 $output .= $indent . '}' . self::LINE_FEED; 305 306 return $output; 307 } 308 309 public function __toString() 310 { 311 return $this->generate(); 312 } 313 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |