PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes/TYPO3/FLOW3/Reflection/DocCommentParser.php

https://github.com/christianjul/FLOW3-Composer
PHP | 125 lines | 50 code | 13 blank | 62 comment | 10 complexity | 00d2020371a6588382404d9c5b7bda5c MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\Reflection;
  3. /* *
  4. * This script belongs to the FLOW3 framework. *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License, either version 3 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * The TYPO3 project - inspiring people to share! *
  11. * */
  12. use TYPO3\FLOW3\Annotations as FLOW3;
  13. /**
  14. * A little parser which creates tag objects from doc comments
  15. *
  16. * @FLOW3\Scope("singleton")
  17. */
  18. class DocCommentParser {
  19. /**
  20. * The description as found in the doc comment
  21. * @var string
  22. */
  23. protected $description = '';
  24. /**
  25. * An array of tag names and their values (multiple values are possible)
  26. * @var array
  27. */
  28. protected $tags = array();
  29. /**
  30. * Parses the given doc comment and saves the result (description and
  31. * tags) in the parser's object. They can be retrieved by the
  32. * getTags() getTagValues() and getDescription() methods.
  33. *
  34. * @param string $docComment A doc comment as returned by the reflection getDocComment() method
  35. * @return void
  36. */
  37. public function parseDocComment($docComment) {
  38. $this->description = '';
  39. $this->tags = array();
  40. $lines = explode(chr(10), $docComment);
  41. foreach ($lines as $line) {
  42. $line = trim($line);
  43. if ($line === '*/') break;
  44. if (strlen($line) > 0 && strpos($line, '* @') !== FALSE) {
  45. $this->parseTag(substr($line, strpos($line, '@')));
  46. } else if (count($this->tags) === 0) {
  47. $this->description .= preg_replace('/\s*\\/?[\\\\*]*\s?(.*)$/', '$1', $line) . chr(10);
  48. }
  49. }
  50. $this->description = trim($this->description);
  51. }
  52. /**
  53. * Returns the tags which have been previously parsed
  54. *
  55. * @return array Array of tag names and their (multiple) values
  56. */
  57. public function getTagsValues() {
  58. return $this->tags;
  59. }
  60. /**
  61. * Returns the values of the specified tag. The doc comment
  62. * must be parsed with parseDocComment() before tags are
  63. * available.
  64. *
  65. * @param string $tagName The tag name to retrieve the values for
  66. * @return array The tag's values
  67. * @throws \TYPO3\FLOW3\Reflection\Exception
  68. */
  69. public function getTagValues($tagName) {
  70. if (!$this->isTaggedWith($tagName)) throw new \TYPO3\FLOW3\Reflection\Exception('Tag "' . $tagName . '" does not exist.', 1169128255);
  71. return $this->tags[$tagName];
  72. }
  73. /**
  74. * Checks if a tag with the given name exists
  75. *
  76. * @param string $tagName The tag name to check for
  77. * @return boolean TRUE the tag exists, otherwise FALSE
  78. */
  79. public function isTaggedWith($tagName) {
  80. return (isset($this->tags[$tagName]));
  81. }
  82. /**
  83. * Returns the description which has been previously parsed
  84. *
  85. * @return string The description which has been parsed
  86. */
  87. public function getDescription() {
  88. return $this->description;
  89. }
  90. /**
  91. * Parses a line of a doc comment for a tag and its value.
  92. * The result is stored in the internal tags array.
  93. *
  94. * @param string $line A line of a doc comment which starts with an @-sign
  95. * @return void
  96. */
  97. protected function parseTag($line) {
  98. $tagAndValue = array();
  99. if (preg_match('/@[A-Za-z0-9\\\\]+\\\\([A-Za-z0-9]+)(?:\\((.*)\\))?$/', $line, $tagAndValue) === 0) {
  100. $tagAndValue = preg_split('/\s/', $line, 2);
  101. } else {
  102. array_shift($tagAndValue);
  103. }
  104. $tag = strtolower(trim($tagAndValue[0], '@'));
  105. if (count($tagAndValue) > 1) {
  106. $this->tags[$tag][] = trim($tagAndValue[1], ' "');
  107. } else {
  108. $this->tags[$tag] = array();
  109. }
  110. }
  111. }
  112. ?>