1 | <?php |
---|
2 | |
---|
3 | require_once('DojoBlock.php'); |
---|
4 | require_once('DojoFunctionDeclare.php'); |
---|
5 | require_once('DojoFunctionBody.php'); |
---|
6 | require_once('Text.php'); |
---|
7 | |
---|
8 | class DojoObject extends DojoBlock |
---|
9 | { |
---|
10 | private $object = 'DojoObject'; |
---|
11 | |
---|
12 | private $values = array(); |
---|
13 | public $declarations = array(); |
---|
14 | private $name = ''; |
---|
15 | private $body; |
---|
16 | private $extra_block_values = array(); |
---|
17 | private $anonymous = false; |
---|
18 | |
---|
19 | public function __construct($package, $line_number = false, $position = false){ |
---|
20 | parent::__construct($package, $line_number, $position); |
---|
21 | $this->body = new DojoFunctionBody($package, $line_number, $position); |
---|
22 | } |
---|
23 | |
---|
24 | public function __destruct() { |
---|
25 | parent::__destruct(); |
---|
26 | unset($this->values); |
---|
27 | unset($this->declarations); |
---|
28 | unset($this->body); |
---|
29 | unset($this->extra_block_values); |
---|
30 | unset($this->anonymous); |
---|
31 | } |
---|
32 | |
---|
33 | public function setName($name){ |
---|
34 | $this->name = $name; |
---|
35 | } |
---|
36 | |
---|
37 | public function getName(){ |
---|
38 | return $this->name; |
---|
39 | } |
---|
40 | |
---|
41 | public function setAnonymous($anonymous){ |
---|
42 | $this->anonymous = true; |
---|
43 | } |
---|
44 | |
---|
45 | public function isAnonymous(){ |
---|
46 | return $this->anonymous; |
---|
47 | } |
---|
48 | |
---|
49 | public function getBlockCommentKeys(){ |
---|
50 | return $this->body->getBlockCommentKeys(); |
---|
51 | } |
---|
52 | |
---|
53 | public function getBlockComment($key){ |
---|
54 | return $this->body->getBlockComment($key); |
---|
55 | } |
---|
56 | |
---|
57 | public function addBlockCommentKey($key){ |
---|
58 | return $this->body->addBlockCommentKey($key); |
---|
59 | } |
---|
60 | |
---|
61 | public function addBlockCommentKeySet($key){ |
---|
62 | return $this->body->addBlockCOmmentKeySet($key); |
---|
63 | } |
---|
64 | |
---|
65 | public function build(){ |
---|
66 | if(!$this->start){ |
---|
67 | die("DojoObject->build() used before setting a start position"); |
---|
68 | } |
---|
69 | |
---|
70 | $lines = Text::chop($this->package->getCode(), $this->start[0], $this->start[1], false, false, true); |
---|
71 | $end = array($this->start[0], $this->start[1]); |
---|
72 | |
---|
73 | do { |
---|
74 | $lines = Text::chop($this->package->getCode(), $end[0], $end[1], false, false, true); |
---|
75 | foreach ($lines as $line_number => $line) { |
---|
76 | if (preg_match('%^\s*}%', $line)) { |
---|
77 | break; |
---|
78 | } |
---|
79 | if (preg_match('%^(\s*)([a-zA-Z0-9_$]+|"\s+")\s*:%', $line, $match)) { |
---|
80 | if ($end[0] != $this->start[0] && $end[1] != $this->start[1]) { |
---|
81 | if ($end[0]+1 > $line_number) { |
---|
82 | continue; |
---|
83 | } |
---|
84 | $between_lines = Text::chop($this->package->getSource(), $end[0]+1, 0, $line_number, strlen($match[1]), true); |
---|
85 | foreach ($between_lines as $between_line) { |
---|
86 | $this->body->addBlockCommentLine($between_line); |
---|
87 | } |
---|
88 | } |
---|
89 | $end = array($line_number, strlen($match[0])); |
---|
90 | if ($match[2]{0} == '"' || $match[2]{0} == "'") { |
---|
91 | $key = trim(implode(Text::chop($this->package->getSource(), $line_number, strpos($line, '"') + 1, $line_number, strlen($match[0]) - 3, false))); |
---|
92 | }else{ |
---|
93 | $key = $match[2]; |
---|
94 | } |
---|
95 | break; |
---|
96 | } |
---|
97 | } |
---|
98 | if (!$key) { |
---|
99 | $end = Text::findTermination($lines, '}'); |
---|
100 | }else{ |
---|
101 | $parameter = new DojoParameter($this->package, $end[0], $end[1], '}'); |
---|
102 | $end = $parameter->build(); |
---|
103 | $this->values[$key][] = $parameter; |
---|
104 | } |
---|
105 | } |
---|
106 | while ($lines[$end[0]]{$end[1]} != '}'); |
---|
107 | |
---|
108 | $this->setEnd($end[0], $end[1]); |
---|
109 | return $end; |
---|
110 | } |
---|
111 | |
---|
112 | public function getKeys(){ |
---|
113 | if (!$this->values) { |
---|
114 | $this->build(); |
---|
115 | } |
---|
116 | return array_keys($this->values); |
---|
117 | } |
---|
118 | |
---|
119 | public function getValues(){ |
---|
120 | if(!$this->values){ |
---|
121 | $this->build(); |
---|
122 | } |
---|
123 | return $this->values; |
---|
124 | } |
---|
125 | |
---|
126 | public function rollOut(&$output, $item_type = 'Object'){ |
---|
127 | $package_name = $this->package->getPackageName(); |
---|
128 | $name = $this->getName(); |
---|
129 | $variables = array(); |
---|
130 | $check_keys = array('summary','description'); |
---|
131 | |
---|
132 | foreach($this->getValues() as $key => $values){ |
---|
133 | foreach ($values as $value) { |
---|
134 | if($value->isA(DojoFunctionDeclare)){ |
---|
135 | $function = $value->getFunction(); |
---|
136 | $this->declarations[] = $function; |
---|
137 | if(!$function->isConstructor()){ |
---|
138 | $function->setFunctionName("{$name}.{$key}"); |
---|
139 | $function->rollOut($output); |
---|
140 | } |
---|
141 | }elseif ($value->isA(DojoObject)){ |
---|
142 | $object = $value->getObject(); |
---|
143 | $object->setName("{$name}.{$key}"); |
---|
144 | $object->rollOut($output); |
---|
145 | }else{ |
---|
146 | $this->addBlockCommentKey($key); |
---|
147 | $full_variable_name = "{$name}.{$key}"; |
---|
148 | if (empty($output[$full_variable_name])) { |
---|
149 | $output[$full_variable_name] = array(); |
---|
150 | } |
---|
151 | $variables[] = $key; |
---|
152 | } |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | foreach($check_keys as $ck){ |
---|
157 | $this->addBlockCommentKey($ck); |
---|
158 | } |
---|
159 | $this->addBlockCommentKeySet("example"); |
---|
160 | |
---|
161 | $output[$name]['type'] = $item_type; |
---|
162 | if ($comment = $this->getBlockComment('summary')) { |
---|
163 | $output[$name]['summary'] = $comment; |
---|
164 | } |
---|
165 | if ($comment = $this->getBlockComment('description')) { |
---|
166 | $output[$name]['description'] = $comment; |
---|
167 | } |
---|
168 | $examples = $this->getBlockComment('example'); |
---|
169 | if ($examples && count($examples)) { |
---|
170 | $output[$name]['examples'] = $examples; |
---|
171 | } |
---|
172 | |
---|
173 | foreach($variables as $key){ |
---|
174 | $full_variable_name = "{$name}.{$key}"; |
---|
175 | if($comment = $this->getBlockComment($key)){ |
---|
176 | list($tags, $parameter_type, $options, $summary) = DojoFunctionDeclare::parseVariable($comment); |
---|
177 | if (!empty($tags)) { |
---|
178 | $output[$full_variable_name]['tags'] = $tags; |
---|
179 | } |
---|
180 | if (!empty($parameter_type)) { |
---|
181 | $output[$full_variable_name]['type'] = $parameter_type; |
---|
182 | } |
---|
183 | $output[$full_variable_name]['summary'] = $summary; |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | foreach($check_keys as $ck){ |
---|
188 | if(!$this->isAnonymous() && $comment = $this->getBlockComment($ck)){ |
---|
189 | $output[$name][$ck] = $comment; |
---|
190 | } |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | public function removeCodeFrom($lines){ |
---|
195 | for($i = $this->start[0]; $i <= $this->end[0]; $i++){ |
---|
196 | $line = $lines[$i]; |
---|
197 | if($i == $this->start[0]){ |
---|
198 | $lines[$i] = Text::blankOutAt($line, $this->start[1]); |
---|
199 | }elseif ($i == $this->end[0]){ |
---|
200 | $lines[$i] = Text::blankOutAt($line, 0, $this->end[1]); |
---|
201 | }else{ |
---|
202 | $lines[$i] = Text::blankOut($line, $line); |
---|
203 | } |
---|
204 | } |
---|
205 | return $lines; |
---|
206 | } |
---|
207 | } |
---|
208 | |
---|
209 | ?> |
---|