4
* This file is part of the Nette Framework (http://nette.org)
6
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
8
* For the full copyright and license information, please view
9
* the file license.txt that was distributed with this source code.
12
namespace Nette\Utils\PhpGenerator;
18
* Class/Interface/Trait description.
22
* @method ClassType setName(string $name)
23
* @method ClassType setType(string $type)
24
* @method ClassType setFinal(bool $on)
25
* @method ClassType setAbstract(bool $on)
26
* @method ClassType addExtend(string $class)
27
* @method ClassType addImplement(string $interface)
28
* @method ClassType addTrait(string $trait)
29
* @method ClassType addDocument(string $doc)
31
class ClassType extends Nette\Object
36
/** @var string class|interface|trait */
37
public $type = 'class';
46
public $extends = array();
49
public $implements = array();
52
public $traits = array();
55
public $documents = array();
57
/** @var mixed[] name => value */
58
public $consts = array();
60
/** @var Property[] name => Property */
61
public $properties = array();
63
/** @var Method[] name => Method */
64
public $methods = array();
67
public function __construct($name = NULL)
73
/** @return ClassType */
74
public function addConst($name, $value)
76
$this->consts[$name] = $value;
81
/** @return Property */
82
public function addProperty($name, $value = NULL)
84
$property = new Property;
85
return $this->properties[$name] = $property->setName($name)->setValue($value);
90
public function addMethod($name)
93
if ($this->type === 'interface') {
94
$method->setVisibility('')->setBody(FALSE);
96
$method->setVisibility('public');
98
return $this->methods[$name] = $method->setName($name);
102
public function __call($name, $args)
104
return Nette\ObjectMixin::callProperty($this, $name, $args);
108
/** @return string PHP code */
109
public function __toString()
112
foreach ($this->consts as $name => $value) {
113
$consts[] = "const $name = " . Helpers::dump($value) . ";\n";
115
$properties = array();
116
foreach ($this->properties as $property) {
117
$properties[] = ($property->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $property->documents)) . "\n */\n" : '')
118
. $property->visibility . ($property->static ? ' static' : '') . ' $' . $property->name
119
. ($property->value === NULL ? '' : ' = ' . Helpers::dump($property->value))
122
return Nette\Utils\Strings::normalize(
123
($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n" : '')
124
. ($this->abstract ? 'abstract ' : '')
125
. ($this->final ? 'final ' : '')
128
. ($this->extends ? 'extends ' . implode(', ', (array) $this->extends) . ' ' : '')
129
. ($this->implements ? 'implements ' . implode(', ', (array) $this->implements) . ' ' : '')
131
. Nette\Utils\Strings::indent(
132
($this->traits ? "use " . implode(', ', (array) $this->traits) . ";\n\n" : '')
133
. ($this->consts ? implode('', $consts) . "\n\n" : '')
134
. ($this->properties ? implode("\n", $properties) . "\n\n" : '')
135
. implode("\n\n\n", $this->methods), 1)