~ubuntu-branches/debian/experimental/php-nette/experimental

« back to all changes in this revision

Viewing changes to Nette-2.0.13/Nette/Utils/PhpGenerator/ClassType.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2013-11-30 08:47:54 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20131130084754-4udf1xsu9085tnfc
Tags: 2.1.0~rc-1
* New upstream branch
* Update copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
 
3
 
/**
4
 
 * This file is part of the Nette Framework (http://nette.org)
5
 
 *
6
 
 * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7
 
 *
8
 
 * For the full copyright and license information, please view
9
 
 * the file license.txt that was distributed with this source code.
10
 
 */
11
 
 
12
 
namespace Nette\Utils\PhpGenerator;
13
 
 
14
 
use Nette;
15
 
 
16
 
 
17
 
/**
18
 
 * Class/Interface/Trait description.
19
 
 *
20
 
 * @author     David Grudl
21
 
 *
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)
30
 
 */
31
 
class ClassType extends Nette\Object
32
 
{
33
 
        /** @var string */
34
 
        public $name;
35
 
 
36
 
        /** @var string  class|interface|trait */
37
 
        public $type = 'class';
38
 
 
39
 
        /** @var bool */
40
 
        public $final;
41
 
 
42
 
        /** @var bool */
43
 
        public $abstract;
44
 
 
45
 
        /** @var string[] */
46
 
        public $extends = array();
47
 
 
48
 
        /** @var string[] */
49
 
        public $implements = array();
50
 
 
51
 
        /** @var string[] */
52
 
        public $traits = array();
53
 
 
54
 
        /** @var string[] */
55
 
        public $documents = array();
56
 
 
57
 
        /** @var mixed[] name => value */
58
 
        public $consts = array();
59
 
 
60
 
        /** @var Property[] name => Property */
61
 
        public $properties = array();
62
 
 
63
 
        /** @var Method[] name => Method */
64
 
        public $methods = array();
65
 
 
66
 
 
67
 
        public function __construct($name = NULL)
68
 
        {
69
 
                $this->name = $name;
70
 
        }
71
 
 
72
 
 
73
 
        /** @return ClassType */
74
 
        public function addConst($name, $value)
75
 
        {
76
 
                $this->consts[$name] = $value;
77
 
                return $this;
78
 
        }
79
 
 
80
 
 
81
 
        /** @return Property */
82
 
        public function addProperty($name, $value = NULL)
83
 
        {
84
 
                $property = new Property;
85
 
                return $this->properties[$name] = $property->setName($name)->setValue($value);
86
 
        }
87
 
 
88
 
 
89
 
        /** @return Method */
90
 
        public function addMethod($name)
91
 
        {
92
 
                $method = new Method;
93
 
                if ($this->type === 'interface') {
94
 
                        $method->setVisibility('')->setBody(FALSE);
95
 
                } else {
96
 
                        $method->setVisibility('public');
97
 
                }
98
 
                return $this->methods[$name] = $method->setName($name);
99
 
        }
100
 
 
101
 
 
102
 
        public function __call($name, $args)
103
 
        {
104
 
                return Nette\ObjectMixin::callProperty($this, $name, $args);
105
 
        }
106
 
 
107
 
 
108
 
        /** @return string  PHP code */
109
 
        public function __toString()
110
 
        {
111
 
                $consts = array();
112
 
                foreach ($this->consts as $name => $value) {
113
 
                        $consts[] = "const $name = " . Helpers::dump($value) . ";\n";
114
 
                }
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))
120
 
                                . ";\n";
121
 
                }
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 ' : '')
126
 
                        . $this->type . ' '
127
 
                        . $this->name . ' '
128
 
                        . ($this->extends ? 'extends ' . implode(', ', (array) $this->extends) . ' ' : '')
129
 
                        . ($this->implements ? 'implements ' . implode(', ', (array) $this->implements) . ' ' : '')
130
 
                        . "\n{\n\n"
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)
136
 
                        . "\n\n}") . "\n";
137
 
        }
138
 
 
139
 
}