Overview

Namespaces

  • Budovy
  • Kdyby
    • BootstrapFormRenderer
      • DI
      • Latte
  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • Nextras
    • Datagrid
  • None
  • PHP
  • Tester
    • CodeCoverage
    • Runner
      • Output
  • Vodacek
    • Forms
      • Controls
  • WebLoader
    • Filter
    • Nette

Classes

  • ClassType
  • Helpers
  • Method
  • Parameter
  • PhpLiteral
  • Property
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\PhpGenerator;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * Class method description.
 15:  *
 16:  * @author     David Grudl
 17:  *
 18:  * @method Method setName(string)
 19:  * @method string getName()
 20:  * @method Method setParameters(Parameter[])
 21:  * @method Parameter[] getParameters()
 22:  * @method Method setUses(array)
 23:  * @method array getUses()
 24:  * @method string getBody()
 25:  * @method Method setStatic(bool)
 26:  * @method bool isStatic()
 27:  * @method Method setVisibility(string)
 28:  * @method string getVisibility()
 29:  * @method Method setFinal(bool)
 30:  * @method bool isFinal()
 31:  * @method Method setAbstract(bool)
 32:  * @method bool isAbstract()
 33:  * @method Method setReturnReference(bool)
 34:  * @method bool getReturnReference()
 35:  * @method Method setDocuments(string[])
 36:  * @method string[] getDocuments()
 37:  * @method Method addDocument(string)
 38:  */
 39: class Method extends Nette\Object
 40: {
 41:     /** @var string */
 42:     private $name;
 43: 
 44:     /** @var array of name => Parameter */
 45:     private $parameters = array();
 46: 
 47:     /** @var array of name => bool */
 48:     private $uses = array();
 49: 
 50:     /** @var string|FALSE */
 51:     private $body;
 52: 
 53:     /** @var bool */
 54:     private $static;
 55: 
 56:     /** @var string  public|protected|private or none */
 57:     private $visibility;
 58: 
 59:     /** @var bool */
 60:     private $final;
 61: 
 62:     /** @var bool */
 63:     private $abstract;
 64: 
 65:     /** @var bool */
 66:     private $returnReference;
 67: 
 68:     /** @var array of string */
 69:     private $documents = array();
 70: 
 71: 
 72:     /** @return Method */
 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:     /** @return Parameter */
 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:     /** @return Parameter */
104:     public function addUse($name)
105:     {
106:         $param = new Parameter;
107:         return $this->uses[] = $param->setName($name);
108:     }
109: 
110: 
111:     /** @return Method */
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:     /** @return Method */
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:     /** @return string  PHP code */
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: 
API documentation generated by ApiGen 2.8.0