root / trunk / docscripts / docparser.php

Revision 7844, 11.7 kB (checked in by pottedmeat, 20 months ago)

Fixes #2673. Changed a regex and a dojo.declare permutation

  • Property svn:executable set to *
Line 
1<?php
2
3require_once('inc/Dojo.php');
4require_once('inc/DojoPackage.php');
5require_once('inc/Plugins.php');
6require_once('inc/helpers.inc');
7require_once('lib/benchmark/Timer.php');
8
9$output = array();
10
11$timer = new Benchmark_Timer();
12$timer->start();
13
14$dojo = new Dojo('../');
15$files = $dojo->getFileList();
16
17foreach ($files as $file) {
18  //echo "Parsing  $file ...<br>\n";
19    //flush();
20    //ob_flush();
21
22  $package = new DojoPackage($dojo, $file);
23  $package_name = $package->getPackageName();
24
25  $compound_calls = $package->getFunctionCalls('dojo.kwCompoundRequire');
26  $require_calls = $package->getFunctionCalls('dojo.require');
27  $require_if_calls = array_merge($package->getFunctionCalls('dojo.requireIf'), $package->getFunctionCalls('dojo.requireAfterIf'));
28  $declare_calls = array_merge($package->getFunctionCalls('dojo.declare'), $package->getFunctionCalls('dojo.widget.defineWidget'));
29  $inherit_calls = $package->getFunctionCalls('dojo.inherits', true);
30  $mixin_calls = array_merge($package->getFunctionCalls('dojo.extend'), $package->getFunctionCalls('dojo.lang.extend', true), $package->getFunctionCalls('dojo.mixin'), $package->getFunctionCalls('dojo.lang.mixin'));
31  $declarations = $package->getFunctionDeclarations();
32  $objects = $package->getObjects();
33  $aliases = $package->getAliases();
34
35  // Since there can be chase conditions between declarations and calls, we need to find which were "swallowed" by larger blocks
36  $package->removeSwallowed($mixin_calls);
37  $package->removeSwallowed($declarations);
38
39  // Handle compound require calls
40  foreach ($compound_calls as $call) {
41    if ($call->getParameter(0)->isA(DojoObject)) {
42      $object = $call->getParameter(0)->getObject();
43      foreach ($object->getValues() as $key => $value) {
44        if ($value->isA(DojoArray)) {
45          foreach ($value->getArray()->getItems() as $item) {
46            if ($item->isA(DojoString)) {
47              if (!$output['function_names'][$package_name]) {
48                $output['function_names'][$package_name] = array();
49              }
50              $output[$package_name]['meta']['requires'][$key][] = $item->getString();
51            }
52          }
53        }
54      }
55    }
56  }
57
58  // Handle dojo.require calls
59  foreach ($require_calls as $call) {
60    $require = $call->getParameter(0);
61    if ($require->isA(DojoString)) {
62      $output[$package_name]['meta']['requires']['common'][] = $require->getString();
63    }
64  }
65 
66  // Handle dojo.requireAfterIf calls
67  foreach ($require_if_calls as $call) {
68    $environment = $call->getParameter(0);
69    $require = $call->getParameter(1);
70    if ($environment && $require) {
71      $environment = $environment->getValue();
72      $require = $environment->getValue();
73      if ($require instanceof DojoString) {
74                if ($environment instanceof DojoString) {
75            $output[$package_name]['meta']['requires'][$environment->getValue()][] = $require->getValue();
76                }
77            }
78            else {
79                if ($environment->getValue() == "dojo.render.svg.capable") {
80              $output[$package_name]['meta']['requires']['svg'][] = $require;
81                }
82                else if($environment->getValue() == "dojo.render.vml.capable") {
83          $output[$package_name]['meta']['requires']['vml'][] = $require;
84                }
85            }
86    }
87  }
88 
89  // This closely matches dojo.widget.defineWidget as declared in src/widget/Widget.js
90  foreach ($declare_calls as $call) {
91    $init = null;
92    if ($call->getName() == 'dojo.declare') {
93      $args = array($call->getParameter(0), null, $call->getParameter(1), $call->getParameter(2), $call->getParameter(3));
94      $name = $args[0]->getString();
95      if ($args[3]->isA(DojoFunctionDeclare)) {
96        $init = $args[3]->getFunction();
97      }
98            if ($args[3]->isA(DojoObject)) {
99                $args[4] = $args[3];
100                $args[3] = null;
101            }
102    }
103    else {
104      if ($call->getParameter(3)->isA(DojoString)) {
105        $args = array($call->getParameter(0), $call->getParameter(3), $call->getParameter(1), $call->getParameter(4), $call->getParameter(2));
106      }
107      else {
108        $args = array($call->getParameter(0));
109        $p = 3;
110        if ($call->getParameter(1)->isA(DojoString)) {
111          array_push($args, $call->getParameter(1), $call->getParameter(2));
112        }
113        else {
114          array_push($args, null, $call->getParameter(1));
115          $p = 2;
116        }
117        if ($call->getParameter($p)->isA(DojoFunctionDeclare)) {
118          $init = $call->getParameter($p)->getFunction();
119          array_push($args, $call->getParameter($p), $call->getParameter($p + 1));
120        }
121        else {
122          array_push($args, null, $call->getParameter($p));
123        }
124      }
125    }
126   
127    $package_name = $package_name;
128    $name = $args[0]->getString();
129
130    // $args looks like (name, null, superclass(es), initializer, mixins)     
131    if ($args[2]->isA(DojoVariable)) {
132      $output[$package_name]['meta']['functions'][$name]['meta']['prototype_chain'][] = $args[2]->getVariable();
133      $output[$package_name]['meta']['functions'][$name]['meta']['call_chain'][] = $args[2]->getVariable();
134    }
135    elseif ($args[2]->isA(DojoArray)) {
136      $items = $args[2]->getArray()->getItems();
137      foreach ($items as $item) {
138        if ($item->isA(DojoString)) {
139          $item = $item->getString();
140          $output[$package_name]['meta']['functions'][$name]['meta']['prototype_chain'][] = $item;
141          $output[$package_name]['meta']['functions'][$name]['meta']['call_chain'][] = $item;
142        }
143      }
144    }
145
146    if ($args[4]->isA(DojoObject)) {
147      $object = $args[4]->getObject();
148            $object->setName($name);
149      $object->setAnonymous(true);
150      $values = $object->getValues();
151      foreach ($values as $key => $value) {
152        if ($key == 'initializer' && $value->isA(DojoFunctionDeclare)) {
153          $init = $value->getFunction();
154                    $init->setConstructor(true);
155          continue;
156        }
157        if ($value->isA(DojoFunctionDeclare)) {
158          $function = $value->getFunction($value);
159          $function->setPrototype($name);
160        }
161        elseif (!$value->isA(DojoObject)) {
162          $output[$package_name]['meta']['functions'][$name]['meta']['prototype_variables'][] = $key;
163        }
164      }
165      $object->rollOut($output, 'function');
166      if ($object->getBlockComment('summary')) {
167        $output[$package_name]['meta']['functions'][$name]['meta']['summary'] = $object->getBlockComment('summary');
168      }
169      if ($object->getBlockComment('description')) {
170        $output[$package_name]['meta']['functions'][$name]['meta']['description'] = $object->getBlockComment('description');
171      }
172    }
173   
174    if ($init) {
175      $init->setFunctionName($name);
176      $init->rollOut($output);
177    }
178  }
179 
180  // Handle function declarations
181  foreach ($declarations as $declaration) {
182    $declaration->rollOut($output);
183  }
184 
185  foreach ($inherit_calls as $call) {
186    if ($call->getParameter(0)->isA(DojoVariable) && $call->getParameter(1)->isA(DojoVariable)) {
187      $output[$package_name]['meta']['functions'][$call->getParameter(0)->getVariable()]['meta']['prototype_chain'][] = $call->getParameter(1)->getVariable();
188    }
189  }
190
191  // Handle. dojo.lang.extend and dojo.lang.mixin calls
192  foreach ($mixin_calls as $call) {
193    if ($call->getParameter(0)->isA(DojoVariable)) {
194      $object = $call->getParameter(0)->getVariable();
195      $call_name = $call->getName();
196
197      if ($call->getParameter(1)->isA(DojoObject)) {
198        $properties = $call->getParameter(1)->getObject();
199        $keys = $properties->getValues();
200        foreach ($keys as $key => $function) {
201          if ($function->isA(DojoFunctionDeclare)) {
202            $function = $function->getFunction();
203            if ($call_name == 'dojo.lang.extend') {
204              $function->setPrototype($object);
205            }
206            $function->setFunctionName($object . '.' . $key);
207            $function->rollOut($output);
208          }
209          else {
210            if ($call_name == 'dojo.lang.mixin' || $call_name == 'dojo.mixin') {
211              $output[$package_name]['meta']['functions'][$object]['meta']['variables'][] = $key;
212            }
213            else {
214              $output[$package_name]['meta']['functions'][$object]['meta']['prototype_variables'][] = $key;
215            }
216          }
217        }
218      }
219      elseif ($call->getParameter(1)->isA(DojoString)) {
220        $properties = $call->getParameter(1)->getString();
221        // Note: inherits expects to be reading from prototype values
222        if ($call_name == 'dojo.lang.extend' && strpos($properties, '.prototype') !== false) {
223          $output[$package_name]['meta']['functions'][$object]['meta']['prototype_chain'][] = str_replace('.prototype', '', $properties);
224        }
225        elseif ($call_name == 'dojo.lang.extend' && strpos($properties, 'new ') !== false) {
226          $output[$package_name]['meta']['functions'][$object]['meta']['prototype_chain'][] = str_replace('new ', '', $properties);
227          $output[$package_name]['meta']['functions'][$object]['meta']['call_chain'][] = str_replace('new ', '', $properties);
228        }
229        else {
230          $output[$package_name]['meta']['functions'][$object]['meta']['object_inherits'][] = $properties;
231        }
232      }
233    }
234  }
235 
236  foreach ($objects as $object) {
237    $object->rollOut($output);
238  }
239 
240  if ($output[$package_name]) {
241    $output['function_names'][$package_name] = array();
242    if (!empty($output[$package_name]['meta']['functions'])) {
243      $output['function_names'][$package_name] = array_values(array_keys($output[$package_name]['meta']['functions']));
244    }
245    $output['object_names'][$package_name] = array();
246    if (!empty($output[$package_name]['meta']['objects'])) {
247      $output['object_names'][$package_name] = array_values(array_keys($output[$package_name]['meta']['objects']));
248    }   
249  }
250}
251
252foreach (array_keys($output['function_names']) as $package_name) {
253  sort($output['function_names'][$package_name]);
254}
255
256$output_by_object = array();
257foreach ($output as $package_name => $package_meta) {
258    if (array_key_exists('meta', $package_meta)) {
259        $package_meta = $package_meta['meta'];
260        foreach (array('functions', 'objects') as $type) {
261            if (array_key_exists($type, $package_meta)) {
262                $package_functions = $package_meta[$type];
263                foreach ($package_functions as $function_name => $function_meta) {
264                    $output_by_object[$function_name]['type'] = substr($type, 0, strlen($type) - 1);
265                    $output_by_object[$function_name]['resources'][] = $package_name;
266                    $output_by_object[$function_name]['resources'] = array_unique($output_by_object[$function_name]['resources']);
267                    if (array_key_exists('extra', $function_meta)) {
268                        $function_extra = $function_meta['extra'];
269                        foreach ($function_extra as $key => $value) {
270                            if (is_array($value)) {
271                                if (empty($output_by_object[$function_name][$key])) {
272                                    $output_by_object[$function_name][$key] = array();
273                                }
274                                $output_by_object[$function_name][$key] = array_unique(array_merge($value, $output_by_object[$function_name][$key]));
275                            }
276                            else {
277                                $output_by_object[$function_name][$key] = $value;
278                            }
279                        }
280                    }
281                    if (array_key_exists('meta', $function_meta)) {
282                        $function_meta = $function_meta['meta'];
283                        foreach ($function_meta as $key => $value) {
284                            if (is_array($value)) {
285                                if (empty($output_by_object[$function_name][$key])) {
286                                    $output_by_object[$function_name][$key] = array();
287                                }
288                                $output_by_object[$function_name][$key] = array_unique(array_merge($value, $output_by_object[$function_name][$key]));
289                            }
290                            else {
291                                $output_by_object[$function_name][$key] = $value;
292                            }
293                        }
294                    }
295                }
296            }
297        }
298    }
299}
300
301$timer->setMarker("Main Processing finished");
302
303//header("Content-type: text/plain");
304//print_r($output);
305
306$plugins = new Plugins('.');
307$plugins->write($output, $output_by_object);
308
309$timer->stop();
310if (isset($_GET['benchmark'])) {
311  $timer->display();
312}
313
314echo "Parsing complete!\n";
315
316?>
Note: See TracBrowser for help on using the browser.