/libs/phaser/docgen/src/Processor.php

https://gitlab.com/davidgranado/space-strategy-game · PHP · 397 lines · 287 code · 78 blank · 32 comment · 33 complexity · 5e9a582390d2186d15c6fcdeec9ffe1c MD5 · raw file

  1. <?php
  2. class Processor
  3. {
  4. public $file;
  5. public $blocks;
  6. public $class;
  7. public $consts;
  8. public $methods;
  9. public $properties;
  10. public $docgen;
  11. public $processLog;
  12. public $corrupted;
  13. /**
  14. * Processes the given JS source file.
  15. *
  16. * @param mixed $file
  17. * @return Processor
  18. */
  19. public function __construct($docgen, $file)
  20. {
  21. $this->docgen = $docgen;
  22. $this->class = null;
  23. $this->consts = [];
  24. $this->methods = [];
  25. $this->properties = [];
  26. $this->file = $file;
  27. $this->corrupted = false;
  28. $this->methods['private'] = [];
  29. $this->methods['protected'] = [];
  30. $this->methods['public'] = [];
  31. $this->methods['static'] = [];
  32. $this->properties['private'] = [];
  33. $this->properties['protected'] = [];
  34. $this->properties['public'] = [];
  35. $this->scanFile();
  36. }
  37. /**
  38. * Scans the given JS source file and extracts blocks from it
  39. */
  40. private function scanFile() {
  41. $js = file($this->file);
  42. $scanningForOpen = true;
  43. $scanningForClose = false;
  44. $openLine = 0;
  45. $closeLine = 0;
  46. $chunk = [];
  47. // Literally scan the JS file, line by line
  48. for ($i = 0; $i < count($js); $i++)
  49. {
  50. $line = trim($js[$i]);
  51. if ($scanningForOpen && $line === "/**")
  52. {
  53. $scanningForOpen = false;
  54. $scanningForClose = true;
  55. $chunk = [];
  56. $openLine = $i;
  57. }
  58. if ($scanningForClose && $line === "*/")
  59. {
  60. $scanningForOpen = true;
  61. $scanningForClose = false;
  62. $closeLine = $i;
  63. // The first element is always the opening /** so remove it
  64. array_shift($chunk);
  65. if (isset($js[$i + 1]))
  66. {
  67. $this->blocks[] = new Block($openLine, $closeLine, $js[$i + 1], $chunk);
  68. }
  69. }
  70. else
  71. {
  72. $chunk[] = $line;
  73. }
  74. }
  75. // Process the data into our documentation types
  76. for ($i = 0; $i < $this->total(); $i++)
  77. {
  78. if ($this->blocks[$i]->isClass)
  79. {
  80. $tempClass = new ClassDesc($this, $this->blocks[$i]);
  81. $this->class = $tempClass;
  82. if ($tempClass->corrupted)
  83. {
  84. $this->corrupted = true;
  85. }
  86. }
  87. else if ($this->blocks[$i]->isConst)
  88. {
  89. $tempConst = new Constant($this, $this->blocks[$i]);
  90. $this->consts[$tempConst->name] = $tempConst;
  91. }
  92. else if ($this->blocks[$i]->isMethod)
  93. {
  94. $tempMethod = new Method($this, $this->blocks[$i]);
  95. if ($tempMethod->isPublic)
  96. {
  97. $this->methods['public'][$tempMethod->name] = $tempMethod;
  98. }
  99. else if ($tempMethod->isProtected)
  100. {
  101. $this->methods['protected'][$tempMethod->name] = $tempMethod;
  102. }
  103. else if ($tempMethod->isPrivate)
  104. {
  105. $this->methods['private'][$tempMethod->name] = $tempMethod;
  106. }
  107. else if ($tempMethod->isStatic)
  108. {
  109. $this->methods['static'][$tempMethod->name] = $tempMethod;
  110. }
  111. }
  112. else if ($this->blocks[$i]->isProperty)
  113. {
  114. $tempProperty = new Property($this, $this->blocks[$i]);
  115. if ($tempProperty->corrupted === false)
  116. {
  117. if ($tempProperty->isPublic)
  118. {
  119. $this->properties['public'][$tempProperty->name] = $tempProperty;
  120. }
  121. else if ($tempProperty->isProtected)
  122. {
  123. $this->properties['protected'][$tempProperty->name] = $tempProperty;
  124. }
  125. else if ($tempProperty->isPrivate)
  126. {
  127. $this->properties['private'][$tempProperty->name] = $tempProperty;
  128. }
  129. }
  130. }
  131. }
  132. if ($this->class === null)
  133. {
  134. $this->corrupted = true;
  135. }
  136. $this->sortArrays();
  137. }
  138. public function getPublicProperties()
  139. {
  140. return $this->properties['public'];
  141. }
  142. public function getPublicMethods()
  143. {
  144. return $this->methods['public'];
  145. }
  146. public function getArray()
  147. {
  148. $consts = [];
  149. $methods = [];
  150. $properties = [];
  151. foreach ($this->consts as $key => $value)
  152. {
  153. $consts[] = $value->getArray();
  154. }
  155. // Methods
  156. $methods['public'] = [];
  157. $methods['protected'] = [];
  158. $methods['private'] = [];
  159. $methods['static'] = [];
  160. foreach ($this->methods['public'] as $key => $value)
  161. {
  162. $methods['public'][] = $value->getArray();
  163. }
  164. foreach ($this->methods['protected'] as $key => $value)
  165. {
  166. $methods['protected'][] = $value->getArray();
  167. }
  168. foreach ($this->methods['private'] as $key => $value)
  169. {
  170. $methods['private'][] = $value->getArray();
  171. }
  172. foreach ($this->methods['static'] as $key => $value)
  173. {
  174. $methods['static'][] = $value->getArray();
  175. }
  176. // Properties
  177. $properties['public'] = [];
  178. $properties['protected'] = [];
  179. $properties['private'] = [];
  180. foreach ($this->properties['public'] as $key => $value)
  181. {
  182. $properties['public'][] = $value->getArray();
  183. }
  184. foreach ($this->properties['protected'] as $key => $value)
  185. {
  186. $properties['protected'][] = $value->getArray();
  187. }
  188. foreach ($this->properties['private'] as $key => $value)
  189. {
  190. $properties['private'][] = $value->getArray();
  191. }
  192. return array(
  193. 'class' => $this->class->getArray(),
  194. 'consts' => $consts,
  195. 'methods' => $methods,
  196. 'properties' => $properties
  197. );
  198. }
  199. public function getJSON()
  200. {
  201. return json_encode($this->getArray());
  202. }
  203. public function getConstsArray()
  204. {
  205. $out = [];
  206. for ($i = 0; $i < count($this->consts); $i++)
  207. {
  208. $out[] = $this->consts[$i]->getArray();
  209. }
  210. return $out;
  211. }
  212. public function extend()
  213. {
  214. // Quick bailout
  215. if (!$this->class->extendsFrom())
  216. {
  217. return;
  218. }
  219. $proc = $this;
  220. do
  221. {
  222. $extends = $proc->class->extends;
  223. $proc = $this->docgen->get($extends);
  224. // echo "\n\nextend found: " . $proc->getName() . "\n";
  225. $this->merge($proc);
  226. }
  227. while ($proc->class->extendsFrom());
  228. $this->sortArrays();
  229. }
  230. public function sortArrays()
  231. {
  232. // Alphabetically sort the arrays based on the key
  233. ksort($this->consts);
  234. ksort($this->methods['public']);
  235. ksort($this->methods['protected']);
  236. ksort($this->methods['private']);
  237. ksort($this->methods['static']);
  238. ksort($this->properties['public']);
  239. ksort($this->properties['protected']);
  240. ksort($this->properties['private']);
  241. }
  242. public function getMethodNames()
  243. {
  244. $output = [];
  245. foreach ($this->methods['public'] as $key => $method)
  246. {
  247. $output[$method->name] = true;
  248. }
  249. return $output;
  250. }
  251. public function getPropertyNames()
  252. {
  253. $output = [];
  254. foreach ($this->properties['public'] as $key => $property)
  255. {
  256. $output[$property->name] = true;
  257. }
  258. return $output;
  259. }
  260. public function merge($processor)
  261. {
  262. // We only want to merge in public methods and properties.
  263. // Technically JavaScript merges in bloody everything, but for the sake of docs we'll keep them #public# only.
  264. $inheritedMethods = $processor->getPublicMethods();
  265. $currentMethods = $this->getMethodNames();
  266. // Flag them as inherited
  267. foreach ($inheritedMethods as $key => $method)
  268. {
  269. if (!array_key_exists($method->name, $currentMethods))
  270. {
  271. $method->inherited = true;
  272. $method->inheritedFrom = $processor->getName();
  273. $this->methods['public'][$method->name] = $method;
  274. }
  275. }
  276. $inheritedProperties = $processor->getPublicProperties();
  277. $currentProperties = $this->getPropertyNames();
  278. // Flag them as inherited!
  279. foreach ($inheritedProperties as $key => $property)
  280. {
  281. if (!array_key_exists($property->name, $currentProperties))
  282. {
  283. $property->inherited = true;
  284. $property->inheritedFrom = $processor->getName();
  285. $this->properties['public'][$property->name] = $property;
  286. }
  287. }
  288. }
  289. /**
  290. * The total number of blocks scanned.
  291. */
  292. public function total()
  293. {
  294. return count($this->blocks);
  295. }
  296. public function log($text) {
  297. $this->processLog[] = $text;
  298. }
  299. public function getLog() {
  300. return $this->processLog;
  301. // return array_reverse($this->processLog);
  302. }
  303. public function getName() {
  304. return $this->class->name;
  305. }
  306. public function __toString()
  307. {
  308. if ($this->corrupted)
  309. {
  310. return "JSDoc Corrupted Class";
  311. }
  312. else
  313. {
  314. return "Class: " . $this->class->name . ", Methods: " . count($this->methods['public']) . ", Properties: " . count($this->properties['public']) . "\n";
  315. }
  316. }
  317. }
  318. ?>