PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/doclets/xml/classWriter.php

https://github.com/nickdunn/phpdoctor
PHP | 343 lines | 233 code | 96 blank | 14 comment | 26 complexity | 487a878c7aaec7049e4d28450fb5b4ab MD5 | raw file
  1. <?php
  2. class ClassWriter extends HTMLWriter {
  3. function classWriter(&$doclet) {
  4. parent::HTMLWriter($doclet);
  5. $rootDoc =& $this->_doclet->rootDoc();
  6. $phpdoctor =& $this->_doclet->phpdoctor();
  7. $packages =& $rootDoc->packages();
  8. ksort($packages);
  9. foreach ($packages as $packageName => $package) {
  10. $this->_depth = $package->depth() + 1;
  11. $classes =& $package->allClasses();
  12. if ($classes) {
  13. ksort($classes);
  14. foreach ($classes as $name => $class) {
  15. $doc = new DomDocument();
  16. $doc->preserveWhiteSpace = FALSE;
  17. $doc->formatOutput = TRUE;
  18. $dom_class = $doc->createElement('class');
  19. if ($class->isInterface()) {
  20. $dom_class->setAttribute('type', 'interface');
  21. } else {
  22. $dom_class->setAttribute('type', 'class');
  23. }
  24. $dom_class->setAttribute('name', $class->name());
  25. $dom_class->setAttribute('handle', strtolower($class->name()));
  26. $dom_package = $doc->createElement('package');
  27. $dom_package->setAttribute('name', $class->packageName());
  28. $dom_package->setAttribute('handle', $class->packageName());
  29. $dom_class->appendChild($dom_package);
  30. $dom_location = $doc->createElement('location', $class->sourceFilename());
  31. $dom_location->setAttribute('line', $class->sourceLine());
  32. $dom_class->appendChild($dom_location);
  33. $implements =& $class->interfaces();
  34. if (count($implements) > 0) {
  35. $dom_interfaces = $doc->createElement('interfaces');
  36. foreach ($implements as $interface) {
  37. $dom_interface = $doc->createElement('interface', $interface->name());
  38. $dom_interface->setAttribute('package', $interface->packageName());
  39. $dom_interfaces->appendChild($dom_interface);
  40. }
  41. $dom_class->appendChild($dom_interfaces);
  42. }
  43. $dom_modifiers = $doc->createElement('modifiers');
  44. foreach(explode(' ', trim($class->modifiers())) as $modifier) {
  45. $dom_modifiers->appendChild($doc->createElement('modifier', $modifier));
  46. }
  47. $dom_class->appendChild($dom_modifiers);
  48. if ($class->superclass()) {
  49. $superclass =& $rootDoc->classNamed($class->superclass());
  50. if ($superclass) {
  51. $dom_superclass = $doc->createElement('superclass', $superclass->name());
  52. } else {
  53. $dom_superclass = $doc->createElement('superclass', $class->superclass());
  54. }
  55. $dom_class->appendChild($dom_superclass);
  56. }
  57. $textTag =& $class->tags('@text');
  58. if ($textTag) {
  59. $dom_description = $doc->createElement('description', $this->_processInlineTags($textTag));
  60. $dom_class->appendChild($dom_description);
  61. }
  62. $this->_processTags($class->tags(), $doc, $dom_class);
  63. $constants =& $class->constants();
  64. ksort($constants);
  65. $fields =& $class->fields();
  66. ksort($fields);
  67. $methods =& $class->methods();
  68. ksort($methods);
  69. if ($constants) {
  70. $dom_constants = $doc->createElement('constants');
  71. foreach ($constants as $field) {
  72. $textTag =& $field->tags('@text');
  73. $dom_constant = $doc->createElement('constant');
  74. $dom_modifiers = $doc->createElement('modifiers');
  75. foreach(explode(' ', trim($field->modifiers())) as $modifier) {
  76. $dom_modifiers->appendChild($doc->createElement('modifier', $modifier));
  77. }
  78. $dom_constant->appendChild($dom_modifiers);
  79. $type = $field->typeAsString();
  80. $type = $this->__removeTextFromMarkup($type);
  81. $dom_constant->setAttribute('name', ((!$field->constantValue()) ? "$" : "") . $field->name());
  82. $dom_constant->setAttribute('type', $type);
  83. if ($field->value()) $dom_constant->setAttribute('value', htmlspecialchars($field->value()));
  84. $dom_constant_location = $doc->createElement('location', $field->sourceFilename());
  85. $dom_constant_location->setAttribute('line', $field->sourceLine());
  86. $dom_constant->appendChild($dom_constant_location);
  87. if ($textTag) {
  88. $dom_constant_description = $doc->createElement('description',
  89. $this->_processInlineTags($textTag)
  90. );
  91. $dom_constant->appendChild($dom_constant_description);
  92. }
  93. $this->_processTags($field->tags(), $doc, $dom_constant);
  94. $dom_constants->appendChild($dom_constant);
  95. }
  96. $dom_class->appendChild($dom_constants);
  97. }
  98. if ($fields) {
  99. $dom_fields = $doc->createElement('fields');
  100. foreach ($fields as $field) {
  101. $textTag =& $field->tags('@text');
  102. $dom_field = $doc->createElement('field');
  103. $dom_modifiers = $doc->createElement('modifiers');
  104. foreach(explode(' ', trim($field->modifiers())) as $modifier) {
  105. $dom_modifiers->appendChild($doc->createElement('modifier', $modifier));
  106. }
  107. $dom_field->appendChild($dom_modifiers);
  108. $type = $field->typeAsString();
  109. $type = $this->__removeTextFromMarkup($type);
  110. $dom_field->setAttribute('name', ((!$field->constantValue()) ? "$" : "") . $field->name());
  111. $dom_field->setAttribute('type', $type);
  112. if ($field->value()) $dom_field->setAttribute('value', htmlspecialchars($field->value()));
  113. $dom_field_location = $doc->createElement('location', $field->sourceFilename());
  114. $dom_field_location->setAttribute('line', $field->sourceLine());
  115. $dom_field->appendChild($dom_field_location);
  116. if ($textTag) {
  117. $dom_field_description = $doc->createElement('description',
  118. $this->_processInlineTags($textTag)
  119. );
  120. $dom_field->appendChild($dom_field_description);
  121. }
  122. $this->_processTags($field->tags(), $doc, $dom_field);
  123. $dom_fields->appendChild($dom_field);
  124. }
  125. $dom_class->appendChild($dom_fields);
  126. }
  127. if ($class->superclass()) {
  128. $superclass =& $rootDoc->classNamed($class->superclass());
  129. if ($superclass) {
  130. $dom_inherited_fields = $doc->createElement('inherited-fields');
  131. $this->inheritFields($superclass, $rootDoc, $package, $doc, $dom_inherited_fields);
  132. $dom_class->appendChild($dom_inherited_fields);
  133. }
  134. }
  135. if ($methods) {
  136. $dom_methods = $doc->createElement('methods');
  137. foreach($methods as $method) {
  138. $textTag =& $method->tags('@text');
  139. $dom_method = $doc->createElement('method');
  140. $dom_modifiers = $doc->createElement('modifiers');
  141. foreach(explode(' ', trim($method->modifiers())) as $modifier) {
  142. $dom_modifiers->appendChild($doc->createElement('modifier', $modifier));
  143. }
  144. $dom_method->appendChild($dom_modifiers);
  145. $type = $method->returnTypeAsString();
  146. $type = $this->__removeTextFromMarkup($type);
  147. $dom_method->setAttribute('name', $method->name());
  148. $dom_method->setAttribute('return', $type);
  149. $dom_signature = $doc->createElement('parameters');
  150. $this->getSignature($method, $doc, $dom_signature);
  151. $dom_method->appendChild($dom_signature);
  152. $dom_method_location = $doc->createElement('location', $method->sourceFilename());
  153. $dom_method_location->setAttribute('line', $method->sourceLine());
  154. $dom_method->appendChild($dom_method_location);
  155. if ($textTag) {
  156. $dom_method_description = $doc->createElement('description',
  157. $this->_processInlineTags($textTag)
  158. );
  159. $dom_method->appendChild($dom_method_description);
  160. }
  161. $this->_processTags($method->tags(), $doc, $dom_method);
  162. $dom_methods->appendChild($dom_method);
  163. }
  164. $dom_class->appendChild($dom_methods);
  165. }
  166. if ($class->superclass()) {
  167. $superclass =& $rootDoc->classNamed($class->superclass());
  168. if ($superclass) {
  169. $dom_inherited_methods = $doc->createElement('inherited-methods');
  170. $this->inheritMethods($superclass, $rootDoc, $package, $doc, $dom_inherited_methods);
  171. $dom_class->appendChild($dom_inherited_methods);
  172. }
  173. }
  174. $doc->appendChild($dom_class);
  175. $this->_output = $doc->saveXML();
  176. $this->_write($package->asPath().'/'.strtolower($class->name()).'.xml', $class->name(), TRUE);
  177. }
  178. }
  179. }
  180. }
  181. /** Display the inherited fields of an element. This method calls itself
  182. * recursively if the element has a parent class.
  183. *
  184. * @param ProgramElementDoc element
  185. * @param RootDoc rootDoc
  186. * @param PackageDoc package
  187. */
  188. function inheritFields(&$element, &$rootDoc, &$package, $doc, &$dom_wrapper)
  189. {
  190. $fields =& $element->fields();
  191. if ($fields) {
  192. ksort($fields);
  193. $num = count($fields); $foo = 0;
  194. $dom_class = $doc->createElement('class');
  195. $dom_class->setAttribute('name', $element->_name);
  196. $dom_class->setAttribute('package', $element->_package);
  197. foreach($fields as $field) {
  198. $dom_wrapper->setAttribute('package', $field->packageName());
  199. $class =& $field->containingClass();
  200. $dom_wrapper->setAttribute('class', $class->name());
  201. $dom_field = $doc->createElement('field');
  202. $dom_field->setAttribute('name', $field->name());
  203. $dom_class->appendChild($dom_field);
  204. }
  205. $dom_wrapper->appendChild($dom_class);
  206. if ($element->superclass()) {
  207. $superclass =& $rootDoc->classNamed($element->superclass());
  208. if ($superclass) {
  209. $this->inheritFields($superclass, $rootDoc, $package, $doc, $dom_wrapper);
  210. }
  211. }
  212. }
  213. }
  214. /** Display the inherited methods of an element. This method calls itself
  215. * recursively if the element has a parent class.
  216. *
  217. * @param ProgramElementDoc element
  218. * @param RootDoc rootDoc
  219. * @param PackageDoc package
  220. */
  221. function inheritMethods(&$element, &$rootDoc, &$package, $doc, &$dom_wrapper)
  222. {
  223. $methods =& $element->methods();
  224. if ($methods) {
  225. ksort($methods);
  226. $num = count($methods); $foo = 0;
  227. $dom_class = $doc->createElement('class');
  228. $dom_class->setAttribute('name', $element->_name);
  229. $dom_class->setAttribute('package', $element->_package);
  230. foreach($methods as $method) {
  231. $dom_wrapper->setAttribute('package', $method->packageName());
  232. $class =& $method->containingClass();
  233. $dom_wrapper->setAttribute('class', $class->name());
  234. $dom_method = $doc->createElement('method');
  235. $dom_method->setAttribute('name', $method->name());
  236. $dom_class->appendChild($dom_method);
  237. }
  238. $dom_wrapper->appendChild($dom_class);
  239. if ($element->superclass()) {
  240. $superclass =& $rootDoc->classNamed($element->superclass());
  241. if ($superclass) {
  242. $this->inheritMethods($superclass, $rootDoc, $package, $doc, $dom_wrapper);
  243. }
  244. }
  245. }
  246. }
  247. }