PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/DocBlox/Transformer/Behaviour/GeneratePaths.php

https://github.com/androa/Docblox
PHP | 86 lines | 28 code | 8 blank | 50 comment | 1 complexity | 75d68747744c981661c67eec43bff559 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_GeneratePaths 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 file receives a 'generated-path' attribute which contains the path on the filesystem where the docs for
  46. * that file van be found.
  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. $qry = $xpath->query("/project/file[@path]");
  59. /** @var DOMElement $element */
  60. foreach ($qry as $element) {
  61. $files[] = $element->getAttribute('path');
  62. $element->setAttribute('generated-path', $this->generateFilename($element->getAttribute('path')));
  63. }
  64. return $xml;
  65. }
  66. /**
  67. * Converts a source file name to the name used for generating the end result.
  68. *
  69. * @param string $file
  70. *
  71. * @return string
  72. */
  73. public function generateFilename($file)
  74. {
  75. $info = pathinfo(str_replace(DIRECTORY_SEPARATOR, '_', trim($file, DIRECTORY_SEPARATOR . '.')));
  76. return '_' . $info['filename'] . '.html';
  77. }
  78. }