CommentParser.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\DeploymentConfig;
  7. use Magento\Framework\App\Config\CommentParserInterface;
  8. use Magento\Framework\Filesystem;
  9. use Magento\Framework\App\Filesystem\DirectoryList;
  10. use Magento\Framework\Config\File\ConfigFilePool;
  11. /**
  12. * Parses and retrieves comments from configuration files.
  13. */
  14. class CommentParser implements CommentParserInterface
  15. {
  16. /**
  17. * The library to work with file system.
  18. *
  19. * @var Filesystem
  20. */
  21. private $filesystem;
  22. /**
  23. * Stores file key to file name config.
  24. *
  25. * @var ConfigFilePool
  26. */
  27. private $configFilePool;
  28. /**
  29. * @param Filesystem $filesystem The library to work with file system
  30. * @param ConfigFilePool $configFilePool Stores file key to file name config
  31. */
  32. public function __construct(
  33. Filesystem $filesystem,
  34. ConfigFilePool $configFilePool
  35. ) {
  36. $this->filesystem = $filesystem;
  37. $this->configFilePool = $configFilePool;
  38. }
  39. /**
  40. * Retrieves list of comments from config file.
  41. *
  42. * E.g.,
  43. * ```php
  44. * [
  45. * 'modules' => 'Some comment for the modules section'
  46. * 'system' => 'Some comment for the system section',
  47. * ...
  48. * ]
  49. * ```
  50. *
  51. * The keys of this array are section names to which the comments relate.
  52. * The values of this array are comments for these sections.
  53. *
  54. * If file with provided name does not exist - empty array will be returned.
  55. *
  56. * @param string $fileName The name of config file
  57. * @return array
  58. */
  59. public function execute($fileName)
  60. {
  61. $result = [];
  62. $dirReader = $this->filesystem->getDirectoryRead(DirectoryList::CONFIG);
  63. if (!$dirReader->isExist($fileName)) {
  64. return $result;
  65. }
  66. $fileContent = $dirReader->readFile($fileName);
  67. $commentBlocks = array_filter(
  68. token_get_all($fileContent),
  69. function ($entry) {
  70. return T_DOC_COMMENT == $entry[0];
  71. }
  72. );
  73. foreach ($commentBlocks as $commentBlock) {
  74. $text = $this->getCommentText($commentBlock[1]);
  75. $section = $this->getSectionName($commentBlock[1]);
  76. if ($section && $text) {
  77. $result[$section] = $text;
  78. }
  79. }
  80. return $result;
  81. }
  82. /**
  83. * Retrieves text of comment.
  84. *
  85. * @param string $commentBlock The comment
  86. * @return string|null
  87. */
  88. private function getCommentText($commentBlock)
  89. {
  90. $commentsLine = [];
  91. foreach (preg_split("/(\r?\n)/", $commentBlock) as $commentLine) {
  92. if (preg_match('/^(?=\s+?\*[^\/])(.+)/', $commentLine, $matches)
  93. && false === strpos($commentLine, 'For the section')
  94. ) {
  95. $commentsLine[] = preg_replace('/^(\*\s?)/', '', trim($matches[1]));
  96. }
  97. }
  98. return empty($commentsLine) ? null : implode(PHP_EOL, $commentsLine);
  99. }
  100. /**
  101. * Retrieves section name to which the comment relates.
  102. *
  103. * @param string $comment The comment
  104. * @return string|null
  105. */
  106. private function getSectionName($comment)
  107. {
  108. $pattern = '/\s+\* For the section: (.+)\s/';
  109. preg_match_all($pattern, $comment, $matches);
  110. return empty($matches[1]) ? null : trim(array_shift($matches[1]));
  111. }
  112. }