PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/DocBlox/Transformer/Behaviour/AddLinkInformation.php

https://github.com/androa/Docblox
PHP | 141 lines | 67 code | 16 blank | 58 comment | 5 complexity | 194a19db07ed16a165e6d32a49e4535d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * DocBlox
  4. *
  5. * PHP Version 5
  6. *
  7. * @category DocBlox
  8. * @package Transformer
  9. * @subpackage Behaviour
  10. * @author Mike van Riel <mike.vanriel@naenius.com>
  11. * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT
  13. * @link http://docblox-project.org
  14. */
  15. /**
  16. * Behaviour that adds generated path information on the File elements.
  17. *
  18. * @category DocBlox
  19. * @package Transformer
  20. * @subpackage Behaviour
  21. * @author Mike van Riel <mike.vanriel@naenius.com>
  22. * @license http://www.opensource.org/licenses/mit-license.php MIT
  23. * @link http://docblox-project.org
  24. */
  25. class DocBlox_Transformer_Behaviour_AddLinkInformation implements
  26. DocBlox_Transformer_Behaviour_Interface
  27. {
  28. /** @var DocBlox_Core_Log */
  29. protected $logger = null;
  30. /**
  31. * Sets the logger for this behaviour.
  32. *
  33. * @param DocBlox_Core_Log $log
  34. *
  35. * @return void
  36. */
  37. public function setLogger(DocBlox_Core_Log $log = null)
  38. {
  39. $this->logger = $log;
  40. }
  41. /**
  42. * Adds extra information to the structure.
  43. *
  44. * This method enhances the Structure information with the following information:
  45. * - Every @see tag, or a tag with a type receives an attribute with a direct link to that tag's type entry.
  46. * - Every tag receives an excerpt containing the first 15 characters.
  47. *
  48. * @param DOMDocument $xml
  49. *
  50. * @return DOMDocument
  51. */
  52. public function process(DOMDocument $xml)
  53. {
  54. if ($this->logger) {
  55. $this->logger->log('Adding path information to each xml "file" tag');
  56. }
  57. $xpath = new DOMXPath($xml);
  58. // add to classes
  59. $qry = $xpath->query('//class[full_name]/..');
  60. $class_paths = array();
  61. /** @var DOMElement $element */
  62. foreach ($qry as $element)
  63. {
  64. $path = $element->getAttribute('path');
  65. foreach ($element->getElementsByTagName('class') as $class)
  66. {
  67. $class_paths[$class->getElementsByTagName('full_name')->item(0)->nodeValue] = $path;
  68. }
  69. }
  70. // add to interfaces
  71. $qry = $xpath->query('//interface[full_name]/..');
  72. /** @var DOMElement $element */
  73. foreach ($qry as $element)
  74. {
  75. $path = $element->getAttribute('path');
  76. /** @var DOMElement $class */
  77. foreach ($element->getElementsByTagName('interface') as $class)
  78. {
  79. $class_paths[$class->getElementsByTagName('full_name')->item(0)->nodeValue] = $path;
  80. }
  81. }
  82. // add extra xml elements to tags
  83. if ($this->logger) {
  84. $this->logger->log('Adding link information and excerpts to all DocBlock tags');
  85. }
  86. $qry = $xpath->query('//docblock/tag/@type|//docblock/tag/type|//extends|//implements');
  87. /** @var DOMElement $element */
  88. foreach ($qry as $element)
  89. {
  90. $type = rtrim($element->nodeValue, '[]');
  91. $node = ($element->nodeType == XML_ATTRIBUTE_NODE)
  92. ? $element->parentNode
  93. : $element;
  94. if (isset($class_paths[$type])) {
  95. $file_name = $this->generateFilename($class_paths[$type]);
  96. $node->setAttribute('link', $file_name . '#' . $type);
  97. }
  98. // add a 15 character excerpt of the node contents, meant for the sidebar
  99. $node->setAttribute('excerpt', utf8_encode(substr($type, 0, 15) . (strlen($type) > 15 ? '...' : '')));
  100. }
  101. $qry = $xpath->query('//docblock/tag[@name="see" or @name="throw" or @name="throws"]');
  102. /** @var DOMElement $element */
  103. foreach ($qry as $element)
  104. {
  105. $node_value = explode('::', $element->nodeValue);
  106. if (isset($class_paths[$node_value[0]])) {
  107. $file_name = $this->generateFilename($class_paths[$node_value[0]]);
  108. $element->setAttribute('link', $file_name . '#' . $element->nodeValue);
  109. }
  110. }
  111. return $xml;
  112. }
  113. /**
  114. * Converts a source file name to the name used for generating the end result.
  115. *
  116. * @param string $file
  117. *
  118. * @return string
  119. */
  120. public function generateFilename($file)
  121. {
  122. $info = pathinfo(str_replace(DIRECTORY_SEPARATOR, '_', trim($file, DIRECTORY_SEPARATOR . '.')));
  123. return '_' . $info['filename'] . '.html';
  124. }
  125. }