PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/tatamiJS/src/com/objetdirect/tatami/public/util/docscripts/generate.php

http://tatami.googlecode.com/
PHP | 374 lines | 291 code | 64 blank | 19 comment | 85 complexity | 651e7af222f6cf2a3f5a444806644d4b MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, LGPL-2.0, MIT
  1. <?php
  2. # php generate.php
  3. # -- Runs everything in the modules directory
  4. # php generate.php custom
  5. # -- Runs only the module starting with "custom"
  6. # php generate.php --outfile=custom-api custom
  7. # -- Runs the custom module, serializes to custom-api.xml
  8. # php generate.php dijit dojo
  9. # -- Runs both dijit and dojo modules
  10. if ($_SERVER['HTTP_HOST']) {
  11. die('Run from command line');
  12. }
  13. error_reporting(E_ALL ^ E_NOTICE);
  14. $debug = true;
  15. require_once('includes/dojo.inc');
  16. require_once('lib/generator/JsonSerializer.php');
  17. require_once('lib/generator/XmlSerializer.php');
  18. require_once('lib/generator/Freezer.php');
  19. $keys = array();
  20. $namespaces = array();
  21. // Do this in 3 parts
  22. // 1. Turn all variables into single objects
  23. // 2. Internalize class members
  24. // 3. Serialize objects
  25. $args = array();
  26. $outfile = null;
  27. $clean = false;
  28. foreach (array_slice($argv, 1) as $arg) {
  29. if ($arg{0} == '-') {
  30. if (preg_match('%^--outfile=(.+)$%', $arg, $match)) {
  31. $outfile = $match[1];
  32. }
  33. elseif ($arg == '--clean') {
  34. $clean = true;
  35. }
  36. else {
  37. die("ERROR: Unrecognized argument: $arg\n");
  38. }
  39. }
  40. else {
  41. $args[] = $arg;
  42. }
  43. }
  44. function _unlink($loc){
  45. file_exists($loc) && unlink($loc);
  46. }
  47. if ($clean) {
  48. _unlink('cache/nodes');
  49. _unlink('cache/resources');
  50. _unlink('cache/' . ($outfile ? $outfile : 'api') . '.json');
  51. _unlink('cache/' . ($outfile ? $outfile : 'api') . '.xml');
  52. }
  53. $files = dojo_get_files($args);
  54. $nodes = new Freezer('cache', 'nodes');
  55. $resources = new Freezer('cache', 'resources');
  56. print "=== PARSING FILES ===\n";
  57. flush();
  58. foreach ($files as $set){
  59. list($namespace, $file) = $set;
  60. if (!$namespaces[$namespace]) {
  61. $namespaces[$namespace] = true;
  62. }
  63. $ctime = dojo_get_file_time($namespace, $file);
  64. if ($ctime == $resources->open($namespace . '%' . $file, null)) {
  65. continue;
  66. }
  67. print "$namespace/$file\t\t" . memory_get_usage() . "\n";
  68. flush();
  69. $contents = dojo_get_contents($namespace, $file);
  70. $provides = $contents['#provides'];
  71. unset($contents['#provides']);
  72. $resource = $contents['#resource'];
  73. unset($contents['#resource']);
  74. $requires = $contents['#requires'];
  75. unset($contents['#requires']);
  76. foreach ($contents as $var => $content) {
  77. foreach ($content as $key_key => $key_value) {
  78. $key_type = 'undefined';
  79. if (is_numeric($key_value)) {
  80. $key_type = 'numeric';
  81. }elseif(is_array($key_value)) {
  82. $key_type = 'array';
  83. }elseif(is_bool($key_value)) {
  84. $key_type = 'bool';
  85. }elseif(is_string($key_value)) {
  86. $key_type = 'string';
  87. }
  88. $keys[$key_key][] = $key_type;
  89. $keys[$key_key] = array_unique($keys[$key_key]);
  90. }
  91. $node = $nodes->open($var, array());
  92. $new = !empty($node);
  93. // Handle file-level information
  94. if (!is_array($node['#provides']) || !in_array($provides, $node['#provides'])) {
  95. $node['#provides'][] = $provides;
  96. }
  97. if (!is_array($node['#namespaces']) || !in_array($namespace, $node['#namespaces'])) {
  98. $node['#namespaces'][] = $namespace;
  99. }
  100. $node['#resource'][] = "$namespace/$resource";
  101. if (trim($content['type'])) {
  102. $node['type'] = $content['type'];
  103. }
  104. if (!empty($content['tags'])) {
  105. $node['tags'] = $content['tags'];
  106. }
  107. if (!empty($content['private'])) {
  108. $node['private'] = $content['private'];
  109. }
  110. if (!empty($content['private_parent'])) {
  111. $node['private_parent'] = $content['private_parent'];
  112. }
  113. if (trim($content['summary'])) {
  114. $node['summary'] = $content['summary'];
  115. }
  116. if (trim($content['description'])) {
  117. $node['description'] = $content['description'];
  118. }
  119. if (trim($content['exceptions'])) {
  120. $node['exceptions'] = $content['exceptions'];
  121. }
  122. if ($content['private']) {
  123. $node['private'] = $content['private'];
  124. }
  125. if ($content['private_parent']) {
  126. $node['private_parent'] = $content['private_parent'];
  127. }
  128. if (is_array($content['alias'])) {
  129. foreach ($content['alias'] as $alias) {
  130. $node['alias'] = $alias;
  131. }
  132. }
  133. if (is_array($content['examples'])) {
  134. foreach ($content['examples'] as $example) {
  135. if (!is_array($node['examples']) || !in_array($example, $node['examples'])) {
  136. $node['examples'][] = $example;
  137. }
  138. }
  139. }
  140. if ($content['instance']) {
  141. $node['instance'] = $content['instance'];
  142. }
  143. if ($content['prototype']) {
  144. $node['prototype'] = $content['prototype'];
  145. }
  146. if (!is_array($node['returns'])) {
  147. $node['returns'] = array();
  148. }
  149. if (trim($content['returns'])) {
  150. $node['returns'] = array_unique(array_merge($node['returns'], explode('|', $content['returns'])));
  151. }
  152. if (trim($content['return_summary'])) {
  153. $node['return_summary'] = $content['return_summary'];
  154. }
  155. foreach (array('prototype', 'instance', 'normal') as $scope) {
  156. if (!empty($content['mixins'][$scope])) {
  157. if (empty($node['mixins'][$scope])) {
  158. $node['mixins'][$scope] = array();
  159. }
  160. $node['mixins'][$scope] = array_unique(array_merge($node['mixins'][$scope], $content['mixins'][$scope]));
  161. }
  162. }
  163. if ($content['type'] == 'Function') {
  164. if ($content['classlike']) {
  165. $node['classlike'] = true;
  166. }
  167. if ($node['chains']) {
  168. if (!$content['chains']['prototype']) {
  169. $content['chains']['prototype'] = array();
  170. }
  171. $node['chains']['prototype'] = array_unique(array_merge($node['chains']['prototype'], $content['chains']['prototype']));
  172. if (!$content['chains']['call']) {
  173. $content['chains']['call'] = array();
  174. }
  175. $node['chains']['call'] = array_unique(array_merge($node['chains']['call'], $content['chains']['call']));
  176. }
  177. else {
  178. $node['chains']['prototype'] = ($content['chains']['prototype']) ? $content['chains']['prototype'] : array();
  179. $node['chains']['call'] = ($content['chains']['call']) ? $content['chains']['call'] : array();
  180. }
  181. if ($content['chains']) {
  182. unset($content['chains']['prototype']);
  183. unset($content['chains']['call']);
  184. $types = array_keys($content['chains']);
  185. if (!empty($types)) {
  186. print_r($types);
  187. die();
  188. }
  189. }
  190. if (!empty($content['parameters'])) {
  191. if (!empty($node['parameters'])) {
  192. $node_parameters = array_keys($node['parameters']);
  193. $content_parameters = array_keys($content['parameters']);
  194. $long_parameters = (count($node_parameters) > count($content_parameters)) ? $node_parameters : $content_parameters;
  195. $short_parameters = (count($node_parameters) > count($content_parameters)) ? $content_parameters : $node_parameters;
  196. $match = true;
  197. foreach ($long_parameters as $i => $parameter) {
  198. if ($i < count($short_parameters) && $parameter != $short_parameters[$i]) {
  199. $match = false;
  200. }
  201. }
  202. if ($match) {
  203. // Only process these if they match the first occurence
  204. foreach ($content['parameters'] as $parameter_name => $parameter) {
  205. if (empty($node['parameters'][$parameter_name]['type'])) {
  206. $node['parameters'][$parameter_name]['type'] = $parameter['type'];
  207. }
  208. if (trim($parameter['summary'])) {
  209. $node['parameters'][$parameter_name]['summary'] = $parameter['summary'];
  210. }
  211. }
  212. }
  213. }
  214. else {
  215. $node['parameters'] = $content['parameters'];
  216. }
  217. }
  218. }
  219. $nodes->save($var, $node);
  220. }
  221. $resources->save($namespace . '%' . $file, $ctime);
  222. // print_r($keys);
  223. }
  224. unset($resources);
  225. print "=== BUILDING OBJECT STRUCTURE ===\n";
  226. flush();
  227. $roots = array();
  228. $ids = $nodes->ids();
  229. $percent = 0;
  230. foreach ($ids as $pos => $id) {
  231. $new_percent = floor($pos / count($ids) * 50);
  232. if ($new_percent % 5 == 0 && $percent % 5 != 0) {
  233. print floor($new_percent) . "%\n";
  234. }
  235. $percent = $new_percent;
  236. $parts = explode('.', $id);
  237. if (count($parts) > 1) {
  238. $name = array_pop($parts);
  239. $parent = implode('.', $parts);
  240. $node = $nodes->open($id, array());
  241. if (!is_array($node['#namespaces']) || (count($args) && !count(array_intersect($args, $node['#namespaces'])))) {
  242. continue;
  243. }
  244. if (!array_key_exists($parent, $roots)) {
  245. $roots[$parent] = array();
  246. }
  247. if ($node['type'] == 'Function') {
  248. $roots[$id]['function'] = true;
  249. }
  250. if ($node['classlike']) {
  251. $roots[$id]['classlike'] = true;
  252. }
  253. }
  254. }
  255. // Figure out whether a root item has children or not
  256. $pos = 0;
  257. $root_count = count($roots);
  258. foreach ($roots as $id => $root) {
  259. $new_percent = floor(50 + ($pos++ / $root_count * 50));
  260. if ($new_percent % 5 == 0 && $percent % 5 != 0) {
  261. print floor($new_percent) . "%\n";
  262. }
  263. $percent = $new_percent;
  264. if ($root['function'] && !$root['classlike']) {
  265. $has_children = false;
  266. $parts = explode('.', $id);
  267. if (count($parts) > 1) {
  268. foreach ($roots as $possible_child_id => $possible_child) {
  269. $child_parts = explode('.', $possible_child_id);
  270. if (count($child_parts) == count($parts)+1 && strpos($possible_child_id, "$id.") === 0) {
  271. $has_children = true;
  272. break;
  273. }
  274. }
  275. if (!$has_children) {
  276. unset($roots[$id]);
  277. }
  278. }
  279. }
  280. }
  281. print "=== SERIALIZING OBJECTS ===\n";
  282. // Aggregate and save
  283. if ($outfile) {
  284. $json = new JsonSerializer('cache', 'json', $outfile);
  285. $xml = new XmlSerializer('cache', 'xml', $outfile);
  286. }
  287. else {
  288. $json = new JsonSerializer('cache', 'json');
  289. $xml = new XmlSerializer('cache', 'xml');
  290. }
  291. foreach ($roots as $id => $root) {
  292. if(!$id){
  293. // Minor bug
  294. continue;
  295. }
  296. $node = $nodes->open($id, null);
  297. $parts = explode('.', $id);
  298. foreach ($ids as $child_id) {
  299. $child_parts = explode('.', $child_id);
  300. if (count($child_parts) == count($parts)+1 && strpos($child_id, "$id.") === 0 && !array_key_exists($child_id, $roots)) {
  301. $node['#children'][array_pop($child_parts)] = $nodes->open($child_id, null);
  302. }
  303. }
  304. print "$id\t\t" . memory_get_usage() . "\n";
  305. flush();
  306. $json->setObject($id, $node);
  307. $xml->setObject($id, $node);
  308. }
  309. // * Assemble parent/child relationships
  310. ?>