root / trunk / docscripts / inc / DojoFunctionBody.php

Revision 6373, 6.3 kB (checked in by pottedmeat, 2 years ago)

Properly handle instantiated functions. Make sure things output in function_names, aliasing, and bug fixes

Line 
1<?php
2
3require_once('DojoBlock.php');
4
5class DojoFunctionBody extends DojoBlock
6{
7    private $object = 'DojoFunctionBody';
8
9  private $comment_end;
10
11  private $keys = array();
12  private $comments = array();
13  private $return_comments = array();
14  private $instance_variables = array();
15  private $this_inheritance_calls = array();
16  private $extra_initial_comment_block = array();
17 
18  public function build()
19  {
20        if (!$this->start) {
21      die("DojoFunctionBody->build() used before setting a start position");
22    }
23    if ($this->end) {
24      return $this->end;
25    }
26       
27        $balance = 0;
28        $start_position = $this->start[1];
29        $lines = Text::chop($this->package->getCode(), $this->start[0], $this->start[1], false, false, true);
30    return $this->end = Text::findTermination($lines, '}', '{}');
31  }
32 
33  public function addBlockCommentLine($line)
34  {
35    if (trim($line) != '') {
36      $this->extra_initial_comment_block[] = $line;
37    }
38  }
39 
40  public function addBlockCommentKey($key)
41  {
42    if ($key) {
43      $this->keys[] = $key;
44    }
45  }
46 
47  public function getSource()
48  {
49    $this->getBlockCommentKeys();
50    $source = array();
51    $lines = Text::chop($this->package->getSource(), $this->comment_end[0], $this->comment_end[1], $this->end[0], $this->end[1], true);
52    foreach ($lines as $line_number => $line) {
53      $trimmed_line = trim($line);
54      if ($trimmed_line === '') continue;
55      $source[] = $line;
56    }
57    return implode("\n", $source);
58  }
59 
60  public function getBlockComment($key)
61  {
62    $value = '';
63    $this->getBlockCommentKeys();
64    if (!empty($this->comments[$key])) {
65      $value = preg_replace('%\s+%', ' ', trim($this->comments[$key]));
66    }
67    return $value;
68  }
69 
70  public function getBlockCommentKeys()
71  {
72    if ($this->comments) {
73      return array_keys($this->comments);
74    }
75   
76    $this->build();
77
78    $expression = '%^\b(' . implode('|', $this->keys) . ')\b\W*%';
79    $buffer = array();
80    $key = '';
81    $started = false;
82   
83    $lines = Text::chop($this->package->getSource(), $this->start[0], $this->start[1], $this->end[0], $this->end[1], true);
84    for ($i = 0; $i < 2; $i++) {
85      if ($i == 1) {
86        $lines = $this->extra_initial_comment_block;
87      }
88      foreach ($lines as $line_number =>  $line) {
89        list($comment, , , $data, $multiline) = Text::findComments($line, $multiline);
90       
91        if ($started && $comment === false) {
92          $this->comment_end = array($line_number, 0);
93          break;
94        }
95        elseif ($comment) {
96          $started = true;
97        }
98       
99        if (preg_match($expression, $comment, $match)) {
100          if ($buffer && $key) {
101            $this->comments[$key] = implode(' ', $buffer);
102            $buffer = array();
103          }
104          $key = $match[1];
105          if ($match[0] == $comment) {
106            $comment = '';
107          }
108          else {
109            $comment = substr($comment, strlen($match[0]));
110          }
111        }
112       
113        if ($data) {
114          $this->comment_end = array($line_number, 0);
115          break;
116        }
117       
118        if ($comment !== '') {
119          $buffer[] = $comment;
120        }
121      }
122     
123      if ($buffer && $key) {
124        $this->comments[$key] = implode(' ', $buffer);
125      }
126 
127      if ($i == 0 && !$this->comment_end) {
128        $this->comment_end = $this->start;
129      }
130    }
131
132    return array_keys($this->comments);
133  }
134 
135  public function getInstanceVariableNames()
136  {
137    if ($this->instance_variables) {
138      return $this->instance_variables;
139    }
140   
141    $this->build();
142    $lines = Text::chop($this->package->getCode(), $this->start[0], $this->start[1], $this->end[0], $this->end[1], true);
143    foreach ($lines as $line) {
144      if (preg_match('%\bthis\.([a-zA-Z0-9._$]+)\s*=\s*(?!function)%', $line, $match)) {
145        $this->instance_variables[] = $match[1];
146      }
147    }
148    return $this->instance_variables;
149  }
150 
151  public function getInstanceFunctions($function_name)
152  {
153    $functions = array();
154    $this->build();
155    $lines = Text::chop($this->package->getCode(), $this->start[0], $this->start[1], $this->end[0], $this->end[1], true);
156    foreach ($lines as $line_number => $line) {
157      if (preg_match('%\bthis\.([a-zA-Z0-9._$]+)\s*=\s*function\b%', $line, $match, PREG_OFFSET_CAPTURE)) {
158        $function = new DojoFunctionDeclare($this->package, $line_number, $match[0][1]);
159                $function->setFunctionName($function_name);
160        $end = $function->build();
161        $functions[] = $function;
162      }
163    }
164    return $functions;
165  }
166 
167  public function getReturnComments()
168  {
169    if ($this->return_comments) {
170      return $this->return_comments;
171    }
172   
173    $buffer = array();
174    $this->getBlockCommentKeys();
175    $lines = Text::chop($this->package->getSource(), $this->comment_end[0], $this->comment_end[1], $this->end[0], $this->end[1], true);
176    foreach ($lines as $line) {
177      if ($multiline) {
178        list($first, $middle, $last, $data, $multiline) = Text::findComments($line, $multiline);
179        if ($first) {
180          $buffer[] = trim($first);
181        }
182        if ($data) {
183          $multiline = false;
184          if ($buffer) {
185            $this->return_comments[] = implode(' ', array_diff($buffer, array('')));
186            $buffer = array();
187          }
188        }
189      }
190      if (strpos($line, 'return') !== false) {
191        if ($data && $buffer) {
192          $this->return_comments[] = implode(' ', array_diff($buffer, array('')));
193          $buffer = array();
194        }
195        list($first, $middle, $last, $data, $multiline) = Text::findComments($line, $multiline);
196        if ($last) {
197          $buffer[] = $last;
198        }
199      }
200    }
201   
202    if ($data && $buffer) {
203      $this->return_comments[] = implode(' ', array_diff($buffer, array('')));
204    }
205   
206    $this->return_comment = array_unique($this->return_comments);
207   
208    return $this->return_comments;
209  }
210 
211  public function getThisInheritanceCalls()
212  {
213    if ($this->this_inheritance_calls) {
214      return $this->this_inheritance_calls;
215    }
216   
217    $this->build();
218    $lines = Text::chop($this->package->getCode(), $this->start[0], $this->start[1], $this->end[0], $this->end[1], true);
219    foreach ($lines as $line) {
220      if (preg_match('%\b([a-zA-Z0-9_.$]+)\.(?:apply|call)\s*\(%', $line, $match)) {
221        $this->this_inheritance_calls[] = $match[1];
222      }
223    }
224    return $this->this_inheritance_calls;
225  }
226
227}
228 
229?>
Note: See TracBrowser for help on using the browser.