Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • ClassType
  • Helpers
  • Method
  • Parameter
  • PhpLiteral
  • Property
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\PhpGenerator;
  9: 
 10: use Nette;
 11: use Nette\Utils\Strings;
 12: 
 13: 
 14: /**
 15:  * Class/Interface/Trait description.
 16:  *
 17:  * @author     David Grudl
 18:  *
 19:  * @method ClassType setName(string)
 20:  * @method string getName()
 21:  * @method ClassType setType(string)
 22:  * @method string getType()
 23:  * @method ClassType setFinal(bool)
 24:  * @method bool isFinal()
 25:  * @method ClassType setAbstract(bool)
 26:  * @method bool isAbstract()
 27:  * @method ClassType setExtends(string[]|string)
 28:  * @method string[]|string getExtends()
 29:  * @method ClassType addExtend(string)
 30:  * @method ClassType setImplements(string[])
 31:  * @method string[] getImplements()
 32:  * @method ClassType addImplement(string)
 33:  * @method ClassType setTraits(string[])
 34:  * @method string[] getTraits()
 35:  * @method ClassType addTrait(string)
 36:  * @method ClassType setDocuments(string[])
 37:  * @method string[] getDocuments()
 38:  * @method ClassType addDocument(string)
 39:  * @method ClassType setConsts(scalar[])
 40:  * @method scalar[] getConsts()
 41:  * @method ClassType setProperties(Property[])
 42:  * @method Property[] getProperties()
 43:  * @method ClassType setMethods(Method[])
 44:  * @method Method[] getMethods()
 45:  */
 46: class ClassType extends Nette\Object
 47: {
 48:     /** @var string */
 49:     private $name;
 50: 
 51:     /** @var string  class|interface|trait */
 52:     private $type = 'class';
 53: 
 54:     /** @var bool */
 55:     private $final;
 56: 
 57:     /** @var bool */
 58:     private $abstract;
 59: 
 60:     /** @var string[]|string */
 61:     private $extends = array();
 62: 
 63:     /** @var string[] */
 64:     private $implements = array();
 65: 
 66:     /** @var string[] */
 67:     private $traits = array();
 68: 
 69:     /** @var string[] */
 70:     private $documents = array();
 71: 
 72:     /** @var mixed[] name => value */
 73:     private $consts = array();
 74: 
 75:     /** @var Property[] name => Property */
 76:     private $properties = array();
 77: 
 78:     /** @var Method[] name => Method */
 79:     private $methods = array();
 80: 
 81: 
 82:     /** @return ClassType */
 83:     public static function from($from)
 84:     {
 85:         $from = $from instanceof \ReflectionClass ? $from : new \ReflectionClass($from);
 86:         $class = new static($from->getShortName());
 87:         $class->type = $from->isInterface() ? 'interface' : (PHP_VERSION_ID >= 50400 && $from->isTrait() ? 'trait' : 'class');
 88:         $class->final = $from->isFinal();
 89:         $class->abstract = $from->isAbstract() && $class->type === 'class';
 90:         $class->implements = $from->getInterfaceNames();
 91:         $class->documents = preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"));
 92:         $namespace = $from->getNamespaceName();
 93:         if ($from->getParentClass()) {
 94:             $class->extends = $from->getParentClass()->getName();
 95:             if ($namespace) {
 96:                 $class->extends = Strings::startsWith($class->extends, "$namespace\\") ? substr($class->extends, strlen($namespace) + 1) : '\\' . $class->extends;
 97:             }
 98:             $class->implements = array_diff($class->implements, $from->getParentClass()->getInterfaceNames());
 99:         }
100:         if ($namespace) {
101:             foreach ($class->implements as & $interface) {
102:                 $interface = Strings::startsWith($interface, "$namespace\\") ? substr($interface, strlen($namespace) + 1) : '\\' . $interface;
103:             }
104:         }
105:         foreach ($from->getProperties() as $prop) {
106:             if ($prop->getDeclaringClass() == $from) { // intentionally ==
107:                 $class->properties[$prop->getName()] = Property::from($prop);
108:             }
109:         }
110:         foreach ($from->getMethods() as $method) {
111:             if ($method->getDeclaringClass() == $from) { // intentionally ==
112:                 $class->methods[$method->getName()] = Method::from($method);
113:             }
114:         }
115:         return $class;
116:     }
117: 
118: 
119:     public function __construct($name = NULL)
120:     {
121:         $this->name = $name;
122:     }
123: 
124: 
125:     /** @return ClassType */
126:     public function addConst($name, $value)
127:     {
128:         $this->consts[$name] = $value;
129:         return $this;
130:     }
131: 
132: 
133:     /** @return Property */
134:     public function addProperty($name, $value = NULL)
135:     {
136:         $property = new Property;
137:         return $this->properties[$name] = $property->setName($name)->setValue($value);
138:     }
139: 
140: 
141:     /** @return Method */
142:     public function addMethod($name)
143:     {
144:         $method = new Method;
145:         if ($this->type === 'interface') {
146:             $method->setVisibility('')->setBody(FALSE);
147:         } else {
148:             $method->setVisibility('public');
149:         }
150:         return $this->methods[$name] = $method->setName($name);
151:     }
152: 
153: 
154:     /** @return string  PHP code */
155:     public function __toString()
156:     {
157:         $consts = array();
158:         foreach ($this->consts as $name => $value) {
159:             $consts[] = "const $name = " . Helpers::dump($value) . ";\n";
160:         }
161:         $properties = array();
162:         foreach ($this->properties as $property) {
163:             $properties[] = ($property->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $property->documents)) . "\n */\n" : '')
164:                 . $property->visibility . ($property->static ? ' static' : '') . ' $' . $property->name
165:                 . ($property->value === NULL ? '' : ' = ' . Helpers::dump($property->value))
166:                 . ";\n";
167:         }
168:         return Strings::normalize(
169:             ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n" : '')
170:             . ($this->abstract ? 'abstract ' : '')
171:             . ($this->final ? 'final ' : '')
172:             . $this->type . ' '
173:             . $this->name . ' '
174:             . ($this->extends ? 'extends ' . implode(', ', (array) $this->extends) . ' ' : '')
175:             . ($this->implements ? 'implements ' . implode(', ', (array) $this->implements) . ' ' : '')
176:             . "\n{\n\n"
177:             . Strings::indent(
178:                 ($this->traits ? "use " . implode(', ', (array) $this->traits) . ";\n\n" : '')
179:                 . ($this->consts ? implode('', $consts) . "\n\n" : '')
180:                 . ($this->properties ? implode("\n", $properties) . "\n\n" : '')
181:                 . implode("\n\n\n", $this->methods), 1)
182:             . "\n\n}") . "\n";
183:     }
184: 
185: }
186: 
Nette 2.2 API documentation generated by ApiGen 2.8.0